/* LCD显示相关 */#define DATA1/* LCD写数据时为1 */#define COM0/* LCD写命令时为0 */#define LINE10b10000000#define LINE20b11000000#define LCD_ERD6#define LCD_RWRD5#define LCD_RSRD4void LCD_WRITE_4( unsigned char R1, unsigned char FLAG );void LCD_WRITE( unsigned char R1, unsigned char FLAG );unsigned char LCD_READ( void );void LCD_BUSY( void );void LcdWriteChar( unsigned char x, unsigned char y, char s );void lcd_dispaly_str( unsigned char x, unsigned char y, char *s );void LCD_CSH( void );/* LCD写4位命令 */void LCD_WRITE_4( unsigned char R1, unsigned char FLAG ){LCD_RW= 0; NOP();LCD_RS= FLAG;PORTD&= 0xF0;LCD_E= 1; NOP();PORTD|= R1;NOP(); NOP();LCD_E= 0; NOP();LCD_RS= 0; NOP();PORTD&= 0xF0;}/* LCD写命令 */void LCD_WRITE( unsigned char R1, unsigned char FLAG ){unsigned char R2;LCD_BUSY();LCD_RW= 0; NOP();LCD_RS= FLAG;R2= (R1 >> 4) & 0x0F;PORTD&= 0xF0; NOP();LCD_E= 1;PORTD|= R2;NOP(); NOP();LCD_E = 0;R2= R1 & 0x0F;PORTD&= 0xF0; NOP();LCD_E= 1; NOP();PORTD|= R2;NOP(); NOP();LCD_E= 0; NOP();LCD_RS= 0; NOP();PORTD&= 0xF0;}/* 读LCD状态 */unsigned char LCD_READ( void ){unsigned char R1;TRISD= 0x0F;LCD_RS= 0; NOP();LCD_RW= 1;NOP(); NOP();LCD_E = 1;NOP(); NOP();NOP(); NOP();R1= 0;R1= (PORTD << 4) & 0xF0;LCD_E= 0;NOP(); NOP();NOP(); NOP();LCD_E = 1;NOP(); NOP();R1|= (PORTD & 0x0F);LCD_E= 0; NOP();LCD_RW= 0;TRISD= 0x00;return(R1);}/* 检测LCD是否忙 */void LCD_BUSY( void ){unsigned char R1;do{R1 = LCD_READ();}while ( (R1 & 0x80) == 0x80 );}/* 写字符 */void LcdWriteChar( unsigned char x, unsigned char y, char s ){if ( y == 0 ){LCD_WRITE( 0x80 + x, COM );}else {LCD_WRITE( 0xC0 + x, COM );}LCD_WRITE( s, DATA );}/* 写字符串 */void lcd_dispaly_str( unsigned char x, unsigned char y, char *s ){if ( y == 0 ){LCD_WRITE( 0x80 + x, COM );}else {LCD_WRITE( 0xC0 + x, COM );}while ( *s ){LCD_WRITE( *s, DATA );s++;}}/* LCD初始化 */void LCD_CSH( void ){TRISD = 0x00; /* RD方向输出 */__delay_ms( 20 );LCD_WRITE_4( 0b0011, COM );__delay_ms( 5 );LCD_WRITE_4( 0b0011, COM );__delay_ms( 10 );LCD_WRITE_4( 0b0011, COM );__delay_ms( 10 );LCD_WRITE_4( 0b0010, COM );LCD_BUSY();LCD_WRITE( 0b00101000, COM );LCD_WRITE( 0b00001100, COM );LCD_WRITE( 0b00000001, COM );__delay_ms( 2 );LCD_WRITE( 0b00000110, COM );}/* 游标闪烁 */void SetCurPosition( unsigned char x, unsigned char y ){unsigned char temp;temp= x & 0x0f;y&= 0x01;if ( y )temp |= 0x40; /* 如果在第2行 */temp |= 0x80;LCD_WRITE( temp, COM );LCD_WRITE( 0x0f, COM );}/* 关游标 */void LCD_DELET_CUR_P( void ){LCD_WRITE( 0x0c, COM );}
使用:
LCD_CSH();lcd_dispaly_str(0,0,"dasdasas");