找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2734|回复: 1
打印 上一主题 下一主题
收起左侧

4×4矩形键盘使用stm32源程序

[复制链接]
跳转到指定楼层
楼主

因为没有那么多LED灯,我用的诗stm32f103小系统的 所以就用一个灯来测试代码。整体代码附加在下面。
  1. #ifndef __KEY_H
  2. #define __KEY_H
  3. #include "sys.h"
  4. //#define KEY0 GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_2)
  5. //#define KEY1 GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_3)
  6. //#define KEY2 GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4)
  7. //#define KEY3 GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_5)


  8. #define HPin  GPIO_Pin_3|GPIO_Pin_2|GPIO_Pin_1|GPIO_Pin_0
  9. #define LPin  GPIO_Pin_7|GPIO_Pin_6|GPIO_Pin_5|GPIO_Pin_4
  10. //#define HRead();  GPIO_ReadInputData(GPIOD);
  11. //#define LRead();  GPIO_ReadInputData(GPIOD);
  12. //void KEY_Init(void);
  13. //u8 KEY_Scan(void);

  14. void HL_Init_HScan(void);
  15. void HL_Init_LScan(void);

  16. u8 HL_Scan(void);
  17. #endif
复制代码
  1. #include "key.h"
  2. #include "delay.h"
  3. //void KEY_Init(void)
  4. //{
  5. //   GPIO_InitTypeDef GPIO_InitStructure;
  6. //   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);
  7. //   GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;
  8. //   GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
  9. //   GPIO_Init(GPIOE,&GPIO_InitStructure);
  10. //}
  11. //u8 KEY_Scan(void)
  12. //{
  13. //    if((KEY0==0)||(KEY1==0)||(KEY2==0)||(KEY3==0))
  14. //        {
  15. //          delay_ms(20);
  16. //          if(KEY0==0){while(KEY0==0);return 1;}
  17. //          else if(KEY1==0){while(KEY1==0);return 2;}
  18. //          else if(KEY2==0){while(KEY2==0);return 3;}
  19. //          else if(KEY3==0){while(KEY3==0);return 4;}
  20. //        }
  21. //        return 0;
  22. //}
  23. //行置为输入 默认为1 列置为输出默认为0        扫描行
  24. void HL_Init_HScan(void)
  25. {
  26.      GPIO_InitTypeDef GPIO_InitStructure;
  27.          RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
  28.      GPIO_InitStructure.GPIO_Pin=HPin;
  29.          GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
  30.          GPIO_Init(GPIOA,&GPIO_InitStructure);

  31.          GPIO_InitStructure.GPIO_Pin=LPin;
  32.          GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  33.          GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  34.          GPIO_Init(GPIOA,&GPIO_InitStructure);
  35.          GPIO_ResetBits(GPIOA,LPin);
  36. }
  37. //行置为输出 默认为0 列置为输入默认为1        扫描列
  38. void HL_Init_LScan(void)
  39. {
  40.      GPIO_InitTypeDef GPIO_InitStructure;
  41.          RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
  42.      GPIO_InitStructure.GPIO_Pin=LPin;
  43.          GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
  44.          GPIO_Init(GPIOA,&GPIO_InitStructure);

  45.          GPIO_InitStructure.GPIO_Pin=HPin;
  46.          GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  47.          GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  48.          GPIO_Init(GPIOA,&GPIO_InitStructure);
  49.          GPIO_ResetBits(GPIOA,HPin);


  50. }
  51. u8 HL_Scan(void)
  52. {
  53.   u16 temp=0;
  54.         u8 key=0;
  55.         HL_Init_HScan();
  56.         temp=GPIO_ReadInputData(GPIOA);
  57.     temp&=0x000f; //得到低四位数据 检测4个2进制数中0的存在
  58.         if(temp==0x0e)//第一行有按键被按下
  59.         {
  60.           key=1        ;
  61.         }
  62.         else if(temp==0x0d)
  63.         {
  64.           key=2;
  65.         }
  66.         else if(temp==0x0b)
  67.         {
  68.           key=3;
  69.         }
  70.         else if(temp==0x07)
  71.         {
  72.           key=4;
  73.         }else key=0;

  74.    HL_Init_LScan();
  75.    temp=GPIO_ReadInputData(GPIOA);
  76.    temp&=0x00f0;//得到高四位 即列数据
  77.    if((temp==0xe0)&&(key!=0))//第一列被按下
  78.    {
  79.       key=(key-1)*4+1;
  80.    }else
  81.    if((temp==0xd0)&&(key!=0))
  82.    {
  83.       key=(key-1)*4+2;
  84.    }else
  85.    if((temp==0xb0)&&(key!=0))
  86.    {
  87.      key=(key-1)*4+3;
  88.    }else
  89.    if((temp==0x70)&&(key!=0))
  90.    {
  91.      key=(key-1)*4+4;
  92.    }
  93.    else key=0;
  94.   
  95.    return key;
  96. }
复制代码

键盘矩阵.7z

185.96 KB, 下载次数: 26, 下载积分: 黑币 -5

评分

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

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:421308 发表于 2019-10-10 16:54 | 只看该作者
文件都打不开
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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