- #include<reg51.h>
- sbit RS=P2^0; //1602的数据/指令选择控制线
- sbit RW=P2^1; //1602的读写控制线
- sbit E=P2^2; //1602的使能控制线
- unsigned char code tab1[]=“welcome AT89S51!”;
- unsigned char code tab2[]=“LCD1602test..OK”;
- bit lcd1602_read_status();
- void lcd1602_write_cmd(unsigned char cmd);
- void lcd1602_write_dat(unsigned char dat);
- void lcd1602_init();
- void delay(unsigned int i);
- void delay(unsigned int i)
- {while(i--);}
- bit lcd1602_read_status() //读1602状态
- { unsigned char temp;
- RS=0; //选择输入指令
- RW=1; //选择读
- temp=P0; //把命令字送入P0
- delay(20);
- E=1; //写入时序,一个下降沿,将命令传达到LCD
- delay(20);
- E=0;
- return (bit)(temp&0x80);
- }
- void lcd1602_write_dat(unsigned char dat) //1602写数据函数
- {
- while(lcd1602_read_status()!=0);//读取1602状态
- E=0;//根据时序图使能清零
- RS=1; //选择数据寄存器
- RW=0; //选择写
- P0=dat; //把要显示的数据送入P0
- delay(20); //延时一小会儿,让1602准备接收数据
- E=1; //使能线电平变化,数据送入1602的8位数据口
- delay(20);
- E=0;
- }
- void lcd1602_write_cmd(unsigned char cmd) //1602写命令函数
- {
- while(lcd1602_read_status()!=0);//读取1602状态
- E=0;//根据时序图使能清零
- RS=0; //选择输入指令
- RW=0; //选择写
- P0=cmd; //把命令字送入P0
- delay(20); //延时一小会儿,让1602准备接收数据
- E=1; //写入时序,一个下降沿,将命令传达到LCD
- delay(20);
- E=0;
- }
- void lcd1602_init()/ // 液晶初始化
- { {
- E=0;//由时序图可知开始时使能端E为低电平
- lcd1602_write_cmd(0x38); //8位数据,显示2行,5*7点阵/字符
- lcd1602_write_cmd(0x0c); //开启显示屏,关光标,光标不闪烁
- lcd1602_write_cmd(0x01; );/ // 清屏
- lcd1602_write_cmd(0x06); //显示地址递增,即写一个数据后,显示位置右移一位
- delay(200);
- }
- void lcd1602_display()//液晶显示
- {
- unsigned char i,j;
- lcd1602_write_cmd(0x80+0x00); //第一行显示
- for(i=0;i<15;i++) //将table1[]中的数据依次写入1602显示
- {lcd1602_write_dat(tab1[i]);delay(20);}
- lcd1602_write_cmd(0x80+0x40); //第二行显示
- for(j=0;j<14;j++) //将table2[]中的数据依次写入1602显示
- {lcd1602_write_dat(tab2[j]);delay(20);}
- }
- void main()//主函数
- {
- lcd1602_init();
- lcd1602_display();
- while(1);//动态停机
- }
复制代码 |