Ðề tài: CCS C for PIC16F877A
View Single Post
Old 01-12-2007, 12:57 AM   #7
Mr.Bi
Đệ tử 7 túi
 
Tham gia ngày: Jul 2007
Nơi Cư Ngụ: Đà Nẵng
Bài gửi: 188
:
Code:
#include <16F877A.h>
#include <DEFINE_16F877A.h>
#include <DEFINE_16F87x.h>
#fuses  NOWDT,XT,NOPROTECT,NOLVP,PUT
#use    delay(clock=4000000)

#use standard_io (B)
#use standard_io (A)

#define E            PIN_D0
#define RS          PIN_D1
#define RW          PIN_D2
#define LCD_D4      PIN_D4
#define LCD_D5      PIN_D5
#define LCD_D6      PIN_D6
#define LCD_D7      PIN_D7

#bit CANCEL    =0x05.1 // CANCEL = RA1
#bit OK              =0x05.0 // OK    = RA0

unsigned int8 key[] ; // mang luu cac so da nhap tu ban phim
const unsigned int8 num[11] = {'0','1','2','3','4','5','6','7','8','9'} ;
const unsigned int8 num_ss[11] = {' ','3','9','5','2','6','1','0'} ; // cac so dung de so sanh
unsigned int8 a = 0 ; // bien dem cac so nhap tu ban phim
unsigned int8 b = 0 ; // bien dem so lan sai ma

void LCD_Init ( void );                    // ham khoi tao LCD
void LCD_SetPosition ( unsigned int data );//Thiet lap vi tri con tro
void LCD_PutChar ( unsigned int data );    // Ham viet1kitu/1chuoi len LCD
void LCD_PutCmd ( unsigned int data ) ;    // Ham gui lenh len LCD
void tao_xung_E();                        // Xung kich hoat
void send_to_LCD( unsigned int data);      // Dat du lieu len chan Data

void delay_ms_INT(unsigned long i)
{ unsigned long k , j ;
  for(k=0;k<=i;k++) { for(j=0;j<=i;j++){;} }
}

void LCD_Init ( void )
{
    delay_ms(200); // thoi gian de LCD khoi dong

    output_low(RW);        // RW = 0
    output_low(RS);        // che do gui lenh

    send_to_LCD( 0x00 );
    output_high ( E ); output_low ( E ); delay_ms ( 10 );


    send_to_LCD( 0x03 );  /* init with specific nibbles to start 4-bit mode */
    output_high ( E ); output_low ( E ); delay_ms ( 3 ); // tao xung tren chan E
    output_high ( E ); output_low ( E ); delay_ms ( 3 );
    output_high ( E ); output_low ( E ); delay_ms ( 3 );

    send_to_LCD( 0x02 );  /* set 4-bit interface */
    output_high ( E ); output_low ( E ); delay_ms ( 3 ); // tao xung tren chan E


    LCD_PutCmd ( 0x2C );    /* function set (all lines, 5x7 characters) */
    LCD_PutCmd ( 0x0C);    /* display ON, cursor off, no blink */
    LCD_PutCmd ( 0x06 );    /* entry mode set, increment & scroll left */
    LCD_PutCmd ( 0x01 );    /* clear display */
}

void LCD_SetPosition ( unsigned int data )
{

    send_to_LCD( swap ( data ) | 0x08 );
    output_high ( E ); output_low ( E ); delay_ms ( 3 ); // tao xung tren chan E

    send_to_LCD( swap ( data ) );
    output_high ( E ); output_low ( E ); delay_ms ( 3 ); // tao xung tren chan E

}

void LCD_PutChar ( unsigned int data )
{
    output_high( RS ); //RS = 1
    output_low ( RW ); //RW = 0

    send_to_LCD( swap ( data ) );                        // gui 4 bit cao
    output_high ( E ); output_low ( E ); delay_ms ( 3 ); // tao xung tren chan E

    send_to_LCD( swap ( data ) );                        //gui 4 bit thap
    output_high ( E ); output_low ( E ); delay_ms ( 3 ); // tao xung tren chan E

}

void LCD_PutCmd ( unsigned int data )
{
    output_low(RS); // RS = 0
    output_low(RW); // RW = 0

    send_to_LCD( swap(data) );    // gui 4 bit cao
    output_high (E); output_low (E); delay_ms (3);

    send_to_LCD( swap(data) );    // gui 4 bit thap
    output_high (E); output_low (E); delay_ms ( 3 );
}


void send_to_LCD( unsigned int data )
{
    output_bit ( LCD_D4, data & 0x01 );
    output_bit ( LCD_D5, data & 0x02 );
    output_bit ( LCD_D6, data & 0x04 );
    output_bit ( LCD_D7, data & 0x08 );
}

