STM32F4实现矩阵键盘

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


STM32F4实现矩阵键盘_第1张图片


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;
}

你可能感兴趣的:(STM32)