|
單片機(jī)源程序如下:- #include <reg51.h>
- // 對(duì)LCD1602的底層以及高層時(shí)序做封裝
- // IO接口定義
- #define LCD1602_DB P0 //data bus 數(shù)據(jù)總線
- // 控制總線
- sbit LCD1602_RS = P2^6;
- sbit LCD1602_RW = P2^5;
- sbit LCD1602_EN = P2^7;
- /************ 低層時(shí)序 ********************************/
- void Read_Busy() //忙檢測(cè)函數(shù),判斷bit7是0,允許執(zhí)行;1禁止
- {
- unsigned char sta; //
- LCD1602_DB = 0xff;
- LCD1602_RS = 0;
- LCD1602_RW = 1;
- do
- {
- LCD1602_EN = 1;
- sta = LCD1602_DB;
- LCD1602_EN = 0; //使能,用完就拉低,釋放總線
- }while(sta & 0x80);
- }
- void Lcd1602_Write_Cmd(unsigned char cmd) //寫(xiě)命令
- {
- Read_Busy();
- LCD1602_RS = 0;
- LCD1602_RW = 0;
- LCD1602_DB = cmd;
- LCD1602_EN = 1;
- LCD1602_EN = 0;
- }
- void Lcd1602_Write_Data(unsigned char dat) //寫(xiě)數(shù)據(jù)
- {
- Read_Busy();
- LCD1602_RS = 1;
- LCD1602_RW = 0;
- LCD1602_DB = dat;
- LCD1602_EN = 1;
- LCD1602_EN = 0;
- }
- /************* 高層時(shí)序 ******************************/
- // 本函數(shù)用來(lái)設(shè)置當(dāng)前光標(biāo)位置,其實(shí)就是設(shè)置當(dāng)前正在編輯的位置,
- // 其實(shí)就是內(nèi)部的數(shù)據(jù)地址指針,其實(shí)就是RAM顯存的偏移量
- // x范圍是0-15,y=0表示上面一行,y=1表示下面一行
- void LcdSetCursor(unsigned char x,unsigned char y) //坐標(biāo)顯示
- {
- unsigned char addr;
- if(y == 0)
- addr = 0x00 + x;
- else
- addr = 0x40 + x;
-
- Lcd1602_Write_Cmd(addr|0x80);
- }
- // 函數(shù)功能是:從坐標(biāo)(x,y)開(kāi)始顯示字符串str
- // 注意這個(gè)函數(shù)不能跨行顯示,因?yàn)轱@存地址是不連續(xù)的
- // 其實(shí)我們可以封裝出一個(gè)能夠折行顯示的函數(shù)的
- void LcdShowStr(unsigned char x,unsigned char y,unsigned char *str) //顯示字符串
- {
- LcdSetCursor(x,y); //當(dāng)前字符的坐標(biāo)
- while(*str != '\0')
- {
- Lcd1602_Write_Data(*str++);
- }
- }
- // 初始化LCD,使之能夠開(kāi)始正常工作
- void InitLcd1602() //1602初始化
- {
- Lcd1602_Write_Cmd(0x38); //打開(kāi),5*8,8位數(shù)據(jù)
- //Lcd1602_Write_Cmd(0x0c); // 打開(kāi)顯示并且無(wú)光標(biāo)
- Lcd1602_Write_Cmd(0x0f); // 打開(kāi)顯示并且光標(biāo)閃爍
- Lcd1602_Write_Cmd(0x06);
- Lcd1602_Write_Cmd(0x01); //清屏
- }
復(fù)制代碼
51hei.png (4.45 KB, 下載次數(shù): 50)
下載附件
2021-12-9 04:08 上傳
Keil代碼下載:
lcd1602.zip
(9.43 KB, 下載次數(shù): 17)
2021-12-6 20:37 上傳
點(diǎn)擊文件名下載附件
具體程序文件 下載積分: 黑幣 -5
|
評(píng)分
-
查看全部評(píng)分
|