找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

帖子
查看: 1600|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

這個(gè)接地才能用的STM32矩陣按鍵如何改成上拉電阻可用的?

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:405123 發(fā)表于 2019-12-13 08:55 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
有沒(méi)有大佬能不能幫我把這個(gè)接地才能用的矩陣按鍵改成上拉電阻可用的!

單片機(jī)源程序如下:
  1. #include "key4_4.h"
  2. #include "delay.h"
  3. #include "sys.h"
  4. //平臺(tái):STM32F103
  5. //實(shí)驗(yàn)名稱:不連續(xù)I/O矩陣鍵盤實(shí)驗(yàn)  
  6. //Author:GXNU_LPK_20512700***
  7. //使用說(shuō)明:矩陣鍵盤所用引腳都已經(jīng)用宏定義定義好了,移植只需根據(jù)實(shí)際需要在key4_4.c中修改對(duì)應(yīng)的時(shí)鐘、引腳和端口即可,其余都不用修改。
  8. //矩陣鍵盤所用的8個(gè)引腳可連續(xù)可不連續(xù),看實(shí)際需要和個(gè)人愛(ài)好自己定義。
  9. int main(void)
  10. {       
  11.    uart_init(9600);            
  12.    Key_Init();                                 
  13.    delay_init();                               
  14.         printf("不連續(xù)I/O口矩陣鍵盤測(cè)試\r\n");
  15.    
  16.         while(1)
  17.         {                       
  18.         Key_Test();               
  19.     }
  20. }
復(fù)制代碼
  1. #include "key4_4.h"
  2. #include "delay.h"
  3. #include "sys.h"
  4. //8個(gè)引腳 4個(gè)為行 4個(gè)為列
  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. //行輸出時(shí)鐘定義
  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. //列輸入時(shí)鐘定義
  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. //移植代碼只需要修改上面的端口和引腳和時(shí)鐘即可,下面的代碼不用修改。
  36. //矩陣鍵盤所用的8個(gè)引腳可連續(xù)可不連續(xù),看實(shí)際需要和個(gè)人愛(ài)好自己定義。

  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全為零則沒(méi)有按鍵按下  
  95.          else
  96.          {       
  97.             delay_ms(5);    //延時(shí)5ms去抖動(dòng)
  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. }
復(fù)制代碼

所有資料51hei提供下載:
可移植的不連續(xù)IO口矩陣鍵盤(接地).7z (214.93 KB, 下載次數(shù): 8)

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表