找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1111|回复: 0
打印 上一主题 下一主题
收起左侧

STM32单片机矩阵键盘

[复制链接]
跳转到指定楼层
楼主
ID:949309 发表于 2022-4-28 15:54 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include "matrix_key.h"
#include "stm32f10x.h"
#include "delay.h"

void KEYPad_Init1(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE);//开启时钟,使能GPIOB时钟
        
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;        //上拉输入 B0-B3
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOF, &GPIO_InitStructure);
        
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;        //下拉输入 F4-B7
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOF, &GPIO_InitStructure);
}

void KEYPad_Init2(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE);//开启时钟,使能GPIOB时钟
        
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;        //下拉输入 B0-B3
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOF, &GPIO_InitStructure);
        
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;        //上拉输入 B4-B7
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOF, &GPIO_InitStructure);
}

int KEYPAD_Scan()
{
        static u16 keyvalue = 0;
        u32 readvalue = 0;
        KEYPad_Init1();
        readvalue = GPIO_ReadInputData(GPIOF);
        readvalue &= 0x00ff;
        //return readvalue;

        
        
        if(readvalue != 0x000f)
        {
                delay_ms(8);//消抖10ms
                if(readvalue != 0x000f)
                {
                        readvalue = GPIO_ReadInputData(GPIOF);//再次读取状态
                        readvalue &= 0x00ff;
                        //return readvalue;
                        switch(readvalue)
                        {
                                case (0x001f):keyvalue = 0;break;
                                case (0x002f):keyvalue = 1;break;
                                case (0x004f):keyvalue = 2;break;
                                case (0x008f):keyvalue = 3;break;
                        }
                        //return readvalue;
                        KEYPad_Init2();
                        delay_ms(5);
                        readvalue = GPIO_ReadInputData(GPIOF);//再次读取状态
                        readvalue |= 0x00f0;
                        readvalue &= 0x00ff;
                        //return readvalue;
                        switch(readvalue)
                        {
                                case (0x00f1):keyvalue += 0;break;
                                case (0x00f2):keyvalue += 4;break;
                                case (0x00f4):keyvalue += 8;break;
                                case (0x00f8):keyvalue += 12;break;
                        }
                        
                        while(readvalue != 0x00f0)//检测按键松开
                        {
                                readvalue = GPIO_ReadInputData(GPIOF);//再次读取状态
                                readvalue |= 0x00f0;
                                readvalue &= 0x00ff;
                        }
                        //return readvalue;
                }
        }
        return keyvalue;
}


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表