你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【经验分享】STM32f1之4*4矩阵键盘

[复制链接]
STMCU小助手 发布时间:2022-5-14 22:00
前言4*4矩阵键盘对于实验来说必不可少,那么如何实现矩阵键盘基本功能呢?今天就简单地介绍一下如何利用矩阵键盘和LCD显示出对应的键值,直接进入主题。一、4*4矩阵键盘的使用首选,了解它的原理很重要,但在了解它的原理之前,先来看看它的样子。 92SFT$XD[ZI46C09)Z1DO.png 我们可以看到,4*4的矩阵键盘对应有16个按键,分别对应键值0~15(也可以自己设定),然后你会看到它有8个引脚,其中四个是行控制引脚,四个是列控制引脚 2U`{5JF8ELACUMF_)NWUC}A.png 上面就是它的主要原理图了在8个接口上,很明显的标出来C1~4和R1~4,C是column(列),R是row(行)它的扫描原理就是通过高四位输出高电平来对矩阵键盘进行逐行扫描,当低四位接收到的数据不全为1的时候,说明有按键按下,然后通过接收到的数据是哪一位为0来判断是哪一行按键被按下。二、代码部分下面这个代码部分比较简单,就不一一详解了,大家可以自己解读一下,或者在评论区发表
  1. #include "sys.h"
  2. #include "kyscan.h"
  3. #include "delay.h"
  4. #include "lcd.h"
  5. void KEY1_Init(void)
  6. {
  7.    GPIO_InitTypeDef GPIO_InitStructure;
  8.         
  9.    RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB,ENABLE);//??GPIOA?B
  10.    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;//????  
  11.    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;  
  12.    GPIO_Init(GPIOB,&GPIO_InitStructure);
  13.         
  14.         
  15.    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;        //????
  16.    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;      
  17.    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
  18.    GPIO_Init(GPIOA,&GPIO_InitStructure);      
  19.    GPIO_ResetBits(GPIOA,GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7); //??????,???
  20. }
  21. void KEY2_Init(void)
  22. {
  23.    GPIO_InitTypeDef GPIO_InitStructure;
  24.    RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB,ENABLE);
  25.    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;        //????
  26.    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  27.    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
  28.    GPIO_Init(GPIOB,&GPIO_InitStructure);
  29.    GPIO_SetBits(GPIOB,GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);  //???
  30.         
  31.    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPD;   //????
  32.    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
  33.           GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  34.    GPIO_Init(GPIOA,&GPIO_InitStructure);  
  35. }
  36. int KeyScan(void)
  37. {
  38. u8 column=0;
  39. u8 KeyValue=0;
  40.     KEY1_Init();    //??????,???
  41.         
  42.   if(PBin(12)==0)
  43.   {
  44.    delay_ms(50);
  45.    if(PBin(12)==0)
  46.    {
  47.     column=1;        //???
  48.    }
  49.   }
  50.   if(PBin(13)==0)
  51.   {
  52.    delay_ms(50);
  53.    if(PBin(13)==0)
  54.    {
  55.     column=2;        //???
  56.    }
  57.   }
  58.   if(PBin(14)==0)
  59.   {
  60.    delay_ms(50);
  61.    if(PBin(14)==0)
  62.    {
  63.     column=3;        //???
  64.    }
  65.   }
  66.   if(PBin(15)==0)
  67.   {
  68.    delay_ms(50);
  69.    if(PBin(15)==0)
  70.    {
  71.     column=4;        //???
  72.    }
  73.   }  
  74.   if(column==1)  
  75.   {
  76.     KEY2_Init();     //??????,???            
  77.      if(PAin(7)==1) //??????
  78.      {
  79.       KeyValue=0;  
  80.      }
  81.      if(PAin(6)==1) //??????
  82.      {
  83.       KeyValue=4;
  84.      }
  85.      if(PAin(5)==1) //??????
  86.      {
  87.       KeyValue=8;
  88.      }
  89.      if(PAin(4)==1) //??????
  90.      {
  91.       KeyValue=12;
  92.      }
  93.   }
  94.   if(column==2)
  95.   {
  96.     KEY2_Init();     //????               
  97.      if(PAin(7)==1)
  98.      {
  99.       KeyValue=1;
  100.      }
  101.      if(PAin(6)==1)
  102.      {
  103.       KeyValue=5;
  104.      }
  105.      if(PAin(5)==1)
  106.      {
  107.       KeyValue=9;
  108.      }
  109.      if(PAin(4)==1)
  110.      {
  111.       KeyValue=13;
  112.      }
  113.   }  
  114.   if(column==3)
  115.   {
  116.     KEY2_Init();                  
  117.      if(PAin(7)==1)
  118.      {
  119.       KeyValue=2;
  120.      }
  121.      if(PAin(6)==1)
  122.      {
  123.       KeyValue=6;
  124.      }
  125.      if(PAin(5)==1)
  126.      {
  127.       KeyValue=10;
  128.      }
  129.      if(PAin(4)==1)
  130.      {
  131.       KeyValue=14;
  132.      }
  133.   }
  134.   if(column==4)
  135.   {
  136.     KEY2_Init();            
  137.      if(PAin(7)==1)
  138.      {
  139.       KeyValue=3;
  140.                  }
  141.      if(PAin(6)==1)
  142.      {
  143.       KeyValue=7;
  144. ;   
  145.      }
  146.      if(PAin(5)==1)
  147.      {
  148.       KeyValue=11;
  149.      }
  150.      if(PAin(4)==1)
  151.      {
  152.       KeyValue=15;
  153.      }   
  154.   }
  155.     return KeyValue;
  156. }
复制代码
这里其实就是采用了扫描的方式,所以可能会有一定的延迟,大家可以采用中断的方式会更快。对了,我定义是先扫描列,因此这里列我是接PB12~15。下面附上头文件
  1. #ifndef __KY_H
  2. #define __KY_H  
  3. #include "sys.h"
  4. void KEY1_Init(void);
  5. void KEY2_Init(void);
  6. int KeyScan(void);
  7. int KeyScan2(void);
  8. #endif
复制代码
三、总结今天这一讲是比较简单的,下面会附上实现图,希望对大家有所帮助,谢谢每一位访客如图,按下键后返回键值并显示出来,大家可以尝试一下。欢迎收藏关注! `6J2XM`ZGV@)OX(~M{4MJIR.png
收藏 评论0 发布时间:2022-5-14 22:00

举报

0个回答

所属标签

相似分享

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版