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;
}
  • 7
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
STM32标准库没有提供矩阵键盘的驱动函数,但可以利用GPIO输入模式和软件扫描的方式实现矩阵键盘的读取。以下是一个简单的示例代码: ```c #include "stm32f4xx.h" #define ROWS 4 #define COLS 4 GPIO_TypeDef* row_ports[ROWS] = {GPIOA, GPIOA, GPIOA, GPIOA}; uint16_t row_pins[ROWS] = {GPIO_Pin_0, GPIO_Pin_1, GPIO_Pin_2, GPIO_Pin_3}; GPIO_TypeDef* col_ports[COLS] = {GPIOA, GPIOA, GPIOA, GPIOA}; uint16_t col_pins[COLS] = {GPIO_Pin_4, GPIO_Pin_5, GPIO_Pin_6, GPIO_Pin_7}; uint8_t key_map[ROWS][COLS] = { {'1', '2', '3', 'A'}, {'4', '5', '6', 'B'}, {'7', '8', '9', 'C'}, {'*', '0', '#', 'D'} }; void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); // 配置行引脚为输入模式 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; for (int i = 0; i < ROWS; i++) { GPIO_InitStructure.GPIO_Pin = row_pins[i]; GPIO_Init(row_ports[i], &GPIO_InitStructure); } // 配置列引脚为输出模式 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; for (int i = 0; i < COLS; i++) { GPIO_InitStructure.GPIO_Pin = col_pins[i]; GPIO_Init(col_ports[i], &GPIO_InitStructure); } } char get_key(void) { char key = 0; for (int i = 0; i < ROWS; i++) { // 将行引脚拉低,按键才能连接到列引脚 GPIO_ResetBits(row_ports[i], row_pins[i]); for (int j = 0; j < COLS; j++) { if (GPIO_ReadInputDataBit(col_ports[j], col_pins[j]) == RESET) { key = key_map[i][j]; break; } } // 将行引脚恢复高电平 GPIO_SetBits(row_ports[i], row_pins[i]); if (key) { break; } } return key; } int main(void) { GPIO_Configuration(); while (1) { char key = get_key(); if (key) { // 处理按键事件 } } } ``` 上述代码中,ROW和COL分别表示矩阵键盘的行数和列数,row_ports和col_ports分别存储行引脚和列引脚所在的GPIO端口,row_pins和col_pins分别存储行引脚和列引脚所在的GPIO引脚。key_map是一个二维数组,用于将矩阵键盘的行列映射到实际的按键字符。在get_key函数中,通过软件扫描的方式读取矩阵键盘的按键状态,并返回对应的按键字符。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值