#include
#define LCD1602_DB P0
typedef unsigned char uchar;
typedef unsigned int uint;
typedef unsigned long ulong;
sbit LCD1602_RS = P1^0;
sbit LCD1602_RW = P1^1;
sbit LCD1602_E = P1^5;
void initLCD1602();
void LCDShowStr(uchar i, uchar j, uchar * pCh);
void LCDWaitRdy();
void LCDWrCmd(uchar cmd);
void LCDWrData(uchar dat);
void LCDSetCursor(uchar i, uchar j);
void main() {
initLCD1602();
LCDShowStr(0, 1, "Hello world !");
LCDShowStr(1, 1, "Chasing Waves");
while (1) ;
}
void LCDWaitRdy() {
uchar state;
LCD1602_DB = 0xFF; //使数据总线处于高电平
LCD1602_RS = 0; //选择命令模式
LCD1602_RW = 1; //选择读
do {
LCD1602_E = 1; //开启液晶读写使能
state = LCD1602_DB; //从数据总线上读取状态字
LCD1602_E = 0; //关闭液晶读写使能
} while (state & 0x80); //重复直到bit7 == 0为止(不忙了)
}
void LCDWrCmd(uchar cmd) {
LCDWaitRdy();
LCD1602_RS = 0;
LCD1602_RW = 0;
LCD1602_DB = cmd;
LCD1602_E = 1;
LCD1602_E = 0;
}
void LCDWrData(uchar dat) {
LCDWaitRdy();
LCD1602_RS = 1;
LCD1602_RW = 0;
LCD1602_DB = dat;
LCD1602_E = 1;
LCD1602_E = 0;
}
void LCDSetCursor(uchar i, uchar j) {
uchar addr = (i == 0 ? j : 0x40 + j);
LCDWrCmd(addr | 0x80);
}
void LCDShowStr(uchar i, uchar j, uchar * pch) {
LCDSetCursor(i, j);
while (*pch != '\0') {
LCDWrData(*pch++);
}
}
void initLCD1602() {
LCDWrCmd(0x38); //16 * 2显示,5 * 7点阵,8位数据接口
LCDWrCmd(0x0C); //显示器开,光标关闭
LCDWrCmd(0x06); //文字不动,地址自动加1
LCDWrCmd(0x01); //清屏
}
上一篇:51单片机 带修改错误功能的计算器
下一篇:51单片机 UART串口通信
推荐阅读最新更新时间:2024-03-16 15:45