找回密码
 立即注册

QQ登录

只需一步,快速开始

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

stm32矩阵按键扫描代码

[复制链接]
跳转到指定楼层
楼主
ID:225232 发表于 2017-8-7 08:08 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. //直接将代码贴出来了,就不发附件了,仅供参考
  2. u16 Get_KeyValue(void)//使用PF0~PF7
  3. {
  4.         GPIO_InitTypeDef GPIO_InitStructure;

  5.   u8 i=5,j=5;
  6.   u16 temp1,temp2;

  7.    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE);
  8.   GPIO_DeInit(GPIOF);
  9.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;                           
  10.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  11.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  12.   GPIO_Init(GPIOF, &GPIO_InitStructure);

  13.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  14.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  15.   GPIO_Init(GPIOF, &GPIO_InitStructure);

  16.   GPIO_ResetBits(GPIOF,GPIO_Pin_L);//扫描列值
  17.   if((GPIO_ReadInputData(GPIOF)&0x00f0)==0x00f0)
  18.     return 0;
  19.   else
  20.   {
  21.     Delay_nms(70);//按键消抖
  22.     if((GPIO_ReadInputData(GPIOF)&0x00f0)==0x00f0)
  23.       return 0;
  24.     else
  25.       temp1=GPIO_ReadInputData(GPIOF)&0x00f0;
  26.   }

  27.   switch(temp1)
  28.   {
  29.   case 0x00e0:j=0;break;
  30.   case 0x00d0:j=1;break;
  31.   case 0x00b0:j=2;break;
  32.   case 0x0070:j=3;break;
  33.   default:break;
  34.   }
  35.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  36.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  37.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  38.   GPIO_Init(GPIOF, &GPIO_InitStructure);

  39.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
  40.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  41.   GPIO_Init(GPIOF, &GPIO_InitStructure);

  42.   GPIO_ResetBits(GPIOF,GPIO_Pin_R);//扫描行值
  43.   if((GPIO_ReadInputData(GPIOF)&0x000f)==0x000f)
  44.     return 0;
  45.   else
  46.   {//这里不再延时再扫描,因为已经确定了不是抖动才会进入本步操作
  47.     temp2=GPIO_ReadInputData(GPIOF)&0x000f;
  48.   }

  49.   switch(temp2)
  50.   {
  51.   case 0x000e:i=0;break;
  52.   case 0x000d:i=1;break;
  53.   case 0x000b:i=2;break;
  54.   case 0x0007:i=3;break;
  55.   default:break;
  56.   }

  57.   if((i==5)||(j==5))
  58.     return 0;
  59.   else
  60.     return (Key_Tab[i][j]);
  61. }

复制代码

评分

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

查看全部评分

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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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