找回密码
 立即注册

QQ登录

只需一步,快速开始

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

这个接地才能用的STM32矩阵按键如何改成上拉电阻可用的?

[复制链接]
跳转到指定楼层
楼主
ID:405123 发表于 2019-12-13 08:55 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式
有没有大佬能不能帮我把这个接地才能用的矩阵按键改成上拉电阻可用的!

单片机源程序如下:
  1. #include "key4_4.h"
  2. #include "delay.h"
  3. #include "sys.h"
  4. //平台:STM32F103
  5. //实验名称:不连续I/O矩阵键盘实验  
  6. //Author:GXNU_LPK_20512700***
  7. //使用说明:矩阵键盘所用引脚都已经用宏定义定义好了,移植只需根据实际需要在key4_4.c中修改对应的时钟、引脚和端口即可,其余都不用修改。
  8. //矩阵键盘所用的8个引脚可连续可不连续,看实际需要和个人爱好自己定义。
  9. int main(void)
  10. {       
  11.    uart_init(9600);            
  12.    Key_Init();                                 
  13.    delay_init();                               
  14.         printf("不连续I/O口矩阵键盘测试\r\n");
  15.    
  16.         while(1)
  17.         {                       
  18.         Key_Test();               
  19.     }
  20. }
复制代码
  1. #include "key4_4.h"
  2. #include "delay.h"
  3. #include "sys.h"
  4. //8个引脚 4个为行 4个为列
  5. //行输出端口定义
  6. #define X1_GPIO_PORT GPIOA           
  7. #define X2_GPIO_PORT GPIOA   
  8. #define X3_GPIO_PORT GPIOA           
  9. #define X4_GPIO_PORT GPIOB
  10. //列输入端口定义
  11. #define Y1_GPIO_PORT GPIOB           
  12. #define Y2_GPIO_PORT GPIOB   
  13. #define Y3_GPIO_PORT GPIOB           
  14. #define Y4_GPIO_PORT GPIOB

  15. //行输出引脚定义
  16. #define X1_GPIO_PIN GPIO_Pin_11
  17. #define X2_GPIO_PIN GPIO_Pin_12
  18. #define X3_GPIO_PIN GPIO_Pin_15
  19. #define X4_GPIO_PIN GPIO_Pin_3

  20. //列输入引脚定义
  21. #define Y1_GPIO_PIN GPIO_Pin_4
  22. #define Y2_GPIO_PIN GPIO_Pin_5
  23. #define Y3_GPIO_PIN GPIO_Pin_6
  24. #define Y4_GPIO_PIN GPIO_Pin_7

  25. //行输出时钟定义
  26. #define X1_RCC RCC_APB2Periph_GPIOA
  27. #define X2_RCC RCC_APB2Periph_GPIOA
  28. #define X3_RCC RCC_APB2Periph_GPIOA
  29. #define X4_RCC RCC_APB2Periph_GPIOB

  30. //列输入时钟定义
  31. #define Y1_RCC RCC_APB2Periph_GPIOB
  32. #define Y2_RCC RCC_APB2Periph_GPIOB
  33. #define Y3_RCC RCC_APB2Periph_GPIOB
  34. #define Y4_RCC RCC_APB2Periph_GPIOB

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

  37. unsigned char Y1,Y2,Y3,Y4;
  38. void Key_Init(void)
  39. {
  40.    GPIO_InitTypeDef GPIO_InitStructure;   
  41.    RCC_APB2PeriphClockCmd(X1_RCC|X2_RCC|X3_RCC|X4_RCC|Y1_RCC|Y2_RCC|Y3_RCC|Y4_RCC|RCC_APB2Periph_AFIO, ENABLE);
  42.    GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
  43.        
  44. /*****************************4行输出*********************************************/
  45.    GPIO_InitStructure.GPIO_Pin =  X1_GPIO_PIN ;
  46.    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;         
  47.    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  48.    GPIO_Init(X1_GPIO_PORT, &GPIO_InitStructure);
  49.    
  50.    GPIO_InitStructure.GPIO_Pin =  X2_GPIO_PIN ;
  51.    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;         
  52.    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  53.    GPIO_Init(X2_GPIO_PORT, &GPIO_InitStructure);
  54.    
  55.    GPIO_InitStructure.GPIO_Pin =  X3_GPIO_PIN ;
  56.    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;         
  57.    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  58.    GPIO_Init(X3_GPIO_PORT, &GPIO_InitStructure);
  59.        
  60.    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;         
  61.    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  62.    GPIO_InitStructure.GPIO_Pin = X4_GPIO_PIN ;   
  63.    GPIO_Init(X4_GPIO_PORT, &GPIO_InitStructure);
  64.    
  65. /**************************************4列输入*************************************/
  66.    GPIO_InitStructure.GPIO_Pin =  Y1_GPIO_PIN ;   
  67.    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;         
  68.    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  69.    GPIO_Init(Y1_GPIO_PORT, &GPIO_InitStructure);       
  70.    
  71.    GPIO_InitStructure.GPIO_Pin =  Y2_GPIO_PIN ;   
  72.    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;         
  73.    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  74.    GPIO_Init(Y2_GPIO_PORT, &GPIO_InitStructure);       
  75.    
  76.    GPIO_InitStructure.GPIO_Pin =  Y3_GPIO_PIN ;   
  77.    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;         
  78.    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  79.    GPIO_Init(Y3_GPIO_PORT, &GPIO_InitStructure);       
  80.        
  81.    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;         
  82.    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  83.    GPIO_InitStructure.GPIO_Pin = Y4_GPIO_PIN;      
  84.    GPIO_Init(Y4_GPIO_PORT, &GPIO_InitStructure);       
  85. }

  86. int Key_Scan(void)
  87. {
  88.    uchar KeyVal;
  89.    GPIO_SetBits(X1_GPIO_PORT,X1_GPIO_PIN);  //先让X1输出高
  90.    GPIO_SetBits(X2_GPIO_PORT,X2_GPIO_PIN);  //先让X2输出高
  91.    GPIO_SetBits(X3_GPIO_PORT,X3_GPIO_PIN);  //先让X3输出高
  92.    GPIO_SetBits(X4_GPIO_PORT,X4_GPIO_PIN);  //先让X4输出高


  93.         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)  
  94.         return -1; //如果X1到X4全为零则没有按键按下  
  95.          else
  96.          {       
  97.             delay_ms(5);    //延时5ms去抖动
  98.          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)
  99.             return -1;
  100.          }
  101.          
  102.      GPIO_ResetBits(X1_GPIO_PORT,X1_GPIO_PIN);
  103.      GPIO_ResetBits(X2_GPIO_PORT,X2_GPIO_PIN);
  104.      GPIO_ResetBits(X3_GPIO_PORT,X3_GPIO_PIN);
  105.      GPIO_SetBits(X4_GPIO_PORT,X4_GPIO_PIN);
  106.      
  107.     Y1=GPIO_ReadInputDataBit(Y1_GPIO_PORT,Y1_GPIO_PIN);Y2=GPIO_ReadInputDataBit(Y2_GPIO_PORT,Y2_GPIO_PIN);
  108.     Y3=GPIO_ReadInputDataBit(Y3_GPIO_PORT,Y3_GPIO_PIN);Y4=GPIO_ReadInputDataBit(Y4_GPIO_PORT,Y4_GPIO_PIN);
  109.      if(Y1==1&&Y2==0&&Y3==0&&Y4==0)
  110.             KeyVal='*';
  111.      if(Y1==0&&Y2==1&&Y3==0&&Y4==0)
  112.             KeyVal=0;
  113.      if(Y1==0&&Y2==0&&Y3==0&&Y4==1)
  114.             KeyVal='D';
  115.      if(Y1==0&&Y2==0&&Y3==1&&Y4==0)
  116.             KeyVal='#';
  117.    
  118.      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);
  119.     //等待按键释放
  120.      GPIO_SetBits(X1_GPIO_PORT,X1_GPIO_PIN);
  121.      GPIO_ResetBits(X2_GPIO_PORT,X2_GPIO_PIN);
  122.      GPIO_ResetBits(X3_GPIO_PORT,X3_GPIO_PIN);
  123.      GPIO_ResetBits(X4_GPIO_PORT,X4_GPIO_PIN);
  124.    
  125.     Y1=GPIO_ReadInputDataBit(Y1_GPIO_PORT,Y1_GPIO_PIN);Y2=GPIO_ReadInputDataBit(Y2_GPIO_PORT,Y2_GPIO_PIN);
  126.     Y3=GPIO_ReadInputDataBit(Y3_GPIO_PORT,Y3_GPIO_PIN);Y4=GPIO_ReadInputDataBit(Y4_GPIO_PORT,Y4_GPIO_PIN);
  127.      if(Y1==1&&Y2==0&&Y3==0&&Y4==0)
  128.             KeyVal=1;
  129.      if(Y1==0&&Y2==1&&Y3==0&&Y4==0)
  130.             KeyVal=2;
  131.      if(Y1==0&&Y2==0&&Y3==1&&Y4==0)
  132.             KeyVal=3;
  133.      if(Y1==0&&Y2==0&&Y3==0&&Y4==1)
  134.             KeyVal='A';
  135.       
  136.       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);
  137.                

  138.      GPIO_ResetBits(X1_GPIO_PORT,X1_GPIO_PIN);
  139.      GPIO_SetBits(X2_GPIO_PORT,X2_GPIO_PIN);
  140.      GPIO_ResetBits(X3_GPIO_PORT,X3_GPIO_PIN);
  141.      GPIO_ResetBits(X4_GPIO_PORT,X4_GPIO_PIN);
  142.         
  143.      Y1=GPIO_ReadInputDataBit(Y1_GPIO_PORT,Y1_GPIO_PIN);Y2=GPIO_ReadInputDataBit(Y2_GPIO_PORT,Y2_GPIO_PIN);
  144.      Y3=GPIO_ReadInputDataBit(Y3_GPIO_PORT,Y3_GPIO_PIN);Y4=GPIO_ReadInputDataBit(Y4_GPIO_PORT,Y4_GPIO_PIN);
  145.      if(Y1==1&&Y2==0&&Y3==0&&Y4==0)
  146.             KeyVal=4;
  147.      if(Y1==0&&Y2==1&&Y3==0&&Y4==0)
  148.             KeyVal=5;
  149.      if(Y1==0&&Y2==0&&Y3==1&&Y4==0)
  150.             KeyVal=6;
  151.      if(Y1==0&&Y2==0&&Y3==0&&Y4==1)
  152.             KeyVal='B';
  153.    
  154.       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);
  155.                
  156.      GPIO_ResetBits(X1_GPIO_PORT,X1_GPIO_PIN);
  157.      GPIO_ResetBits(X2_GPIO_PORT,X2_GPIO_PIN);
  158.      GPIO_SetBits(X3_GPIO_PORT,X3_GPIO_PIN);
  159.      GPIO_ResetBits(X4_GPIO_PORT,X4_GPIO_PIN);   

  160.      Y1=GPIO_ReadInputDataBit(Y1_GPIO_PORT,Y1_GPIO_PIN);Y2=GPIO_ReadInputDataBit(Y2_GPIO_PORT,Y2_GPIO_PIN);
  161.      Y3=GPIO_ReadInputDataBit(Y3_GPIO_PORT,Y3_GPIO_PIN);Y4=GPIO_ReadInputDataBit(Y4_GPIO_PORT,Y4_GPIO_PIN);
  162.      if(Y1==1&&Y2==0&&Y3==0&&Y4==0)
  163.             KeyVal=7;
  164.      if(Y1==0&&Y2==1&&Y3==0&&Y4==0)
  165.             KeyVal=8;
  166.      if(Y1==0&&Y2==0&&Y3==1&&Y4==0)
  167.             KeyVal=9;
  168.      if(Y1==0&&Y2==0&&Y3==0&&Y4==1)
  169.             KeyVal='C';

  170.        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);
  171.           
  172.                 return KeyVal;
  173. }

  174. /************************************
  175.         按键表盘为:                1  2  3  A
  176.                                                         4  5  6  B
  177.                                                         7  8  9  C
  178.                                                         *  0  #  D
  179. ************************************/
  180. void Key_Test(void)
  181. {
  182.     int num;
  183.           num = Key_Scan();
  184.           switch(num)
  185.           {
  186.                 case 0: printf("0\n"); break;                                                                               
  187.                                 case 1: printf("1\n"); break;                                                                                 
  188.                                 case 2: printf("2\n"); break;                                                                               
  189.                                 case 3: printf("3\n"); break;                                                                               
  190.                                 case 4: printf("4\n"); break;                                                
  191.                                 case 5: printf("5\n"); break;                                                                               
  192.                                 case 6: printf("6\n"); break;                                                                               
  193.                                 case 7: printf("7\n"); break;                                                                        
  194.                                 case 8: printf("8\n"); break;                                                                                        
  195.                                 case 9: printf("9\n"); break;                                                                                                     
  196.                 case 'A': printf("A\n"); break;                                                                                                      
  197.                                 case 'B': printf("B\n"); break;                                                                              
  198.                                 case 'C': printf("C\n"); break;                                                                                                      
  199.                 case 'D': printf("D\n"); break;                                                                                                       
  200.                                 case '#': printf("#\n"); break;                                                                              
  201.                                 case '*': printf("*\n"); break;                                                                      
  202.       }
  203. }
复制代码

所有资料51hei提供下载:
可移植的不连续IO口矩阵键盘(接地).7z (214.93 KB, 下载次数: 8)

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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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