找回密码
 立即注册

QQ登录

只需一步,快速开始

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

STM32 4X4按键矩阵的小程序

[复制链接]
跳转到指定楼层
楼主
ID:378929 发表于 2019-2-16 04:28 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
前篇帖子写了基本的GPIO复用技术,现在补上按键矩阵技术,由于STM32的GPIO口不像传统51,准双向口,故而不能使用线反转法,我采用了行列扫描的技术,GPIO口用了A组,故而库函数没法直接调用赋值和读取同时16位的函数,只能调用一个个pin读取函数和赋值函数,显得有点臃肿。但是STM32性能牛逼,这么多if的语句还跑的这么流畅,如果是C51早gg了,在STC12勉强可以看看。仅供参考。STM32学起来比51更费劲,英语不好的话,库函数弄死人。

此工程,主要用于编程4X4按键矩阵的识别和编码的功能

单片机源程序如下:
  1. #include <STM32F10X.H>
  2. #include <misc.h>

  3. #define PA0_Low  GPIO_ResetBits(GPIOA,GPIO_Pin_0)
  4. #define PA0_High GPIO_SetBits(GPIOA,GPIO_Pin_0)
  5. #define PA1_Low  GPIO_ResetBits(GPIOA,GPIO_Pin_1)
  6. #define PA1_High GPIO_SetBits(GPIOA,GPIO_Pin_1)
  7. #define PA2_Low  GPIO_ResetBits(GPIOA,GPIO_Pin_2)
  8. #define PA2_High GPIO_SetBits(GPIOA,GPIO_Pin_2)
  9. #define PA3_Low  GPIO_ResetBits(GPIOA,GPIO_Pin_3)
  10. #define PA3_High GPIO_SetBits(GPIOA,GPIO_Pin_3)
  11. #define PA4    GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4)
  12. #define PA5    GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_5)
  13. #define PA6    GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_6)
  14. #define PA7    GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_7)
  15. #define PC15_Low GPIO_WriteBit(GPIOC,GPIO_Pin_15,Bit_RESET)
  16. #define PC15_High GPIO_WriteBit(GPIOC,GPIO_Pin_15,Bit_SET)

  17. void RCC_Configuration(void);                             //时钟与复位寄存器
  18. void GPIO_Configuration(void);                                        //GPIO口初始化函数
  19. void Delay_ms(u16 n);                                                                                //ms级别延时函数
  20. u16 Array_Key(void);                                                                                //4X4按键矩阵检测函数
  21. void Led_Show(u16 keystatus);                                                //按键检测后的状态显示函数
  22. u16 ledcount=0;
  23. u16 status=0;
  24. int main(void)
  25. {
  26.         RCC_Configuration();
  27.         GPIO_Configuration();
  28.         while(1)
  29.         {
  30.                 Led_Show(Array_Key());
  31.         }
  32. }

  33. void RCC_Configuration(void)
  34. {
  35.         SystemInit();
  36. }

  37. void GPIO_Configuration(void)
  38. {
  39.         GPIO_InitTypeDef GPIO_Struct;                                                    //定义一个GPIO初始化的结构体变量
  40.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC, ENABLE);
  41.         
  42.         GPIO_Struct.GPIO_Mode=GPIO_Mode_Out_PP;
  43.         GPIO_Struct.GPIO_Speed=GPIO_Speed_50MHz;
  44.         GPIO_Struct.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
  45.         GPIO_Init(GPIOA,&GPIO_Struct);
  46.         
  47.         GPIO_Struct.GPIO_Mode=GPIO_Mode_IPU;
  48.         GPIO_Struct.GPIO_Speed=GPIO_Speed_50MHz;
  49.         GPIO_Struct.GPIO_Pin=GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
  50.         GPIO_Init(GPIOA,&GPIO_Struct);
  51.         
  52.         GPIO_Struct.GPIO_Mode=GPIO_Mode_Out_PP;
  53.         GPIO_Struct.GPIO_Speed=GPIO_Speed_50MHz;
  54.         GPIO_Struct.GPIO_Pin=GPIO_Pin_15;
  55.         GPIO_Init(GPIOC,&GPIO_Struct);
  56.         
  57.         PA0_High;PA1_High;PA2_High;PA3_High;
  58.         PC15_Low;
  59. }

  60. u16 Array_Key(void)
  61. {
  62.         PA0_Low;PA1_High;PA2_High;PA3_High;
  63.         if(PA4 == 0)
  64.         {
  65.                 Delay_ms(10);
  66.                 if(PA4 == 0)
  67.                 {
  68.                         while(!PA4);
  69.                         status = 1;
  70.           }
  71.         }
  72.         if(PA5 == 0)
  73.         {
  74.                 Delay_ms(10);
  75.                 if(PA5 == 0)
  76.                 {
  77.                         while(!PA5);
  78.                         status = 2;
  79.           }
  80.         }
  81.         if(PA6 == 0)
  82.         {
  83.                 Delay_ms(10);
  84.                 if(PA6 == 0)
  85.                 {
  86.                         while(!PA6);
  87.                         status = 3;
  88.           }
  89.         }
  90.         if(PA7 == 0)
  91.         {
  92.                 Delay_ms(10);
  93.                 if(PA7 == 0)
  94.                 {
  95.                         while(!PA7);
  96.                         status = 4;
  97.           }
  98.         }
  99.         
  100.         PA0_High;PA1_Low;PA2_High;PA3_High;
  101.         if(PA4 == 0)
  102.         {
  103.                 Delay_ms(10);
  104.                 if(PA4 == 0)
  105.                 {
  106.                         while(!PA4);
  107.                         status = 5;
  108.           }
  109.         }
  110.         if(PA5 == 0)
  111.         {
  112.                 Delay_ms(10);
  113.                 if(PA5 == 0)
  114.                 {
  115.                         while(!PA5);
  116.                         status = 6;
  117.           }
  118.         }
  119.         if(PA6 == 0)
  120.         {
  121.                 Delay_ms(10);
  122.                 if(PA6 == 0)
  123.                 {
  124.                         while(!PA6);
  125.                         status = 7;
  126.           }
  127.         }
  128.         if(PA7 == 0)
  129.         {
  130.                 Delay_ms(10);
  131.                 if(PA7 == 0)
  132.                 {
  133.                         while(!PA7);
  134.                         status = 8;
  135.           }
  136.         }
  137.         PA0_High;PA1_High;PA2_Low;PA3_High;
  138.         if(PA4 == 0)
  139.         {
  140.                 Delay_ms(10);
  141.                 if(PA4 == 0)
  142.                 {
  143.                         while(!PA4);
  144.                         status = 9;
  145.           }
  146.         }
  147.         if(PA5 == 0)
  148.         {
  149.                 Delay_ms(10);
  150.                 if(PA5 == 0)
  151.                 {
  152.                         while(!PA5);
  153.                         status = 10;
  154.           }
  155.         }
  156.         if(PA6 == 0)
  157.         {
  158.                 Delay_ms(10);
  159.                 if(PA6 == 0)
  160.                 {
  161.                         while(!PA6);
  162.                         status = 11;
  163.           }
  164.         }
  165.         if(PA7 == 0)
  166.         {
  167.                 Delay_ms(10);
  168.                 if(PA7 == 0)
  169.                 {
  170.                         while(!PA7);
  171.                         status = 12;
  172.           }
  173.         }
  174.         PA0_High;PA1_High;PA2_High;PA3_Low;
  175.         if(PA4 == 0)
  176.         {
  177.                 Delay_ms(10);
  178.                 if(PA4 == 0)
  179.                 {
  180.                         while(!PA4);
  181.                         status = 13;
  182.           }
  183.         }
  184.         if(PA5 == 0)
  185.         {
  186.                 Delay_ms(10);
  187.                 if(PA5 == 0)
  188.                 {
  189.                         while(!PA5);
  190.                         status = 14;
  191.           }
  192.         }
  193.         if(PA6 == 0)
  194.         {
  195.                 Delay_ms(10);
  196.                 if(PA6 == 0)
  197.                 {
  198.                         while(!PA6);
  199.                         status = 15;
  200.           }
  201.         }
  202.         if(PA7 == 0)
  203.         {
  204.                 Delay_ms(10);
  205.                 if(PA7 == 0)
  206.                 {
  207.                         while(!PA7);
  208.                         status = 16;
  209.           }
  210.         }
  211.         return status;
  212. }

  213. void Delay_ms(u16 n)
  214. {
  215.         u16 i;
  216.         while(n--)
  217.         {
  218.                 i=12000;
  219.                 while(i--);
  220.         }
  221. }

  222. void Led_Show(u16 keystatus)
  223. {
  224.         if(keystatus != 0)
  225.         {ledcount++;status=0;}
  226.         if(ledcount % 2 == 0)
  227.                 PC15_Low;
  228.         else
  229.                 PC15_High;
  230. }
复制代码

所有资料51hei提供下载:
Key_Array.7z (192.09 KB, 下载次数: 49)



评分

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

查看全部评分

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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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