找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1411|回复: 0
收起左侧

STM32F103ZET6核心板 不连续IO口4*4 矩阵键盘测试程序

[复制链接]
ID:849635 发表于 2021-9-10 14:15 | 显示全部楼层 |阅读模式
  1. /*****接口如下
  2. C1---B9;C2-C4----B6 ;R1---B5;2----15;3---12;4---11   
  3. ***/
  4. #include "key4_4.h"
  5. #include "delay.h"
  6. #include "sys.h"
  7. //8个引脚 4个为行 4个为列
  8. //行输出端口定义
  9. #define X1_GPIO_PORT GPIOA           
  10. #define X2_GPIO_PORT GPIOA   
  11. #define X3_GPIO_PORT GPIOA           
  12. #define X4_GPIO_PORT GPIOB
  13. //列输入端口定义
  14. #define Y1_GPIO_PORT GPIOA        
  15. #define Y2_GPIO_PORT GPIOA  
  16. #define Y3_GPIO_PORT GPIOA         
  17. #define Y4_GPIO_PORT GPIOA

  18. //行输出引脚定义
  19. #define X1_GPIO_PIN GPIO_Pin_5
  20. #define X2_GPIO_PIN GPIO_Pin_6
  21. #define X3_GPIO_PIN GPIO_Pin_7
  22. #define X4_GPIO_PIN GPIO_Pin_10

  23. //列输入引脚定义
  24. #define Y1_GPIO_PIN GPIO_Pin_4
  25. #define Y2_GPIO_PIN GPIO_Pin_3
  26. #define Y3_GPIO_PIN GPIO_Pin_2
  27. #define Y4_GPIO_PIN GPIO_Pin_1

  28. //行输出时钟定义
  29. #define X1_RCC RCC_APB2Periph_GPIOA
  30. #define X2_RCC RCC_APB2Periph_GPIOA
  31. #define X3_RCC RCC_APB2Periph_GPIOA
  32. #define X4_RCC RCC_APB2Periph_GPIOB

  33. //列输入时钟定义
  34. #define Y1_RCC RCC_APB2Periph_GPIOA
  35. #define Y2_RCC RCC_APB2Periph_GPIOA
  36. #define Y3_RCC RCC_APB2Periph_GPIOA
  37. #define Y4_RCC RCC_APB2Periph_GPIOA

  38. //移植代码只需要修改上面的端口和引脚和时钟即可,下面的代码不用修改。
  39. //矩阵键盘所用的8个引脚可连续可不连续,看实际需要和个人爱好自己定义。

  40. unsigned char Y1,Y2,Y3,Y4;
  41. void Key_Init(void)
  42. {
  43.    GPIO_InitTypeDef GPIO_InitStructure;   
  44.    RCC_APB2PeriphClockCmd(X1_RCC|X2_RCC|X3_RCC|X4_RCC|Y1_RCC|Y2_RCC|Y3_RCC|Y4_RCC|RCC_APB2Periph_AFIO, ENABLE);
  45. //   GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
  46. //        RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
  47. //开启AFIO时钟
  48. //
  49. //GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable,ENABLE);
  50. ////改变指定管脚的映射 GPIO_Remap_SWJ_Disable SWJ 完全禁用(JTAG+SW-DP)
  51. //
  52. //GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
  53. //改变指定管脚的映射 GPIO_Remap_SWJ_JTAGDisable ,JTAG-DP 禁用 + SW-DP 使能
  54.         
  55. /*****************************4行输出*********************************************/
  56.    GPIO_InitStructure.GPIO_Pin =  X1_GPIO_PIN ;
  57.    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;         
  58.    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  59.    GPIO_Init(X1_GPIO_PORT, &GPIO_InitStructure);
  60.    
  61.    GPIO_InitStructure.GPIO_Pin =  X2_GPIO_PIN ;
  62.    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;         
  63.    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  64.    GPIO_Init(X2_GPIO_PORT, &GPIO_InitStructure);
  65.    
  66.    GPIO_InitStructure.GPIO_Pin =  X3_GPIO_PIN ;
  67.    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;         
  68.    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  69.    GPIO_Init(X3_GPIO_PORT, &GPIO_InitStructure);
  70.         
  71.    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;         
  72.    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  73.    GPIO_InitStructure.GPIO_Pin = X4_GPIO_PIN ;   
  74.    GPIO_Init(X4_GPIO_PORT, &GPIO_InitStructure);
  75.    
  76. /**************************************4列输入*************************************/
  77.    GPIO_InitStructure.GPIO_Pin =  Y1_GPIO_PIN ;   
  78.    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;         
  79.    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  80.    GPIO_Init(Y1_GPIO_PORT, &GPIO_InitStructure);        
  81.    
  82.    GPIO_InitStructure.GPIO_Pin =  Y2_GPIO_PIN ;   
  83.    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;         
  84.    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  85.    GPIO_Init(Y2_GPIO_PORT, &GPIO_InitStructure);        
  86.    
  87.    GPIO_InitStructure.GPIO_Pin =  Y3_GPIO_PIN ;   
  88.    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;         
  89.    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  90.    GPIO_Init(Y3_GPIO_PORT, &GPIO_InitStructure);        
  91.         
  92.    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;         
  93.    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  94.    GPIO_InitStructure.GPIO_Pin = Y4_GPIO_PIN;      
  95.    GPIO_Init(Y4_GPIO_PORT, &GPIO_InitStructure);        
  96. }

  97. int Key_Scan(void)
  98. {
  99.    uchar KeyVal;
  100.    GPIO_SetBits(X1_GPIO_PORT,X1_GPIO_PIN);  //先让X1输出高
  101.    GPIO_SetBits(X2_GPIO_PORT,X2_GPIO_PIN);  //先让X2输出高
  102.    GPIO_SetBits(X3_GPIO_PORT,X3_GPIO_PIN);  //先让X3输出高
  103.    GPIO_SetBits(X4_GPIO_PORT,X4_GPIO_PIN);  //先让X4输出高


  104.         if((GPIO_ReadInputDataBit(Y1_GPIO_PORT,Y1_GPIO_PIN)|GPIO_ReadInputDataBit(Y2_GPIO_PORT,Y2_GPIO_PIN)|GPIO_ReadInputDataBit(Y3_GPIO_PORT,Y3_GPIO_PIN)|GPIO_ReadInputDataBit(Y4_GPIO_PORT,Y4_GPIO_PIN))==0x0000)  
  105.         return -1; //如果X1到X4全为零则没有按键按下  
  106.          else
  107.          {        
  108.             delay_ms(5);    //延时5ms去抖动
  109.          if((GPIO_ReadInputDataBit(Y1_GPIO_PORT,Y1_GPIO_PIN)|GPIO_ReadInputDataBit(Y2_GPIO_PORT,Y2_GPIO_PIN)|GPIO_ReadInputDataBit(Y3_GPIO_PORT,Y3_GPIO_PIN)|GPIO_ReadInputDataBit(Y4_GPIO_PORT,Y4_GPIO_PIN))==0x0000)
  110.             return -1;
  111.          }
  112.          
  113.      GPIO_ResetBits(X1_GPIO_PORT,X1_GPIO_PIN);
  114.      GPIO_ResetBits(X2_GPIO_PORT,X2_GPIO_PIN);
  115.      GPIO_ResetBits(X3_GPIO_PORT,X3_GPIO_PIN);
  116.      GPIO_SetBits(X4_GPIO_PORT,X4_GPIO_PIN);
  117.      
  118.     Y1=GPIO_ReadInputDataBit(Y1_GPIO_PORT,Y1_GPIO_PIN);Y2=GPIO_ReadInputDataBit(Y2_GPIO_PORT,Y2_GPIO_PIN);
  119.     Y3=GPIO_ReadInputDataBit(Y3_GPIO_PORT,Y3_GPIO_PIN);Y4=GPIO_ReadInputDataBit(Y4_GPIO_PORT,Y4_GPIO_PIN);
  120.      if(Y1==1&&Y2==0&&Y3==0&&Y4==0)
  121.             KeyVal='*';
  122.      if(Y1==0&&Y2==1&&Y3==0&&Y4==0)
  123.             KeyVal=0;
  124.      if(Y1==0&&Y2==0&&Y3==0&&Y4==1)
  125.             KeyVal='D';
  126.      if(Y1==0&&Y2==0&&Y3==1&&Y4==0)
  127.             KeyVal='#';
  128.    
  129.      while(((GPIO_ReadInputDataBit(Y1_GPIO_PORT,Y1_GPIO_PIN))|(GPIO_ReadInputDataBit(Y2_GPIO_PORT,Y2_GPIO_PIN))|(GPIO_ReadInputDataBit(Y3_GPIO_PORT,Y3_GPIO_PIN))|(GPIO_ReadInputDataBit(Y4_GPIO_PORT,Y4_GPIO_PIN))) > 0);
  130.     //等待按键释放
  131.      GPIO_SetBits(X1_GPIO_PORT,X1_GPIO_PIN);
  132.      GPIO_ResetBits(X2_GPIO_PORT,X2_GPIO_PIN);
  133.      GPIO_ResetBits(X3_GPIO_PORT,X3_GPIO_PIN);
  134.      GPIO_ResetBits(X4_GPIO_PORT,X4_GPIO_PIN);
  135.    
  136.     Y1=GPIO_ReadInputDataBit(Y1_GPIO_PORT,Y1_GPIO_PIN);Y2=GPIO_ReadInputDataBit(Y2_GPIO_PORT,Y2_GPIO_PIN);
  137.     Y3=GPIO_ReadInputDataBit(Y3_GPIO_PORT,Y3_GPIO_PIN);Y4=GPIO_ReadInputDataBit(Y4_GPIO_PORT,Y4_GPIO_PIN);
  138.      if(Y1==1&&Y2==0&&Y3==0&&Y4==0)
  139.             KeyVal=1;
  140.      if(Y1==0&&Y2==1&&Y3==0&&Y4==0)
  141.             KeyVal=2;
  142.      if(Y1==0&&Y2==0&&Y3==1&&Y4==0)
  143.             KeyVal=3;
  144.      if(Y1==0&&Y2==0&&Y3==0&&Y4==1)
  145.             KeyVal='A';
  146.       
  147.       while(((GPIO_ReadInputDataBit(Y1_GPIO_PORT,Y1_GPIO_PIN))|(GPIO_ReadInputDataBit(Y2_GPIO_PORT,Y2_GPIO_PIN))|(GPIO_ReadInputDataBit(Y3_GPIO_PORT,Y3_GPIO_PIN))|(GPIO_ReadInputDataBit(Y4_GPIO_PORT,Y4_GPIO_PIN))) > 0);
  148.                

  149.      GPIO_ResetBits(X1_GPIO_PORT,X1_GPIO_PIN);
  150.      GPIO_SetBits(X2_GPIO_PORT,X2_GPIO_PIN);
  151.      GPIO_ResetBits(X3_GPIO_PORT,X3_GPIO_PIN);
  152.      GPIO_ResetBits(X4_GPIO_PORT,X4_GPIO_PIN);
  153.         
  154.      Y1=GPIO_ReadInputDataBit(Y1_GPIO_PORT,Y1_GPIO_PIN);Y2=GPIO_ReadInputDataBit(Y2_GPIO_PORT,Y2_GPIO_PIN);
  155.      Y3=GPIO_ReadInputDataBit(Y3_GPIO_PORT,Y3_GPIO_PIN);Y4=GPIO_ReadInputDataBit(Y4_GPIO_PORT,Y4_GPIO_PIN);
  156.      if(Y1==1&&Y2==0&&Y3==0&&Y4==0)
  157.             KeyVal=4;
  158.      if(Y1==0&&Y2==1&&Y3==0&&Y4==0)
  159.             KeyVal=5;
  160.      if(Y1==0&&Y2==0&&Y3==1&&Y4==0)
  161.             KeyVal=6;
  162.      if(Y1==0&&Y2==0&&Y3==0&&Y4==1)
  163.             KeyVal='B';
  164.    
  165.       while(((GPIO_ReadInputDataBit(Y1_GPIO_PORT,Y1_GPIO_PIN))|(GPIO_ReadInputDataBit(Y2_GPIO_PORT,Y2_GPIO_PIN))|(GPIO_ReadInputDataBit(Y3_GPIO_PORT,Y3_GPIO_PIN))|(GPIO_ReadInputDataBit(Y4_GPIO_PORT,Y4_GPIO_PIN))) > 0);
  166.                
  167.      GPIO_ResetBits(X1_GPIO_PORT,X1_GPIO_PIN);
  168.      GPIO_ResetBits(X2_GPIO_PORT,X2_GPIO_PIN);
  169.      GPIO_SetBits(X3_GPIO_PORT,X3_GPIO_PIN);
  170.      GPIO_ResetBits(X4_GPIO_PORT,X4_GPIO_PIN);   

  171.      Y1=GPIO_ReadInputDataBit(Y1_GPIO_PORT,Y1_GPIO_PIN);Y2=GPIO_ReadInputDataBit(Y2_GPIO_PORT,Y2_GPIO_PIN);
  172.      Y3=GPIO_ReadInputDataBit(Y3_GPIO_PORT,Y3_GPIO_PIN);Y4=GPIO_ReadInputDataBit(Y4_GPIO_PORT,Y4_GPIO_PIN);
  173.      if(Y1==1&&Y2==0&&Y3==0&&Y4==0)
  174.             KeyVal=7;
  175.      if(Y1==0&&Y2==1&&Y3==0&&Y4==0)
  176.             KeyVal=8;
  177.      if(Y1==0&&Y2==0&&Y3==1&&Y4==0)
  178.             KeyVal=9;
  179.      if(Y1==0&&Y2==0&&Y3==0&&Y4==1)
  180.             KeyVal='C';

  181.        while(((GPIO_ReadInputDataBit(Y1_GPIO_PORT,Y1_GPIO_PIN))|(GPIO_ReadInputDataBit(Y2_GPIO_PORT,Y2_GPIO_PIN))|(GPIO_ReadInputDataBit(Y3_GPIO_PORT,Y3_GPIO_PIN))|(GPIO_ReadInputDataBit(Y4_GPIO_PORT,Y4_GPIO_PIN))) > 0);
  182.          
  183.                 return KeyVal;
  184. }

  185. /************************************
  186.         按键表盘为:                1  2  3  A
  187.                                                         4  5  6  B
  188.                                                         7  8  9  C
  189.                                                         *  0  #  D
  190. ************************************/
  191. void Key_Test(void)
  192. {
  193.     int num;
  194.           num = Key_Scan();
  195. //        printf("%d",num) ;
  196.           switch(num)
  197.                 {
  198. //                        {          case -1:printf("0\n"); break;               
  199.                 case 0: printf("0\n"); break;                                                                                
  200.                                 case 1: printf("1\n"); break;                                                                                 
  201.                                 case 2: printf("2\n"); break;                                                                              
  202.                                 case 3: printf("3\n"); break;                                                                              
  203.                                 case 4: printf("4\n"); break;                                                
  204.                                 case 5: printf("5\n"); break;                                                                                
  205.                                 case 6: printf("6\n"); break;                                                                                
  206.                                 case 7: printf("7\n"); break;                                                                        
  207.                                 case 8: printf("8\n"); break;                                                                                       
  208.                                 case 9: printf("9\n"); break;                                                                                                      
  209.                 case 'A': printf("A\n"); break;                                                                                                      
  210.                                 case 'B': printf("B\n"); break;                                                                              
  211.                                 case 'C': printf("C\n"); break;                                                                                                      
  212.                 case 'D': printf("D\n"); break;                                                                                                        
  213.                                 case '#': printf("#\n"); break;                                                                              
  214.                                 case '*': printf("*\n"); break;                                                                       
  215.       }
  216. }
复制代码

代码下载: 代码.7z (219.59 KB, 下载次数: 25)

评分

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

查看全部评分

回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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