您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > lcd1602显示及测试程序
本程序共分为三个文件:Main.cLcd1602.cLcd1602.h/************文件Main.c*************/#includereg52.h#includeLcd1602.h#defineucharunsignedchar#defineuintunsignedintvoiddelay_ms(uintx){uinti,j;for(i=0;ix;i++)for(j=0;j120;j++);}voidmain(){Lcd_init();Write_char(0,10,'a');Write_string(1,0,bb);delay_ms(1000);Clear_line(0);Write_string(0,0,clear0lineOK!);delay_ms(1000);Clear_line(1);Write_string(1,0,clear1lineOK!);delay_ms(1000);Clear_all();Write_string(0,1,ClearallOk!);while(1);}/********文件Lcd1602.c**************/#includereg52.h#includeintrins.h#defineucharunsignedchar#defineuintunsignedint#defineNop_nop_()#defineLcd_dataP0/*数据口*/#definebusy0x80/*检测忙状态*/sbitrs=P2^4;sbitrw=P2^5;sbiten=P2^6;voiddelay(ucharx){uchari,j;for(i=0;ix;i++)for(j=0;j125;j++);}/*写命令*/voidWrite_com(ucharcom){rs=0;delay(5);rw=0;delay(5);Lcd_data=com;delay(5);en=0;delay(10);en=1;delay(10);}/*写数据*/voidWrite_data(uchardat){rs=1;delay(5);rw=0;delay(5);Lcd_data=dat;delay(10);en=0;delay(5);en=1;delay(10);}/*初始化*/voidLcd_init(){Lcd_data=0x00;delay(15);Write_com(0x38);delay(5);Write_com(0x38);delay(5);Write_com(0x38);delay(5);Write_com(0x38);/*显示模式设置*/Write_com(0x08);/*显示关闭*/Write_com(0x01);/*显示清屏*/Write_com(0x06);/*显示光标移动设置*/Write_com(0x0c);/*显示开及光标设置*/}voidWrite_char(ucharx,uchary,uchardat){x&=0x1;/*限制x不能大于1*/y&=0xf;/*限制y不能大于15*/if(x)y|=0x40;/*如果y大于1,则在第二行显示*/y|=0x80;Write_com(y);Write_data(dat);}voidWrite_string(ucharx,uchary,uchar*str){ucharlength=0;x&=0x1;/*限制x不能大于1*/y&=0xf;/*限制y不能大于15*/while(*str!='\0'){Write_char(x,y,*str);y++;str++;}}voidClear_line(ucharline){Write_string(line,0,);switch(line){case0:Write_com(0x80);case1:Write_com(0x80+0x40);default:break;}}voidClear_all(){Write_com(0x01);Write_com(0x80);}/**********文件Lcd1602.h**************/#defineucharunsignedchar#defineuintunsignedint#ifndefLcd1602_h#defineLcd1602_hexternLcd_init();externWrite_data(uchardat);externWrite_char(ucharx,uchary,uchardat);externWrite_string(ucharx,uchary,uchar*dat);externClear_line(ucharline);externClear_all();#endif已经测试通过,由于百度文库不能上传压缩包,故分三个文件。。。如有错误欢迎指出547886405@qq.com^_^
本文标题:lcd1602显示及测试程序
链接地址:https://www.777doc.com/doc-4640504 .html