找回密码
 立即注册

QQ登录

只需一步,快速开始

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

stm32的4*4矩阵扫描按键驱动代码

[复制链接]
跳转到指定楼层
楼主
ID:225232 发表于 2017-8-7 08:14 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
矩阵扫描,没有按键按下一直返回0 ,使用时最最好加判断和延时,不然太快了看不到
全部资料下载地址:
KEY.zip (2.61 KB, 下载次数: 68)

stm32单片机源程序:
  1. #include "stm32f10x.h"
  2. #include "key.h"
  3. #include "sys.h"
  4. #include "delay.h"
  5. //////////////////////////////////////////////////////////////////////////////////         
  6. //本程序只供学习使用,未经作者许可,不得用于其它任何用途
  7. //ALIENTEK战舰STM32开发板
  8. //按键驱动代码          
  9. //版本:V1.0
  10. //版权所有,盗版必究。
  11. //Copyright(C) 广州市星翼电子科技有限公司 2009-2019
  12. //All rights reserved                                                                          
  13. //////////////////////////////////////////////////////////////////////////////////  

  14. /* 定义相关变量*/
  15. #define GPIO_Pin_R         (GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7)
  16. #define GPIO_Pin_L    (GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3)
  17. ErrorStatus HSEStartUpStatus;

  18. u16 const Key_Tab[4][4]=//键盘编码表
  19. {
  20.   {1,2,3,10},
  21.   {4,5,6,11},
  22.   {7,8,9,12},
  23.   {13,14,15,16}
  24. };

  25. /******************************************************************************************
  26. name:void Delay_nus(u32 n)
  27. function:延时Nus
  28. parameter:n                                  
  29. return:无
  30. *******************************************************************************************/
  31. void Delay_nus(u32 n)
  32. {
  33.   u32 j;
  34. while(n--)
  35. { j=8;
  36.    while(j--);
  37. }
  38. }
  39. /******************************************************************************************
  40. name:void Delay_nms(u32 n)
  41. function:延时Nms
  42. parameter:n                                  
  43. return:无
  44. *******************************************************************************************/
  45. void Delay_nms(u32 n)
  46. {
  47. while(n--)
  48.     Delay_nus(1100);
  49. }
  50.        
  51. //按键初始化函数
  52. void KEY_Init(void) //IO初始化
  53. {
  54.         GPIO_InitTypeDef GPIO_InitStructure;

  55.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOE,ENABLE);//使能PORTA,PORTE时钟

  56.         GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4;//KEY0-KEY2
  57.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //设置成上拉输入
  58.         GPIO_Init(GPIOE, &GPIO_InitStructure);//初始化GPIOE2,3,4

  59.         //初始化 WK_UP-->GPIOA.0          下拉输入
  60.         GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_0;
  61.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PA0设置成输入,默认下拉          
  62.         GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.0

  63. }
  64. //按键处理函数
  65. //返回按键值
  66. //mode:0,不支持连续按;1,支持连续按;
  67. //0,没有任何按键按下
  68. //1,KEY0按下
  69. //2,KEY1按下
  70. //3,KEY2按下
  71. //4,KEY3按下 WK_UP
  72. //注意此函数有响应优先级,KEY0>KEY1>KEY2>KEY3!!
  73. u8 KEY_Scan(u8 mode)
  74. {         
  75.         static u8 key_up=1;//按键按松开标志
  76.         if(mode)key_up=1;  //支持连按                  
  77.         if(key_up&&(KEY0==0||KEY1==0||KEY2==0||WK_UP==1))
  78.         {
  79.                 delay_ms(10);//去抖动
  80.                 key_up=0;
  81.                 if(KEY0==0)return KEY0_PRES;
  82.                 else if(KEY1==0)return KEY1_PRES;
  83.                 else if(KEY2==0)return KEY2_PRES;
  84.                 else if(WK_UP==1)return WKUP_PRES;
  85.         }else if(KEY0==1&&KEY1==1&&KEY2==1&&WK_UP==0)key_up=1;             
  86.         return 0;// 无按键按下
  87. }



  88. u16 Get_KeyValue(void)//使用PF0~PF7
  89. {
  90.         GPIO_InitTypeDef GPIO_InitStructure;
  91.   
  92.   u8 i=5,j=5;
  93.   u16 temp1,temp2;
  94.   
  95.    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE);
  96.   GPIO_DeInit(GPIOF);
  97.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;                           
  98.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  99.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  100.   GPIO_Init(GPIOF, &GPIO_InitStructure);
  101.   
  102.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  103.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  104.   GPIO_Init(GPIOF, &GPIO_InitStructure);
  105.   
  106.   GPIO_ResetBits(GPIOF,GPIO_Pin_L);//扫描列值
  107.   if((GPIO_ReadInputData(GPIOF)&0x00f0)==0x00f0)
  108.     return 0;
  109.   else
  110.   {
  111.     Delay_nms(70);//按键消抖
  112.     if((GPIO_ReadInputData(GPIOF)&0x00f0)==0x00f0)
  113.       return 0;
  114.     else
  115.       temp1=GPIO_ReadInputData(GPIOF)&0x00f0;
  116.   }
  117.   
  118.   switch(temp1)
  119.   {
  120.   case 0x00e0:j=0;break;
  121.   case 0x00d0:j=1;break;
  122.   case 0x00b0:j=2;break;
  123.   case 0x0070:j=3;break;
  124.   default:break;
  125.   }
  126.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  127.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  128.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  129.   GPIO_Init(GPIOF, &GPIO_InitStructure);
  130.   
  131.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
  132.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  133.   GPIO_Init(GPIOF, &GPIO_InitStructure);
  134.   
  135.   GPIO_ResetBits(GPIOF,GPIO_Pin_R);//扫描行值
  136.   if((GPIO_ReadInputData(GPIOF)&0x000f)==0x000f)
  137.     return 0;
  138.   else
  139.   {//这里不再延时再扫描,因为已经确定了不是抖动才会进入本步操作
  140.     temp2=GPIO_ReadInputData(GPIOF)&0x000f;
  141.   }
  142.   
  143.   switch(temp2)
  144.   {
  145.   case 0x000e:i=0;break;
  146.   case 0x000d:i=1;break;
  147.   case 0x000b:i=2;break;
  148.   case 0x0007:i=3;break;
  149.   default:break;
  150.   }
  151.    
  152.   if((i==5)||(j==5))
  153.     return 0;
  154.   else
  155.     return (Key_Tab[i][j]);
  156. }

复制代码


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

使用道具 举报

沙发
ID:398105 发表于 2018-9-14 21:19 | 只看该作者
代码很好,多谢!
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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