- /*8位总线驱动LCD1602*/
- #include<reg52.h>
- #defineuchar unsigned char
- #defineuint unsigned int
- #include<intrins.h>//延时时钟周期时用到
- sbitweixuan=P2^7; //将两个锁存器关闭,防止数码管显示乱码
- sbitduanxuan=P2^6;
- //sbitLCD_RW=P2^7; //检查忙位
- sbitLCD_RS=P3^5;
- sbitLCD_EN=P3^4;
- ucharcode table[]="123456789ABCDEFG";
- ucharcode table1[]="123456789ABCDEFG";
- voiddelayxms(uint xms) //延时X毫秒
- {
- uinti,j;
- for(i=0;i<xms;i++)
- for(j=0;j<114;j++);
- }
- /* voidread_LCD_busy() //检查忙函数
- {
- LCD_RS=0;
- LCD_RW=1;
- IO7=1; //I/O口写1
- LCD_EN=1;
- while(IO7); //等待液晶写完
- LCD_EN=0;
- } */
- voiddelayxus(uint xus) //延时X微秒
- {
- uchari;
- for(i=0;i<xus;i++)
- {
- _nop_();
- }
- }
- voidwrite_LCD_CD(uchar cd,uchar CD) //cd=0写指令,cd=1写数据
- {
- //read_LCD_busy();//检查忙函数
- delayxus(20);//最低延时(一定要在开头有个延时)
- LCD_RS=cd;
- P0=CD;
- LCD_EN=1;
- LCD_EN=0;
- }
- voidLCD_init() //initialization,液晶初始化
- {
- write_LCD_CD(0,0x38); //显示模式设置(16*2显示,5*7点阵,4位数据接口)
- write_LCD_CD(0,0x06); // 写一个字符后地址指针加1
- write_LCD_CD(0,0x0c); //设置开显示,不显示光标
- write_LCD_CD(0,0x01); //显示清屏,数据指针清0(清屏需要一定时间)
- delayxus(80);//清屏时间
- }
- voidmain()
- {
- uchari;
- duanxuan=0;
- weixuan=0;
- LCD_EN=0;
- LCD_init();
- write_LCD_CD(0,0x80);//数据地址指针,第一行
- do
- {
- write_LCD_CD(1,table[ i]); //写数据,data是编译器关键词
- i++;
- }
- while(table[ i]!='\0');
- i=0;
- write_LCD_CD(0,0x80+0x40);//数据地址指针,第二行
- do
- {
- write_LCD_CD(1,table1[ i]); //写数据,data是编译器关键词
- i++;
- }
- while(table1[ i]!='\0'); //因为table是字符串
- delayxms(2000);
- //write_LCD_CD(0,0x01); //显示清屏,数据指针清0
- //delayxus(80);
- write_LCD_CD(0,0x0f);
- write_LCD_CD(0,0x01);
- write_LCD_CD(0,0x80);
- while(1);
- }
复制代码
|