野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 11456|回复: 3

f429挑战者开发板矩阵键盘使用

[复制链接]
发表于 2017-5-20 11:08:55 | 显示全部楼层 |阅读模式
本帖最后由 无极剑圣 于 2017-5-24 21:02 编辑

矩阵键盘要求8个接口,可是开发板上没有连续的八个接口,所以可以这样编写程序
  1. #ifndef __KEY_H
  2. #define        __KEY_H

  3. #include "stm32f4xx.h"

  4. //引脚定义
  5. /*******************************************************/
  6. #define KEY1_PIN                  GPIO_Pin_0                 
  7. #define KEY1_GPIO_PORT            GPIOA                     
  8. #define KEY1_GPIO_CLK             RCC_AHB1Periph_GPIOA

  9. #define KEY2_PIN                  GPIO_Pin_13                 
  10. #define KEY2_GPIO_PORT            GPIOC                     
  11. #define KEY2_GPIO_CLK             RCC_AHB1Periph_GPIOC

  12. #define C1_PIN                  GPIO_Pin_3      
  13. #define C1_GPIO_PORT            GPIOA                     
  14. #define C1_GPIO_CLK             RCC_AHB1Periph_GPIOA

  15. #define C2_PIN                  GPIO_Pin_4                 
  16. #define C2_GPIO_PORT            GPIOA                 
  17. #define C2_GPIO_CLK             RCC_AHB1Periph_GPIOA

  18. #define C3_PIN                  GPIO_Pin_3     
  19. #define C3_GPIO_PORT            GPIOD
  20. #define C3_GPIO_CLK             RCC_AHB1Periph_GPIOD

  21. #define C4_PIN                  GPIO_Pin_5
  22. #define C4_GPIO_PORT            GPIOA
  23. #define C4_GPIO_CLK             RCC_AHB1Periph_GPIOA

  24. #define R1_PIN                  GPIO_Pin_4      
  25. #define R1_GPIO_PORT            GPIOH                 
  26. #define R1_GPIO_CLK             RCC_AHB1Periph_GPIOH

  27. #define R2_PIN                  GPIO_Pin_5                 
  28. #define R2_GPIO_PORT            GPIOH            
  29. #define R2_GPIO_CLK             RCC_AHB1Periph_GPIOH

  30. #define R3_PIN                  GPIO_Pin_5      
  31. #define R3_GPIO_PORT            GPIOD                 
  32. #define R3_GPIO_CLK             RCC_AHB1Periph_GPIOD

  33. #define R4_PIN                  GPIO_Pin_6      
  34. #define R4_GPIO_PORT            GPIOD               
  35. #define R4_GPIO_CLK             RCC_AHB1Periph_GPIOD



  36. /*******************************************************/



  37. void Key_GPIO_Config(void);
  38. int Read_KeyValue(void);
  39. int Key_ReadInputData(void);

  40. #endif /* __LED_H */

