|
實現功能:LCD1602的第一行和第二行可以顯示不超過16字符的內容,幫助初學者大致了解LCD1602的運作原理;
51hei.png (54.53 KB, 下載次數: 90)
下載附件
2021-2-27 15:04 上傳
- //LCD1602顯示時分秒
- #include <reg52.h>
- #define uint unsigned int
- #define uchar unsigned char
- sbit rs=P2^6;
- sbit rw=P2^5;
- sbit en=P2^7;
- uchar code Wenhou[]="NinHaoWangHaiJie";//第一行顯示內容;
- uchar code table[]="QQ:248xx9282"; //1602第二行顯示內容,
- void write_command(uchar); //聲明寫指令
- void write_date(uchar); //聲明寫數據
- uchar a,b;
- void delay(uint x) //延時函數
- {
- uint i,j;
- for(i=x;i>0;i--)
- for(j=110;j>0;j--);
- }
- void init()
- {
- en=0;
- rs=1;
- rw=1;
- P0=0xff;
- }
- /*----------------------------------------------------------------------------------------*/
- void main()
- {
- init(); //初始化
- while(1) // 循環內 不斷掃描
- {delay(2000);
- write_command(0x38);//設置16*2顯示 7*5點陣 8位數據口 //0011 1000
- //write_command(0x06);//地址加1 光標右移 //0000 0110
- //write_command(0x08);//只開顯示 //0000 1000
- //write_command(0x01);//清屏 //0000 0001
- write_command(0x0c);//開顯示 不開光標
-
- write_command(0x80);//第一行第一位地址 //0000 1100
- for(b=0;b<16;b++)
- write_date(Wenhou[b]);
- write_command(0xc0);//第二行第一位地址 //1000 0000
- for(a=0;a<12;a++)
- write_date(table[a]);
- delay(5000);
- write_command(0x02);
- }
- }
- void write_command(uchar com) //1602 寫指令
- {
- en=0;
- rs=0;
- rw=0;
- P0=com;
- delay(5);
- en=1;
- delay(5);
- en=0;
- }
- void write_date(uchar date)//1602 寫數據
- {
- en=0;
- rs=1;
- rw=0;
- P0=date;
- delay(5);
- en=1;
- delay(5);
- en=0;
- }
復制代碼
|
|