Không biết tại sao khi cho chạy code LCD chạy đúng nhưng khi nhấn nút Reset của 8051 thì chạy sai, phải nhấn nút 2 lần mới ra kq đúng.
code em mọi người xem thử
code em mọi người xem thử
Code:
#include <at89x52.h> #define rs P2_2 #define en P2_3 //LCD Data pins #define lcdport P2 void lcd_ini(); void lcd_cmd(unsigned char); void lcd_putc(unsigned char); void lcd_puts(char *string); void lcd_goto(unsigned char x, unsigned char y); void delay_ms(unsigned int ms) //ham delay_ms(10) {unsigned int i,j; for(i=0;i<ms;i++) for(j=0;j<121;j++); } void delay_us(unsigned int us) { unsigned int i; for(i=0;i<us;i++); } void main(void) { lcd_ini(); lcd_goto(2,3); lcd_puts("dientuvietnam"); while(1){ } } void lcd_ini() { delay_us(2000); lcd_cmd(0x28); delay_us(2000); lcd_cmd(0x0C); delay_us(2000); lcd_cmd(0x06); lcd_cmd(0x01); lcd_cmd(0x02); lcd_cmd(0x80); } void lcdcmd(unsigned char cmdout) { delay_ms(10); lcdport=cmdout; rs=0; en=1; delay_ms(10); en=0; lcdport=0; } void lcd_cmd(unsigned char cmd_value) { unsigned char cmd_value1; cmd_value1 = (cmd_value & 0xF0); lcdcmd(cmd_value1); / delay_us(100); cmd_value1 = ((cmd_value<<4) & 0xF0); lcdcmd(cmd_value1); delay_us(100); } void lcd_puts(unsigned char *s) { while (*s) { lcd_putc(*s); s++; } } void lcddata(unsigned char dataout) { delay_us(100); lcdport=dataout; //Send data to lcdport=PORTB rs=1; en=1; delay_us(100); en=0; } void lcd_putc(unsigned char data_value) { unsigned char data_value1; data_value1=(data_value&0xF0); lcddata(data_value1); data_value1=((data_value<<4)&0xF0); lcddata(data_value1); } void lcd_goto(int x, int y) { if((x<1||x>2)&&(y<1||y>16)) { x=1; y=1; } if(x == 1) lcd_cmd(0x7F+y); else lcd_cmd(0xBF+y); }
Comment