参考程序
#endif void LcdWaitReady(); //读取“忙”表示,等待液晶准备好 void LcdWriteCmd(unsigned char cmd);//给液晶发送命令cmd void LcdWriteDat(unsigned char dat); //写入数据函数 void LcdClearScreen();//液晶清屏 void LcdInit(); //液晶初始化函数 void CursorPos(unsigned char row,unsignedchar col);//光标定位 void ShowNum(unsigned char row,unsignedchar col,unsigned char Num);//显示3位数 void LcdShowStr(unsigned char row, unsignedchar col,unsigned char code *str); //显示字符串 #endif /*************************************** Lcd1602.c ***************************************/ #define _LCD1602_C #include"config.h" #include"Lcd1602.h" /******************************************** 函数功能:判断液晶模块忙碌状态。忙,等待。 入口参数:无。 ********************************************/ void LcdWaitReady() //读取“忙”表示,等待液晶准备好 { unsigned char sta; LCD1602_DB = 0xFF; LCD1602_RS = 0; LCD1602_RW = 1; do// do...while 循环语句 { LCD1602_E= 1; sta= LCD1602_DB; //读取状态字 LCD1602_E= 0; //读完了要关闭使能,防止液晶输出数据干扰总线 }while (sta & 0x80); //bit7 等于1 表示液晶正忙,重复检测直到其等于0 为止 } /************************************************ 函数功能:给液晶发送命令 入口参数:cmd **************************************************/ void LcdWriteCmd(unsigned char cmd) { LcdWaitReady(); LCD1602_RS = 0; LCD1602_RW = 0; LCD1602_DB = cmd; LCD1602_E = 1; LCD1602_E = 0; } /************************************************ 函数功能:给液晶发送数据 入口参数:dat **************************************************/ void LcdWriteDat(unsigned char dat) //写入数据函数 { LcdWaitReady(); LCD1602_RS = 1; LCD1602_RW = 0; LCD1602_DB = dat; LCD1602_E = 1; LCD1602_E = 0; } /************************************************ 函数功能:清屏 入口参数:无 **************************************************/ void LcdClearScreen() { LcdWriteCmd(0x01); } /************************************************ 函数功能:液晶初始化 入口参数:无 **************************************************/ void LcdInit() //液晶初始化函数 { LcdWriteCmd(0x38); //16*2 显示,5*7 点阵,8 位数据接口 LcdWriteCmd(0x0C); //显示器开,光标关闭 LcdWriteCmd(0x06); //文字不动,地址自动加1 LcdWriteCmd(0x01); //清屏 }
/************************************************ 函数功能:定位光标 入口参数:row行位置,col列位置 **************************************************/ void CursorPos(unsigned char row,unsignedchar col) { row%=2; col%=40; //防止越界 if(row)LcdWriteCmd(0xC0+col); //第2行 elseLcdWriteCmd(0x80+col); //第1行 } /************************************************ 函数功能:显示3位数 入口参数:row行位置,col列位置,Num显示数据 **************************************************/ void ShowNum(unsigned char row,unsignedchar col,unsigned char Num) { row%=2; col%=40; //防止越界 LcdWriteCmd(0x80+row*0x40+col);//光标定位 if(Num<10) { LcdWriteDat(''); //百位 LcdWriteDat('');//十位 LcdWriteDat(Num%10+'0');//个位 } elseif(Num<100) { LcdWriteDat(''); //百位 LcdWriteDat(Num/10%10+'0'); //十位 LcdWriteDat(Num%10+'0'); //个位 } else { LcdWriteDat(Num/100+'0'); //百位 LcdWriteDat(Num/10%10+'0'); //十位 LcdWriteDat(Num%10+'0'); //个位 }
} /************************************************ 函数功能:在给定位置显示字符串 入口参数:row行位置,col列位置,str字符串 **************************************************/ void LcdShowStr(unsigned char row, unsignedchar col, unsigned char code *str) //显示字符 { uchari=0; row%=2;col%=40; //防止越界 LcdWriteCmd(0x80+row*0x40+col);//光标定位 for(;col<40&&str[i]!=0;i++,col++){LcdWriteDat(str[i]);} |
Powered by Discuz! X3.4
© 2001-2023 Discuz! Team.