当前位置:   article > 正文

STM32F4实现矩阵键盘_stm32f4和矩阵键盘

stm32f4和矩阵键盘

程序中所使用的矩阵键盘所接的引脚为PC4-PC5、PF11-PF15和PG0,接线方法为常规矩阵键盘的接法,PC4、PC5、PF11、PF12为行线PF13、PF14、PF15、PG0为列线。


矩阵键盘IO口


u8 check_Key(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        u8 cord_h=0XFF,cord_l=0XFF;  //h为行线 l为列线
        u8 Val = 0xFF;

        /* 行线 推挽输出 */
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT;
        GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4|GPIO_Pin_5;
        GPIO_InitStructure.GPIO_Speed=GPIO_Speed_2MHz;
        GPIO_Init(GPIOC,&GPIO_InitStructure);

        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11|GPIO_Pin_12;
        GPIO_Init(GPIOF,&GPIO_InitStructure);

        /* 列线 上拉输入 */
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN;
        GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
        GPIO_Init(GPIOG,&GPIO_InitStructure);

        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
        GPIO_Init(GPIOF,&GPIO_InitStructure);

        /* 行线输出全部设置为0 */
        GPIO_WriteBit(GPIOC, GPIO_Pin_4|GPIO_Pin_5, Bit_RESET);
        GPIO_WriteBit(GPIOF, GPIO_Pin_11|GPIO_Pin_12, Bit_RESET);
        delay_us(1);

        /* 读入列线值 读入的值分别存入低四位 高四位全部为0 */
        cord_l&=(u8)((GPIO_ReadInputDataBit(GPIOF, GPIO_Pin_13)<<0)|
                     (GPIO_ReadInputDataBit(GPIOF, GPIO_Pin_14)<<1)|
                     (GPIO_ReadInputDataBit(GPIOF, GPIO_Pin_15)<<2)|
                     (GPIO_ReadInputDataBit(GPIOG, GPIO_Pin_0)<<3));
        if(cord_l!=0X0F)
        {
            delay_ms(10);       //消抖 延时后再读一次
            cord_l&=(u8)((GPIO_ReadInputDataBit(GPIOF, GPIO_Pin_13)<<0)|
                         (GPIO_ReadInputDataBit(GPIOF, GPIO_Pin_14)<<1)|
                         (GPIO_ReadInputDataBit(GPIOF, GPIO_Pin_15)<<2)|
                         (GPIO_ReadInputDataBit(GPIOG, GPIO_Pin_0)<<3));
            if(cord_l!=0X0F)
            {
                    /* 交换输入信号读取行线值 */

                    /* 列线 推挽输出 */
                    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT;
                    GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;

                    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
                    GPIO_Init(GPIOG,&GPIO_InitStructure);

                    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
                    GPIO_Init(GPIOF,&GPIO_InitStructure);

                        /* 行线 上拉输入 */                   
                    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN;
                    GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;

                    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4|GPIO_Pin_5;
                    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_2MHz;
                    GPIO_Init(GPIOC,&GPIO_InitStructure);

                    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11|GPIO_Pin_12;
                    GPIO_Init(GPIOF,&GPIO_InitStructure);

                    /* 列线输出全部设置为0 */
                    GPIO_WriteBit(GPIOG, GPIO_Pin_0, Bit_RESET);
                    GPIO_WriteBit(GPIOF, GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15, Bit_RESET);
                    delay_ms(2);
                    /* 读入行线值 */
                    cord_h&=(u8)((GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_4)<<3)| 
                                 (GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_5)<<2)|
                                 (GPIO_ReadInputDataBit(GPIOF, GPIO_Pin_11)<<1)|
                                 (GPIO_ReadInputDataBit(GPIOF, GPIO_Pin_12)<<0));

                    Val=~(cord_h<<4|cord_l); //取反 便于分析Val对应的按键
                    return Val;
            }

        }
        return ~Val;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/302219
推荐阅读
相关标签
  

闽ICP备14008679号