基于51單片機的LCD1602顯示矩陣按鍵源程序(分享設計)
單片機源程序如下:
- /*******************************************************
- *效果:燒錄如果顯示不正常,重新上電即可。按下矩陣鍵盤顯示對應按鍵號
- *
- *
- *
- *********************************************************/
- #include<reg51.h>
- #include"lcd.h"
- #define GPIO_KEY P1
- unsigned char KeyValue;
- //用來存放讀取到的鍵值
- unsigned char KeyState;
- //用來存放按鍵狀態
- unsigned char PuZh[]=" Pechin Science ";
- unsigned char dat[]="0123456789ABCDEF";
- void Delay10ms(); //延時50us
- void KeyDown(); //檢測按鍵函數
-
- /*******************************************************************************
- * 函 數 名 : main
- * 函數功能 : 主函數
- * 輸 入 : 無
- * 輸 出 : 無
- *******************************************************************************/
- 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]);
- }
- }
- }
- /*******************************************************************************
- * 函 數 名 : KeyDown
- * 函數功能 : 檢測有按鍵按下并讀取鍵值
- * 輸 入 : 無
- * 輸 出 : 無
- *******************************************************************************/
- 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; //檢測出錯回復17意思是把數碼管全滅掉。
- }
- //測試行
- 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;
- }
- }
- }
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei提供下載:
LCD1602顯示矩陣按鍵鍵值.zip
(11.73 KB, 下載次數: 92)
2018-4-15 21:33 上傳
點擊文件名下載附件
基于51單片機的LCD1602顯示
|