PDA

View Full Version : giao tiếp Key - LCD & Flash nội ?


Mr.Bi
24-09-2007, 02:00 AM
http://i189.photobucket.com/albums/z78/NgCongMinhDN/machhoanchinh.jpg
http://i189.photobucket.com/albums/z78/NgCongMinhDN/sodothuattoan.jpg

#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'} ;
unsigned int8 num_ss[] ; // mang so sanh
unsigned int8 a = 0 ; // bien dem cac so nhap tu ban phim
unsigned int8 b = 1 ; // bien dem so lan sai ma
unsigned int8 d ; // bien ghi pass vao FLASH
unsigned int8 g = 1 ; // bien thong bao ghi pass vao FLASH trong lan dau tien
unsigned int8 h ; // bien luu lai tong cac so da ghi vao FLASH

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);
while (OK && CANCEL) quetphim_hienthi();

if(!CANCEL) goto THOAT ;


if(!OK) {delay_ms_INT(50);
while(!OK) continue ;
delay_ms_INT(20);
if(g == 1) { LCD_PutCmd(0x01);
LCD_PutCmd(0xC0);
LCD_PutChar('W') ;
LCD_PutChar('r') ;
LCD_PutChar('i') ;
LCD_PutChar('t') ;
LCD_PutChar('t') ;
LCD_PutChar('i') ;
LCD_PutChar('n') ;
LCD_PutChar('g') ;
LCD_PutChar(' ') ;
LCD_PutChar('F') ;
LCD_PutChar('L') ;
LCD_PutChar('A') ;
LCD_PutChar('S') ;
LCD_PutChar('H') ;
for (d=1;d<=a;d++)
{ write_eeprom(d,key[d]);
PORTC = 0x08 ;delay_ms(250);PORTC = 0x00 ;
};
g ++ ;
h = a ; goto THOAT ; // luu lai tong cac so da nhan & THOAT
};
if (a == h )
{ LCD_PutCmd(0x01);
LCD_PutCmd(0xC0);
LCD_PutChar('R') ;
LCD_PutChar('e') ;
LCD_PutChar('a') ;
LCD_PutChar('d') ;
LCD_PutChar('i') ;
LCD_PutChar('n') ;
LCD_PutChar('g') ;
LCD_PutChar(' ') ;

LCD_PutChar('F') ;
LCD_PutChar('L') ;
LCD_PutChar('A') ;
LCD_PutChar('S') ;
LCD_PutChar('H') ;
for (d=1;d<=a;d++)
{ num_ss[d]= read_eeprom(d);delay_ms(250);
if(key[d]!=num_ss[d]) goto HERE ;};

LCD_PutCmd(0x01);
LCD_PutCmd(0x84);
LCD_PutChar('D') ;
LCD_PutChar('U') ;
LCD_PutChar('N') ;
LCD_PutChar('G') ;
LCD_PutChar(' ') ;
LCD_PutChar('M') ;
LCD_PutChar('A') ;
PORTC = 0x08 ;
delay_ms(2000) ;
PORTC = 0x00 ;
b = 1 ;
goto THOAT ;

HERE :
LCD_PutCmd(0x01);
LCD_PutCmd(0x84);
LCD_PutChar('W') ;
LCD_PutChar('R') ;
LCD_PutChar('O') ;
LCD_PutChar('N') ;
LCD_PutChar('G') ;
LCD_PutChar('!') ;
delay_ms(1000) ;

} ;
if (a!=h ){ LCD_PutCmd(0x01);
LCD_PutCmd(0x84);
LCD_PutChar('S') ;
LCD_PutChar('A') ;
LCD_PutChar('I') ;
LCD_PutChar(' ') ;
LCD_PutChar('M') ;
LCD_PutChar('A') ;
delay_ms(1000) ;
} ;

} ;
b ++ ;
THOAT :
a = 0 ;
LCD_PutCmd(0x01);
LCD_PutCmd(0xC5);
LCD_PutChar('R') ;
LCD_PutChar('e') ;
LCD_PutChar('a') ;
LCD_PutChar('d') ;
LCD_PutChar('y') ;
LCD_PutChar('!') ;
if((b%4)==0) { PORTC = 0x04 ; delay_ms(1); PORTC = 0x00 ; b = 1 ; } ;
}


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

LCD_Init();

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

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

while(1){PORTB = 0xF0 ;} ;
}

Cuối cùng cũng làm xong đồ án môn học Lập trình hệ thống ! up lên cho mấy bác pro..bình loạn jup ...xem có thể tối ưu cho gọn & cho hay thêm đc ko ? Chương trình chạy tốt & chuẩn !

Mr.Bi
15-12-2007, 10:59 PM
hình như Pic ở Vietnam ta hành lởm hay sao ấy ! dùng Adapter , để mức 4,5v mà cháy Chip ... cụ thể là gắn detect PIC = Winpic thì unknown . Phải mua con khác .. hic 70k chứ có ít gì !

electronicltv
15-12-2007, 11:32 PM
hình như Pic ở Vietnam ta hành lởm hay sao ấy ! dùng Adapter , để mức 4,5v mà cháy Chip ... cụ thể là gắn detect PIC = Winpic thì unknown . Phải mua con khác .. hic 70k chứ có ít gì !

"hành lởm" hay "hàng lởm"? Nói thế khác nào vơ đũa cả nắm. Pic cũng là hàng hóa thì có thể có lỗi xảy ra là đương nhiên. Để 4.5V mà cháy chip thì có thể Pic bị lỗi nên hư và cũng có thể bạn làm sai hoặc chưa làm đúng :D. Ai dám chắc chắn rằng detect PIC= Winpic thì unknown là không phải lỗi của bạn ?????

Mr.Bi
16-12-2007, 11:40 PM
thật ra thì áp ko tải trong Adapter ko đúng đâu bạn ah ! 4,5V = 6V đấy ! vả lại nó ko đc ổn áp .. mình quá chủ quan ! chạy thử ct giúp thằng bạn ! tiêu con PIC của mình ... khốn khổ wa đi ! bài học nhớ đời !

tata86
30-05-2008, 11:11 AM
đây ko fải là ghi vào flash mà là ghi vào eeprom Mr.Bi xem lại đi nhé