标题: 基于51单片机的LCD1602显示矩阵按键源程序 [打印本页]

作者: 123456789xy    时间: 2018-4-15 21:35
标题: 基于51单片机的LCD1602显示矩阵按键源程序
基于51单片机的LCD1602显示矩阵按键源程序(分享设计)

单片机源程序如下:
  1. /*******************************************************
  2. *效果:烧录如果显示不正常,重新上电即可。按下矩阵键盘显示对应按键号
  3. *
  4. *
  5. *
  6. *********************************************************/

  7. #include<reg51.h>       
  8. #include"lcd.h"

  9. #define GPIO_KEY P1

  10. unsigned char KeyValue;
  11. //用来存放读取到的键值
  12. unsigned char KeyState;
  13. //用来存放按键状态
  14. unsigned char PuZh[]=" Pechin Science ";
  15. unsigned char dat[]="0123456789ABCDEF";
  16. void Delay10ms();   //延时50us
  17. void KeyDown();                 //检测按键函数

  18. /*******************************************************************************
  19. * 函 数 名         : main
  20. * 函数功能                   : 主函数
  21. * 输    入         : 无
  22. * 输    出         : 无
  23. *******************************************************************************/
  24. void main(void)
  25. {
  26.         unsigned char i;
  27.         LcdInit();
  28.         KeyState=0;
  29.         for(i=0;i<16;i++)
  30.         {
  31. //                LcdWriteCom(0x80);
  32.                 LcdWriteData(PuZh[i]);       
  33.         }
  34.         while(1)
  35.         {
  36.                 KeyDown();
  37.                 if(KeyState)
  38.                 {
  39.                         KeyState=0;
  40.                         LcdWriteCom(0x80+0x40);
  41.                         LcdWriteData(dat[KeyValue]);
  42.                 }
  43.         }                               
  44. }
  45. /*******************************************************************************
  46. * 函 数 名         : KeyDown
  47. * 函数功能                   : 检测有按键按下并读取键值
  48. * 输    入         : 无
  49. * 输    出         : 无
  50. *******************************************************************************/
  51. void KeyDown(void)
  52. {
  53.         char a;
  54.         GPIO_KEY=0x0f;
  55.         if(GPIO_KEY!=0x0f)
  56.         {
  57.                 Delay10ms();
  58.                 if(GPIO_KEY!=0x0f)
  59.                 {
  60.                         KeyState=1;
  61.                         //测试列
  62.                         GPIO_KEY=0X0F;
  63. //                         Delay10ms();
  64.                         switch(GPIO_KEY)
  65.                         {
  66.                                 case(0X07):        KeyValue=0;break;
  67.                                 case(0X0b):        KeyValue=1;break;
  68.                                 case(0X0d): KeyValue=2;break;
  69.                                 case(0X0e):        KeyValue=3;break;
  70. //                                default:        KeyValue=17;        //检测出错回复17意思是把数码管全灭掉。
  71.                         }
  72.                         //测试行
  73.                         GPIO_KEY=0XF0;
  74.                         Delay10ms();
  75.                         switch(GPIO_KEY)
  76.                         {
  77.                                 case(0X70):        KeyValue=KeyValue;break;
  78.                                 case(0Xb0):        KeyValue=KeyValue+4;break;
  79.                                 case(0Xd0): KeyValue=KeyValue+8;break;
  80.                                 case(0Xe0):        KeyValue=KeyValue+12;break;
  81. //                                default:        KeyValue=17;
  82.                         }
  83.                         while((a<50)&&(GPIO_KEY!=0xf0))         //检测按键松手检测
  84.                         {
  85.                                 Delay10ms();
  86.                                 a++;
  87.                         }
  88.                         a=0;
  89.                 }
  90.         }
  91. }
  92. ……………………

  93. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
LCD1602显示矩阵按键键值.zip (11.73 KB, 下载次数: 92)







欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1