关于STM32F103型号的矩阵按键的程序书写

博主参加电子大赛编写STM32矩阵按键程序,起初换了几个矩阵按键都不好使,调试许久未发现问题。后按自己思路重写程序,bug消失且运行良好,还贴出程序纪念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近参加电子大赛要写一个矩阵按键,刚开始写了好长时间换了好几个矩阵按键都不好使,但是 我感觉我的程序和原理都没有错,调试了好久也没有发现问题在哪?我按照我自己的思路把程序重写了一遍bug消失了,程序很好的运行。程序贴出来,纪念一下。

#include "keypad.h"
#include "stm32f10x.h"
#include "delay.h"
/*************************************************************************/
/*************************************************************************/
/*************************************************************************/

/*本文件是矩阵按键功能文件,使用方法是:直接调KEYPAD_Scan函数得到一个返回值*/

/*************************************************************************/
/*************************************************************************/
/*************************************************************************/

//函数名:KEYPad_Init1
//参数:无
//功能:行检测初始化
//返回值;无
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;	//下拉输入 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);
}

//函数名:KEYPad_Init2
//参数:无
//功能:列检测初始化
//返回值:无
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);
}

//函数名:扫描函数
//入口参数:无
//返回值:键值
//功能:矩阵按键扫描,返回一个键值
u16 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;
}

 

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值