复制代码




  1.   
  2. #include "./key/bsp_key.h"

  3. /// 延时
  4. void Delay_ms(int xms)        
  5. {
  6.   u32 i,j;
  7.         for(i=xms;i>0;i--)
  8.           for(j=72000;j>0;j--);
  9. }

  10. /**
  11.   * @brief  配置按键用到的I/O口
  12.   * @param  无
  13.   * @retval 无
  14.   */
  15. void Key_GPIO_Config(void)
  16. {
  17.         GPIO_InitTypeDef GPIO_InitStructure;
  18.         
  19.         /*开启按键GPIO口的时钟*/
  20.         RCC_AHB1PeriphClockCmd(KEY1_GPIO_CLK|KEY2_GPIO_CLK|C1_GPIO_CLK|C2_GPIO_CLK|C3_GPIO_CLK|C4_GPIO_CLK|R1_GPIO_CLK|R2_GPIO_CLK|R3_GPIO_CLK|R4_GPIO_CLK,ENABLE);
  21.         
  22.   /*选择按键的引脚*/
  23.         GPIO_InitStructure.GPIO_Pin = KEY1_PIN;
  24.   
  25.   /*设置引脚为输入模式*/
  26.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  27.   
  28.   /*设置引脚下拉*/
  29.   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
  30.         
  31.   /*使用上面的结构体初始化按键*/
  32.         GPIO_Init(KEY1_GPIO_PORT, &GPIO_InitStructure);  
  33.   
  34.   /*选择按键的引脚*/
  35.         GPIO_InitStructure.GPIO_Pin = KEY2_PIN;
  36.   
  37.   /*使用上面的结构体初始化按键*/
  38.         GPIO_Init(KEY2_GPIO_PORT, &GPIO_InitStructure);  

  39.         
  40.   /*选择要控制的GPIO引脚*/                                                                                                                           
  41.         GPIO_InitStructure.GPIO_Pin = C1_PIN;        

  42.         /*调用库函数,使用上面配置的GPIO_InitStructure初始化GPIO*/
  43.         GPIO_Init(C1_GPIO_PORT, &GPIO_InitStructure);        


  44.                 /*选择按键的引脚*/
  45.         GPIO_InitStructure.GPIO_Pin = C2_PIN;
  46.   
  47.   /*使用上面的结构体初始化按键*/
  48.         GPIO_Init(C2_GPIO_PORT, &GPIO_InitStructure);  
  49.         

  50.         
  51.         /*选择按键的引脚*/
  52.         GPIO_InitStructure.GPIO_Pin = C3_PIN;
  53.   
  54.   /*使用上面的结构体初始化按键*/
  55.         GPIO_Init(C3_GPIO_PORT, &GPIO_InitStructure);  

  56.         
  57.         /*选择按键的引脚*/
  58.         GPIO_InitStructure.GPIO_Pin = C4_PIN;
  59.   
  60.   /*使用上面的结构体初始化按键*/
  61.         GPIO_Init(C4_GPIO_PORT, &GPIO_InitStructure);  
  62.         



  63.         /*设置引脚模式为输出模式*/
  64.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;   
  65.         
  66.         /*设置引脚的输出类型为推挽输出*/
  67.         GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  68.         
  69.         /*设置引脚为下拉模式*/
  70.         GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;

  71.         /*设置引脚速率为2MHz */   
  72.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;

  73.                
  74.         /*选择按键的引脚*/
  75.         GPIO_InitStructure.GPIO_Pin = R1_PIN;
  76.   
  77.   /*使用上面的结构体初始化按键*/
  78.         GPIO_Init(R1_GPIO_PORT, &GPIO_InitStructure);  
  79.         
  80.         /*选择按键的引脚*/
  81.         GPIO_InitStructure.GPIO_Pin = R2_PIN;
  82.   
  83.   /*使用上面的结构体初始化按键*/
  84.         GPIO_Init(R2_GPIO_PORT, &GPIO_InitStructure);  
  85.         
  86.         /*选择按键的引脚*/
  87.         GPIO_InitStructure.GPIO_Pin = R3_PIN;
  88.   
  89.   /*使用上面的结构体初始化按键*/
  90.         GPIO_Init(R3_GPIO_PORT, &GPIO_InitStructure);  
  91.         
  92.         /*选择按键的引脚*/
  93.         GPIO_InitStructure.GPIO_Pin = R4_PIN;
  94.   
  95.   /*使用上面的结构体初始化按键*/
  96.         GPIO_Init(R4_GPIO_PORT, &GPIO_InitStructure);  
  97.         
  98.         GPIO_WriteBit(R1_GPIO_PORT,R1_PIN, Bit_SET);
  99.         
  100.         GPIO_WriteBit(R2_GPIO_PORT,R2_PIN, Bit_SET);

  101.         GPIO_WriteBit(R3_GPIO_PORT,R3_PIN, Bit_SET);

  102.         GPIO_WriteBit(R4_GPIO_PORT,R4_PIN, Bit_SET);

  103.         GPIO_WriteBit(C1_GPIO_PORT,C1_PIN, Bit_RESET);

  104.         GPIO_WriteBit(C2_GPIO_PORT,C2_PIN, Bit_RESET);

  105.         GPIO_WriteBit(C3_GPIO_PORT,C3_PIN, Bit_RESET);

  106.         GPIO_WriteBit(C4_GPIO_PORT,C4_PIN, Bit_RESET);

  107.         
  108. }




  109. /************************************/


  110. int Read_KeyValue(void)
  111. {
  112.         int KeyValue=-1;
  113.         if((Key_ReadInputData()&0xFF)!=0x0F)                                                               
  114.         {
  115.                 Delay_ms(8);                                                                                                                                                                        
  116.                 if((Key_ReadInputData()&0xFF)!=0x0F)                                                
  117.                 {
  118.                                                 
  119.                 GPIO_SetBits(R1_GPIO_PORT, R1_PIN);                                                                                
  120.                 GPIO_ResetBits(R2_GPIO_PORT, R2_PIN);
  121.                 GPIO_ResetBits(R3_GPIO_PORT, R3_PIN);
  122.                 GPIO_ResetBits(R4_GPIO_PORT, R4_PIN);
  123.                         
  124.                 switch(Key_ReadInputData()&0xFF)                                                        
  125.                 {                        
  126.                         case 0x11: KeyValue = 1; break;                                // 0001 0001
  127.                         case 0x21: KeyValue = 2; break;                                // 0010 0001
  128.                         case 0x41: KeyValue = 3; break;                                // 0100 0001
  129.                         case 0x81: KeyValue = 14; break;                        // 1000 0001
  130.                 }

  131.                 GPIO_SetBits(R2_GPIO_PORT, R2_PIN);                                                                                
  132.                 GPIO_ResetBits(R1_GPIO_PORT, R1_PIN);
  133.                 GPIO_ResetBits(R3_GPIO_PORT, R3_PIN);
  134.                 GPIO_ResetBits(R4_GPIO_PORT, R4_PIN);                       
  135.                 switch(Key_ReadInputData()&0xFF)                                                        
  136.                 {                        
  137.                         case 0x12: KeyValue = 4; break;                                // 0001 0010
  138.                         case 0x22: KeyValue = 5; break;                                // 0010 0010
  139.                         case 0x42: KeyValue = 6; break;                                // 0100 0010
  140.                         case 0x82: KeyValue = 24; break;                        // 1000 0010
  141.                 }
  142.                
  143.                 GPIO_SetBits(R3_GPIO_PORT, R3_PIN);                                                                                
  144.                 GPIO_ResetBits(R2_GPIO_PORT, R2_PIN);
  145.                 GPIO_ResetBits(R1_GPIO_PORT, R1_PIN);
  146.                 GPIO_ResetBits(R4_GPIO_PORT, R4_PIN);                       
  147.                 switch(Key_ReadInputData()&0xFF)                                                        
  148.                 {                        
  149.                         case 0x14: KeyValue = 7; break;                                // 0001 0100
  150.                         case 0x24: KeyValue = 8; break;                                // 0010 0100
  151.                         case 0x44: KeyValue = 9; break;                                // 0100 0100
  152.                         case 0x84: KeyValue = 34; break;                        // 1000 0100
  153.                 }
  154.                
  155.                 GPIO_SetBits(R4_GPIO_PORT, R4_PIN);                                                                                
  156.                 GPIO_ResetBits(R2_GPIO_PORT, R2_PIN);
  157.                 GPIO_ResetBits(R3_GPIO_PORT, R3_PIN);
  158.                 GPIO_ResetBits(R1_GPIO_PORT, R1_PIN);                       
  159.                 switch(Key_ReadInputData()&0xFF)                                                        
  160.                 {                        
  161.                         case 0x18: KeyValue = 41; break;                                // 0001 1000
  162.                         case 0x28: KeyValue = 0; break;                                // 0010 1000
  163.                         case 0x48: KeyValue = 43; break;                                // 0100 1000
  164.                         case 0x88: KeyValue = 44; break;                                // 1000 1000
  165.                 }               
  166.                
  167.                 GPIO_SetBits(R1_GPIO_PORT, R1_PIN);
  168.                 GPIO_SetBits(R2_GPIO_PORT, R2_PIN);
  169.                 GPIO_SetBits(R3_GPIO_PORT, R3_PIN);
  170.                 GPIO_SetBits(R4_GPIO_PORT, R4_PIN);
  171.                 GPIO_ResetBits(C1_GPIO_PORT, C1_PIN);  
  172.                 GPIO_ResetBits(C2_GPIO_PORT, C2_PIN);  
  173.                 GPIO_ResetBits(C3_GPIO_PORT, C3_PIN);  
  174.                 GPIO_ResetBits(C4_GPIO_PORT, C4_PIN);  
  175.                
  176.                
  177.                 while((Key_ReadInputData()&0xFF)!=0x0F);                                
  178.                
  179.                 return KeyValue;
  180.                 }
  181.         }
  182.         return -1;
  183. }

  184. int Key_ReadInputData()
  185. {
  186.         int KeyValue_1=-1;
  187.         volatile int R1,R2,R3,R4,C1,C2,C3,C4;
  188.         C4=(int)GPIO_ReadInputDataBit(C4_GPIO_PORT, C4_PIN)<<7;
  189.         C3=(int)GPIO_ReadInputDataBit(C3_GPIO_PORT, C3_PIN)<<6;
  190.         C2=(int)GPIO_ReadInputDataBit(C2_GPIO_PORT, C2_PIN)<<5;
  191.         C1=(int)GPIO_ReadInputDataBit(C1_GPIO_PORT, C1_PIN)<<4;
  192.         R4=(int)GPIO_ReadInputDataBit(R4_GPIO_PORT, R4_PIN)<<3;
  193.         R3=(int)GPIO_ReadInputDataBit(R3_GPIO_PORT, R3_PIN)<<2;
  194.         R2=(int)GPIO_ReadInputDataBit(R2_GPIO_PORT, R2_PIN)<<1;
  195.         R1=(int)GPIO_ReadInputDataBit(R1_GPIO_PORT, R1_PIN)<<0;
  196.         
  197.         KeyValue_1=C4|C3|C2|C1|R4|R3|R2|R1;
  198.         
  199.         return KeyValue_1;
  200.                               }
  201. /*********************************************END OF FILE**********************/
复制代码




回复

使用道具 举报

发表于 2017-5-20 14:27:36 | 显示全部楼层
感谢分享
回复

使用道具 举报

发表于 2017-5-24 16:33:57 | 显示全部楼层
你好,楼主,你的程序我看了好久,有两个问题想请教一下
1、这个Key_Scan()函数起到什么作用?Read_KeyValue(void)这个函数就可以扫描按键吧
2、在 Key_GPIO_Config()最后几行的代码
       GPIO_Write(GPIOA, 0X0000);
        KeyValue_temp=GPIO_ReadInputData(GPIOA);
        
        GPIO_WriteBit(R1_GPIO_PORT,R1_PIN, Bit_SET);
    这样做的目的是什么,至今没研究明白,希望得到你的帮助,谢谢
        
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-5-24 20:59:52 | 显示全部楼层
hongtao 发表于 2017-5-24 16:33
你好,楼主,你的程序我看了好久,有两个问题想请教一下
1、这个Key_Scan()函数起到什么作用?Read_KeyV ...

很抱歉坑了你,这两处都是我之前编写的过程中用到的,随后都失去了原有的作用,忘记注释掉了
回复 支持 反对

使用道具 举报

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

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-9-22 14:34 , Processed in 0.061928 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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