void quetphim_hienthi()
{ PORTB = 0b111111110 ; if (RB4==0) { delay_ms_INT(50);            
                                      while(RB4==0) continue ;
                                      delay_ms_INT(20);
                                      LCD_PutChar('0') ; key[a++] = num[0];
                                    } ;
                        if (RB5==0) { delay_ms_INT(50);            
                                      while(RB5==0) continue ;
                                      delay_ms_INT(20);
                                      LCD_PutChar('3') ; key[a++] = num[3];
                                    } ;
                        if (RB6==0) { delay_ms_INT(50);            
                                      while(RB6==0) continue ;
                                      delay_ms_INT(20);
                                      LCD_PutChar('6') ; key[a++] = num[6];
                                    } ;
                        if (RB7==0) { delay_ms_INT(50);            
                                      while(RB7==0) continue ;
                                      delay_ms_INT(20);
                                      LCD_PutChar('9') ; key[a++] = num[9];
                                    } ;

  PORTB = 0b111111101 ; if (RB4==0) { delay_ms_INT(50);            
                                      while(RB4==0) continue ;
                                      delay_ms_INT(20);
                                      LCD_PutChar('1') ; key[a++] = num[1];
                                    } ;
                        if (RB5==0) { delay_ms_INT(50);            
                                      while(RB5==0) continue ;
                                      delay_ms_INT(20);
                                      LCD_PutChar('4') ; key[a++] = num[4];
                                    } ;
                        if (RB6==0) { delay_ms_INT(50);            
                                      while(RB6==0) continue ;
                                      delay_ms_INT(20);
                                      LCD_PutChar('7') ; key[a++] = num[7];
                                    } ;
  PORTB = 0b111111011 ; if (RB4==0) { delay_ms_INT(50);            
                                      while(RB4==0) continue ;
                                      delay_ms_INT(20);
                                      LCD_PutChar('2') ; key[a++] = num[2];
                                    } ;
                        if (RB5==0) { delay_ms_INT(50);            
                                      while(RB5==0) continue ;
                                      delay_ms_INT(20);
                                      LCD_PutChar('5') ; key[a++] = num[5];
                                    } ;
                        if (RB6==0) { delay_ms_INT(50);            
                                      while(RB6==0) continue ;
                                      delay_ms_INT(20);
                                      LCD_PutChar('8') ; key[a++] = num[8];
                                    } ;

}

#int_RB
void ngat_RB(void)
{ LCD_PutCmd(0x83); // dong1 cot 5
  while (OK && CANCEL) quetphim_hienthi(); // nut OK chua dc nhan

  if(CANCEL==0) {delay_ms_INT(20);            // nhan OK
                while(CANCEL==0) continue ;
                delay_ms_INT(20);
                goto THOAT ; };

  if(OK==0 ) {delay_ms_INT(20);            // nhan OK
              while(OK==0) continue ;
              delay_ms_INT(20);
              if (a==7)
                     { 
                        if(key[1]!=num_ss[1]) {goto HERE ;};
                        if(key[2]!=num_ss[2]) {goto HERE ;};
                        if(key[3]!=num_ss[3]) {goto HERE ;};
                        if(key[4]!=num_ss[4]) {goto HERE ;};
                        if(key[5]!=num_ss[5]) {goto HERE ;};
                        if(key[6]!=num_ss[6]) {goto HERE ;};
                        if(key[7]!=num_ss[7]) {goto HERE ;};
                        LCD_PutCmd(0x01);
                        LCD_PutCmd(0x84);
                        LCD_PutChar('Y') ;
                        LCD_PutChar('E') ;
                        LCD_PutChar('S') ;
                        delay_ms(2000) ;
                        
           HERE : 
                        LCD_PutCmd(0x01);
                        LCD_PutCmd(0x84);
                        LCD_PutChar('W') ;
                        LCD_PutChar('r') ;
                        LCD_PutChar('o') ;
                        LCD_PutChar('n') ;
                        LCD_PutChar('g') ;
                        LCD_PutChar('!') ;
                        b++;  // dem so lan sai ma
                      } ;
              if (a!=7){ LCD_PutCmd(0x01);
                          LCD_PutCmd(0x84);
                          LCD_PutChar('W') ;
                          LCD_PutChar('r') ;
                          LCD_PutChar('o') ;
                          LCD_PutChar('n') ;
                          LCD_PutChar('g') ;
                          LCD_PutChar('!') ;
                          b++; // dem so lan sai ma
                          delay_ms(2000) ;
                        } ;

              } ;
  THOAT :
  LCD_PutCmd(0x01);
  LCD_PutCmd(0xC4);
  LCD_PutChar('R') ;
  LCD_PutChar('e') ;
  LCD_PutChar('a') ;
  LCD_PutChar('d') ;
  LCD_PutChar('y') ;
  LCD_PutChar('!') ;

  RBIF = 0 ;
}


void main()
{ set_tris_A(0xFF); PORTA = 0xFF ;
  set_tris_B(0xF0); PORTB = 0xF0 ;
  set_tris_C(0x00); PORTC = 0;
  set_tris_D(0x00);

  LCD_Init(); // khoi tao LCD

  enable_interrupts(global);
  enable_interrupts(int_RB);
  ext_int_edge(H_to_L);

  LCD_PutCmd(0xC4);
  LCD_PutChar('R') ;
  LCD_PutChar('e') ;
  LCD_PutChar('a') ;
  LCD_PutChar('d') ;
  LCD_PutChar('y') ;
  LCD_PutChar('!') ;

  while(1) ;
}
cho em hỏi chút xíu !
- em khai báo biến đếm a toàn cục , như trên code
- ý định của em : mỗi lần nhấn phím thì mảng key[] sẽ tự nạp số vào & nạp theo thứ tự , vì vậy , sau mỗi lần nhấn phím , em cho key[a++]
- nhưng ko hiểu sao , cứ mỗi lần nhấn đúng mã số "3952610", thì ct cứ nhảy tới label HERE .. em nghi ngờ ngay chỗ so sánh mảng key[] và num_ss[] ;
- chắc chắn key[] có giá trị ko đúng như các số mình đã nhấn !
các anh thấy em phân tích vậy có đúng ko ?
- thứ nữa : sau mỗi lần chạy hết ct ngắt thì ko bit ct có thoát khỏi ngắt hay ko ? nhưng em nhấn phím trở lại thì LCD ko chịu hiện số nữa , chỉ khi nhấn từ 3 lần trở lên thì số mới hiện trở lại . LCD lúc ấy đang bận ? nhưng nó có làm gì đâu . Đến nhãn THOAT , chỉ việc hiện " Ready!" lên thui mà . Có lẽ tới chỗ này ct bị treo !?
Mr.Bi vẫn chưa có mặt trong diễn đàn