根據(jù)LCD1602顯示矩陣按鍵實驗硬件電路編寫程序,實現(xiàn)矩陣按鍵的輸入功能和液晶LCD1602顯示功能。上電后LCD1602第一行持續(xù)顯示“Pechin Science“字符,第二行第一位顯示被按下的按鍵編碼。
單片機源程序如下:
- #include<reg52.h>
- #include"lcd.h"
- #define GPIO_KEY P1
- unsigned char KeyValue;
- //用來存放讀取到的鍵值
- unsigned char KeyState;
- //用來存放按鍵狀態(tài)
- unsigned char PuZh[]=" Pechin Science ";
- unsigned char dat[]="0123456789ABCDEF";
- void Delay10ms(); //延時50us
- void KeyDown(); //檢測按鍵函數(shù)
-
- void main(void)
- {
- unsigned char i;
- LcdInit();
- KeyState=0;
- for(i=0;i<16;i++)
- {
- // LcdWriteCom(0x80);
- LcdWriteData(PuZh[i]);
- }
- while(1)
- {
- KeyDown();
- if(KeyState)
- {
- KeyState=0;
- LcdWriteCom(0x80+0x40);
- LcdWriteData(dat[KeyValue]);
- }
- }
- }
- /*******************************************************************************
- * 函 數(shù) 名 : KeyDown
- * 函數(shù)功能 : 檢測有按鍵按下并讀取鍵值
- * 輸 入 : 無
- * 輸 出 : 無
- *******************************************************************************/
- void KeyDown(void)
- {
- char a;
- GPIO_KEY=0x0f;
- if(GPIO_KEY!=0x0f)
- {
- Delay10ms();
- if(GPIO_KEY!=0x0f)
- {
- KeyState=1;
- //測試列
- GPIO_KEY=0X0F;
- // Delay10ms();
- switch(GPIO_KEY)
- {
- case(0X07): KeyValue=0;break;
- case(0X0b): KeyValue=1;break;
- case(0X0d): KeyValue=2;break;
- case(0X0e): KeyValue=3;break;
- // default: KeyValue=17; //檢測出錯回復(fù)17意思是把數(shù)碼管全滅掉。
- }
- //測試行
- GPIO_KEY=0XF0;
- Delay10ms();
- switch(GPIO_KEY)
- {
- case(0X70): KeyValue=KeyValue;break;
- case(0Xb0): KeyValue=KeyValue+4;break;
- case(0Xd0): KeyValue=KeyValue+8;break;
- case(0Xe0): KeyValue=KeyValue+12;break;
- // default: KeyValue=17;
- }
- while((a<50)&&(GPIO_KEY!=0xf0)) //檢測按鍵松手檢測
- {
- Delay10ms();
- a++;
- }
- a=0;
- }
- }
- }
- /*******************************************************************************
- * 函 數(shù) 名 : Delay10ms
- * 函數(shù)功能 : 延時函數(shù),延時10ms
- * 輸 入 : 無
- * 輸 出 : 無
- *******************************************************************************/
- void Delay10ms(void) //誤差 0us
- {
- unsigned char a,b,c;
- for(c=1;c>0;c--)
- for(b=38;b>0;b--)
- for(a=130;a>0;a--);
- }
復(fù)制代碼
所有資料51hei提供下載:
LCD.rar
(30.87 KB, 下載次數(shù): 44)
2019-12-7 19:21 上傳
點擊文件名下載附件
|