51單片機(jī)系列之LCD1602
名稱:LCD1602液晶屏顯示(并口)
平臺(tái):Keil 4, Ly-51S學(xué)習(xí)板
引腳定義如下:1-VSS 2-VDD 3-V0 4-RS 5-R/W 6-E 7-14 DB0-DB7 15-BLA 16-BLK
與51連接:RS-P2.4 RW-P2.5 EN-P2.6 DB-P0
-----------------------------------------------------*/
#include
#define DB P0
sbit RS =P2^4;
sbit RW =P2^5;
sbit EN =P2^6;
/*判忙函數(shù)*/
bit LCD_check_busy() {
DB = 0xFF;
RS = 0;
RW = 1;
EN = 0;
EN = 1;
return (bit)(DB & 0x80);
}
/*寫入命令函數(shù)*/
void LCD_write_com(unsigned char com) {
while(LCD_check_busy());//忙則等待
RS = 0;
RW = 0;
EN = 1;
DB = com;
EN = 0;
}
/*寫入數(shù)據(jù)函數(shù)*/
void LCD_write_data(unsigned char dat) {
while(LCD_check_busy());//忙則等待
RS = 1;
RW = 0;
EN = 1;
DB = dat;
EN = 0;
}
/*在y(0-1)行,x列(0-15)寫入字符串函數(shù)*/
void LCD_write_string(unsigned char x, unsigned char y, unsigned char *s) {
if(0==y) LCD_write_com(0x80 + x);
else LCD_write_com(0xC0 + x);
while(*s) {
LCD_write_data(*s);
s ++;
}
}
/*寫入字符函數(shù)*/
void LCD_write_char(unsigned char x, unsigned char y, unsigned char dat) {
if(0==y) LCD_write_com(0x80 + x);
else LCD_write_com(0xC0 + x);
LCD_write_data(dat);
}
/*清屏函數(shù)*/
void LCD_clear() {
LCD_write_com(0x01);
}
/*初始化函數(shù)*/
void LCD_init() {
LCD_write_com(0x38);/*顯示模式設(shè)置*/
LCD_write_com(0x08);/*顯示關(guān)閉*/
LCD_write_com(0x01);/*顯示清屏*/
LCD_write_com(0x06);/*顯示光標(biāo)移動(dòng)設(shè)置*/
LCD_write_com(0x0C);/*顯示開及光標(biāo)設(shè)置*/
}
/*主函數(shù)*/
void main() {
while(1) {
LCD_init();
LCD_write_char(0,0,'*');
LCD_write_string(1,0,"I'm Chinese.**");
LCD_write_string(0,1,"Happy,hellofuxin");
LCD_write_char(15,0,'!');
while(1);
}
}