51單片機(jī)——LCD1602
1、1602液晶讀寫時(shí)序
(1)、讀狀態(tài)
RS=L,R/W=H,E=H。(判斷忙完畢后釋放總線)
(2)、讀數(shù)據(jù)
RS=H,R/W=H,E=H。
(3)、寫指令
RS=L,R/W=L,D0~D7=指令碼,E=高脈沖
(4)、寫數(shù)據(jù)
RS=H,R/W=L,D0~D7=數(shù)據(jù),E=高脈沖
2、液晶顯示驅(qū)動(dòng)文件
#include
#define LCD1602_DB P0
sbit LCD1602_RS = P1 ^ 0;
sbit LCD1602_RW = P1 ^ 1;
sbit LCD1602_E = P1 ^ 5;
/* 等待液晶準(zhǔn)備好 */
void LcdWaitReady()
{
unsigned char sta;
LCD1602_DB = 0xFF;
LCD1602_RS = 0;
LCD1602_RW = 1;
do
{
LCD1602_E = 1;
sta = LCD1602_DB; //讀取狀態(tài)字
LCD1602_E = 0;
}
while (sta & 0x80); //bit7等于1表示液晶正忙,重復(fù)檢測(cè)直到其等于0為止
}
/* 向LCD1602液晶寫入一字節(jié)命令,cmd-待寫入命令值 */
void LcdWriteCmd(unsigned char cmd)
{
LcdWaitReady();
LCD1602_RS = 0;
LCD1602_RW = 0;
LCD1602_DB = cmd;
LCD1602_E = 1;
LCD1602_E = 0;
}
/* 向LCD1602液晶寫入一字節(jié)數(shù)據(jù),dat-待寫入數(shù)據(jù)值 */
void LcdWriteDat(unsigned char dat)
{
LcdWaitReady();
LCD1602_RS = 1;
LCD1602_RW = 0;
LCD1602_DB = dat;
LCD1602_E = 1;
LCD1602_E = 0;
}
/* 設(shè)置顯示RAM起始地址,亦即光標(biāo)位置,(x,y)-對(duì)應(yīng)屏幕上的字符坐標(biāo) */
void LcdSetCursor(unsigned char x, unsigned char y)
{
unsigned char addr;
if (y == 0) //由輸入的屏幕坐標(biāo)計(jì)算顯示RAM的地址
{
addr = 0x00 + x; //第一行字符地址從0x00起始
}
else
{
addr = 0x40 + x; //第二行字符地址從0x40起始
}
LcdWriteCmd(addr | 0x80); //設(shè)置RAM地址
}
/* 在液晶上顯示字符串,(x,y)-對(duì)應(yīng)屏幕上的起始坐標(biāo),str-字符串指針 */
void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str)
{
LcdSetCursor(x, y); //設(shè)置起始地址
while (*str != '\0') //連續(xù)寫入字符串?dāng)?shù)據(jù),直到檢測(cè)到結(jié)束符
{
LcdWriteDat(*str++);
}
}
/* 初始化1602液晶 */
void InitLcd1602()
{
LcdWriteCmd(0x38); //16*2顯示,5*7點(diǎn)陣,8位數(shù)據(jù)接口
LcdWriteCmd(0x0C); //顯示器開,光標(biāo)關(guān)閉
LcdWriteCmd(0x06); //文字不動(dòng),地址自動(dòng)+1
LcdWriteCmd(0x01); //清屏
}