STM32——4X4矩形键盘

本文介绍了在STM32F407芯片上配置和使用4X4矩形键盘的步骤。首先,通过杜邦线将键盘连接到芯片的特定引脚,然后详细说明了引脚的选择及其在硬件原理图中的对应关系。接着,将8个引脚分为两组,并提供了配置引脚的代码。最后,提到了键盘扫描的实现,标志着4X4键盘的完整设置完成。
摘要由CSDN通过智能技术生成

4X4矩形键盘

准备

我选用的芯片是STM32F407的芯片,一个4X4的矩形键盘。
我们用杜邦线将键盘和芯片连接起来,因为我选引脚为了插起来方便,我选了以下引脚。在这里插入图片描述
选用了左边的引脚(4-18),这样我们刚好一排8个引脚,接上8根杜邦线。

然后根据硬件原理图,看看分别对应芯片的哪个引脚。
在这里插入图片描述
我们不难发现,其中的8个引脚分别是

/*
PD6 PD7 PC6 PC8
PC11 PE5 PA6 PG9
*/

当你做到这里,你离成功已经不远了。

配置引脚

这八个引脚我们要把他分成两组(上面,我已经分成上下两组,因为这有关获取键盘的数值的原理)。
下面是代码

void Keyborad_Init(void)
{
   
	GPIO_InitTypeDef  GPIO_InitStruct;
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);//GPIOD组时钟

	GPIO_InitStruct.GPIO_Pin	= GPIO_Pin_6;    			//引脚 6
	GPIO_InitStruct.GPIO_Mode	= GPIO_Mode_OUT; 			//输出模式
	GPIO_InitStruct.GPIO_OType	= GPIO_OType_PP; 			//推挽输出
	GPIO_InitStruct.GPIO_Speed	= GPIO_Speed_50MHz;			//输出速度
	GPIO_InitStruct.GPIO_PuPd	= GPIO_PuPd_UP;     		//上拉
	GPIO_Init(GPIOD, &GPIO_InitStruct);
	
	GPIO_InitStruct.GPIO_Pin	= GPIO_Pin_7;    			//引脚 7
	GPIO_InitStruct.GPIO_Mode	= GPIO_Mode_OUT; 			//输出模式
	GPIO_InitStruct.GPIO_OType	= GPIO_OType_PP; 			//推挽输出
	GPIO_InitStruct.GPIO_Speed	= GPIO_Speed_50MHz;			//输出速度
	GPIO_InitStruct.GPIO_PuPd	= GPIO_PuPd_UP;     		//上拉
	GPIO_Init(GPIOD, &GPIO_InitStruct);

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
	
	GPIO_InitStruct.GPIO_Pin	= GPIO_Pin_6|GPIO_Pin_8;    //引脚6 8
	GPIO_InitStruct.GPIO_Mode	= GPIO_Mode_OUT; 			//输出模式
	GPIO_InitStruct.GPIO_OType	= GPIO_OType_PP; 			//推挽输出
	GPIO_InitStruct.GPIO_Speed	= GPIO_Speed_50MHz;			//输出速度
	GPIO_InitStruct.GPIO_PuPd	= GPIO_PuPd_UP;     		//上拉
	GPIO_Init(GPIOC, &GPIO_InitStruct);

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);	//GPIOC组时钟
	
	GPIO_InitStruct.GPIO_Pin	= GPIO_Pin_11;    			//引脚11
	GPIO_InitStruct.GPIO_Mode	= GPIO_Mode_IN; 			//输入模式
	GPIO_InitStruct.GPIO_OType	= GPIO_OType_PP; 			//推挽输出
	GPIO_InitStruct.GPIO_Speed	= GPIO_Speed_50MHz;			//输出速度
	GPIO_InitStruct.GPIO_PuPd	= GPIO_PuPd_UP;     		//下拉
	GPIO_Init(GPIOC, &GPIO_InitStruct);

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);	//GPIOE组时钟
	
	GPIO_InitStruct.GPIO_Pin	= GPIO_Pin_5;    			//引脚5
	GPIO_InitStruct.GPIO_Mode	= GPIO_Mode_IN; 			//输入模式
	GPIO_InitStruct.GPIO_OType	= GPIO_OType_PP; 			//推挽输出
	GPIO_InitStruct.GPIO_Speed	= GPIO_Speed_50MHz;			//输出速度
	GPIO_InitStruct.GPIO_PuPd	= GPIO_PuPd_UP;     		//下拉
	GPIO_Init(
  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值