久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

標題: STM32 4X4按鍵矩陣的小程序 [打印本頁]

作者: MrHuan991    時間: 2019-2-16 04:28
標題: STM32 4X4按鍵矩陣的小程序
前篇帖子寫了基本的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)








歡迎光臨 (http://m.zg4o1577.cn/bbs/) Powered by Discuz! X3.1
主站蜘蛛池模板: 91手机看片 | 午夜影院在线免费观看 | 999热视频 | av网站观看| 亚洲天堂男人 | 日本久久一区二区 | 婷婷免费视频 | 91精品国产色综合久久不卡98 | 特黄aaaaaaaaa真人毛片 | 91偷拍视频| 国产精品久久久久久久 | 日韩理论在线 | 六月激情婷婷 | 日本理论片午伦夜理片在线观看 | 久久视频一区二区 | 中文字幕不卡在线观看 | 超碰国产在线 | 日本天堂网 | 黄色欧美视频 | 精品在线播放 | 国产一区在线看 | 国产精品日韩在线 | a毛片视频 | 色黄视频在线观看 | 久久免费观看视频 | 一本色道久久综合亚洲精品酒店 | 精品粉嫩小bbwbbwbbw | 成av人片一区二区三区久久 | 国产麻豆xxxvideo实拍 | 日韩高清在线播放 | 精品一区二区三 | 日本在线观看 | 日本一级做a爱片 | 成人午夜毛片 | 国产又粗又猛又黄又爽的视频 | 欧美资源在线 | 亚洲免费观看视频 | 亚洲视频在线播放 | 一区二区三区四区在线 | 91们嫩草伦理 | 色综合久久综合 |