找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3142|回复: 1
收起左侧

LCD1602显示矩阵按键键值源程序

[复制链接]
ID:658384 发表于 2019-12-7 19:21 | 显示全部楼层 |阅读模式
根据LCD1602显示矩阵按键实验硬件电路编写程序,实现矩阵按键的输入功能和液晶LCD1602显示功能。上电后LCD1602第一行持续显示“Pechin Science“字符,第二行第一位显示被按下的按键编码。

单片机源程序如下:
  1. #include<reg52.h>       
  2. #include"lcd.h"

  3. #define GPIO_KEY P1

  4. unsigned char KeyValue;
  5. //用来存放读取到的键值
  6. unsigned char KeyState;
  7. //用来存放按键状态
  8. unsigned char PuZh[]=" Pechin Science ";
  9. unsigned char dat[]="0123456789ABCDEF";
  10. void Delay10ms();   //延时50us
  11. void KeyDown();                 //检测按键函数


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

所有资料51hei提供下载:
LCD.rar (30.87 KB, 下载次数: 44)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:802962 发表于 2020-10-26 11:34 | 显示全部楼层
按键显示是乱码啊
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表