• 4x4矩阵键盘扫描-华芯微特


    电路图:

    扫描原理:

    逐列扫描,先将第一列输出低,其他所有IO设置为上拉输入,读取IO值,高4位哪个为低,说明是当前列哪个按键按下。

    源码:

     1 /*****************************************************************************
     2  * FILE NAME   keypad.h 
     3  * DESCRIPTION:    
     4  * Copyright   : xxx Technologies Ltd.
     5  * All Rights  Reserved
     6  * Author: Milo lu
     7  * Copyright (c) 2021, PGST Technologies Ltd.
     8  *
     9  * All rights reserved.
    10 -* ****************************************************************************/
    11 #ifndef     __KEYPAD_H
    12 #define     __KEYPAD_H
    13 
    14 //--------------------- Keys Macro Define -----------------------
    15 #define     KEY_0            BIT0
    16 #define     KEY_1            BIT1
    17 #define     KEY_2            BIT2
    18 #define     KEY_3            BIT3
    19 #define     KEY_4            BIT4
    20 #define     KEY_5            BIT5
    21 #define     KEY_6            BIT6
    22 #define     KEY_7            BIT7
    23 #define     KEY_8            BIT8
    24 #define     KEY_9            BIT9
    25 #define     ASTERIK_KEY     BIT10
    26 #define     POUND_KEY       BIT11
    27 #define     ARM_KEY         BIT12
    28 #define     DISARM_KEY      BIT13
    29 #define     HOME_KEY        BIT14
    30 #define     SOS_KEY         BIT15
    31 
    32 
    33 void keypad_init(void);
    34 u16 keypad_scan(void);
    35 
    36 #endif
    37 
    38 //------------------------------ end of file ------------------------------
    keypad.h
      1 /*****************************************************************************
      2  * FILE NAME   keypad.c 
      3  * DESCRIPTION:    
      4  * Copyright   : xxxTechnologies Ltd.
      5  * All Rights  Reserved
      6  * Author: Milo lu
      7  * Copyright (c) 2021, PGST Technologies Ltd.
      8  *
      9  * All rights reserved.
     10 -* ****************************************************************************/
     11 //---------------- system --------------------------
     12 #include "Global.h"
     13 
     14 //---------------- Peripheral ----------------------
     15 #include "keypad.h"
     16 
     17 
     18 
     19 u16 g_key_value;
     20 
     21 /**
     22  * @brief  keypad_init
     23  */
     24 void keypad_init(void)
     25 {
     26     int i;
     27     for(i=0;i<8;i++)        //PB1-PB8 set input with pull-up
     28     {
     29         GPIO_Init(GPIOB,i+1,0,1,0,0);
     30     }
     31 }
     32 
     33 /**
     34  * @brief  keypad_scan
     35  * @return key value
     36  */
     37 u16 keypad_scan(void)
     38 {
     39     u8 io_state;
     40     
     41     u16 key_value = 0;
     42     
     43     //---------------------- Read [1,4,7,*] keys ---------------------------
     44     GPIO_Init(GPIOB,PIN1,1,0,0,0);                      //the first column 
     45     vTaskDelay(2);
     46     io_state = (GPIOB->IDR>>1)&0xF0;                    //get io state
     47     if(io_state!=0xF0)                                  //is any key press in the frist column?
     48     {
     49         vTaskDelay(20);                                 //debouncing
     50         io_state = (GPIOB->IDR>>1)&0xF0;                //get io state again
     51         if(io_state!=0xF0)                              //is any key press? [1,4,7,*]
     52         {
     53             if((io_state&BIT7)==0)
     54             {
     55                 key_value |= KEY_1;
     56             }
     57             if((io_state&BIT6)==0)
     58             {
     59                 key_value |= KEY_4;
     60             }
     61             if((io_state&BIT5)==0)
     62             {
     63                 key_value |= KEY_7;
     64             }
     65             if((io_state&BIT4)==0)
     66             {
     67                 key_value |= ASTERIK_KEY;
     68             }
     69         }
     70     }
     71     GPIO_Init(GPIOB,PIN1,0,1,0,0);                      //release,input with pull-up
     72     
     73     //---------------------- Read [2,5,8,0] keys ---------------------------
     74     GPIO_Init(GPIOB,PIN2,1,0,0,0);                      //the second column
     75     vTaskDelay(2);
     76     io_state = (GPIOB->IDR>>1)&0xF0;                    //get io state
     77     if(io_state!=0xF0)                                  //is any key press in the second column?
     78     {
     79         vTaskDelay(20);                                 //debouncing
     80         io_state = (GPIOB->IDR>>1)&0xF0;                //get io state again
     81         if(io_state!=0xF0)                              //is any key press? [2,5,8,0]
     82         {
     83             if((io_state&BIT7)==0)
     84             {
     85                 key_value |= KEY_2;
     86             }
     87             if((io_state&BIT6)==0)
     88             {
     89                 key_value |= KEY_5;
     90             }
     91             if((io_state&BIT5)==0)
     92             {
     93                 key_value |= KEY_8;
     94             }
     95             if((io_state&BIT4)==0)
     96             {
     97                 key_value |= KEY_0;
     98             }
     99         }
    100     }
    101     GPIO_Init(GPIOB,PIN2,0,1,0,0);                      //release,input with pull-up
    102     
    103     //---------------------- Read [3,6,9,#] keys ---------------------------
    104     GPIO_Init(GPIOB,PIN3,1,0,0,0);                      //the third column
    105     vTaskDelay(2);
    106     io_state = (GPIOB->IDR>>1)&0xF0;                    //get io state
    107     if(io_state!=0xF0)                                  //is any key press in the second column?
    108     {
    109         vTaskDelay(20);                                 //debouncing
    110         io_state = (GPIOB->IDR>>1)&0xF0;                //get io state again
    111         if(io_state!=0xF0)                              //is any key press? [3,6,9,#]
    112         {
    113             if((io_state&BIT7)==0)
    114             {
    115                 key_value |= KEY_3;
    116             }
    117             if((io_state&BIT6)==0)
    118             {
    119                 key_value |= KEY_6;
    120             }
    121             if((io_state&BIT5)==0)
    122             {
    123                 key_value |= KEY_9;
    124             }
    125             if((io_state&BIT4)==0)
    126             {
    127                 key_value |= POUND_KEY;
    128             }
    129         }
    130     }
    131     GPIO_Init(GPIOB,PIN3,0,1,0,0);                      //release,input with pull-up
    132     
    133     //---------------------- Read [arm,diarm,home,sos] keys ---------------------------
    134     GPIO_Init(GPIOB,PIN4,1,0,0,0);                      //the fourth column
    135     vTaskDelay(2);
    136     io_state = (GPIOB->IDR>>1)&0xF0;                    //get io state
    137     if(io_state!=0xF0)                                  //is any key press in the second column?
    138     {
    139         vTaskDelay(20);                                 //debouncing
    140         io_state = (GPIOB->IDR>>1)&0xF0;                //get io state again
    141         if(io_state!=0xF0)                              //is any key press? [arm,diarm,home,sos]
    142         {
    143             if((io_state&BIT7)==0)
    144             {
    145                 key_value |= ARM_KEY;
    146             }
    147             if((io_state&BIT6)==0)
    148             {
    149                 key_value |= DISARM_KEY;
    150             }
    151             if((io_state&BIT5)==0)
    152             {
    153                 key_value |= HOME_KEY;
    154             }
    155             if((io_state&BIT4)==0)
    156             {
    157                 key_value |= SOS_KEY;
    158             }
    159         }
    160     }
    161     GPIO_Init(GPIOB,PIN4,0,1,0,0);                      //release,input with pull-up
    162     
    163     return key_value;
    164 }
    165 
    166 
    167 
    168 
    169 
    170 
    171 
    172 
    173 
    174 
    175 
    176 //--------------------- end of file ------------------------------
    keypad.c

    仅供参考,请勿用于商业。谢谢

     

  • 相关阅读:
    BZOJ1093: [ZJOI2007]最大半连通子图
    BZOJ4033: [HAOI2015]树上染色
    BZOJ1977: [BeiJing2010组队]次小生成树 Tree
    BZOJ4944: [Noi2017]泳池
    BZOJ1269: [AHOI2006]文本编辑器editor
    BZOJ4596: [Shoi2016]黑暗前的幻想乡
    BZOJ1815: [Shoi2006]color 有色图
    BZOJ1488: [HNOI2009]图的同构
    BZOJ3527: [Zjoi2014]力
    Salazar Slytherin's Locket CodeForces
  • 原文地址:https://www.cnblogs.com/lumao1122-Milolu/p/15347731.html
Copyright © 2020-2023  润新知