STM32实现4X4矩阵按键

4x4矩阵按键在这里插入图片描述

u8 keycount;
u8 keysingle;
void keyInit_row(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;  	 
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 
  GPIO_Init(GPIOA, &GPIO_InitStructure);
	GPIO_Write(GPIOA,0x000f);
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void keyInit_column(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;  	 
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 
  GPIO_Init(GPIOA, &GPIO_InitStructure);
	GPIO_Write(GPIOA,0x00f0);
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
}
u8 scan(void)
{
	u16 GPIO_KEY;
	u8 KeyValue=0x20;
	keyInit_column();
	GPIO_KEY=GPIO_ReadInputData(GPIOA)&0x000f;
	if(GPIO_KEY!=0x0000)//读取按键是否按下
	{
		delay_ms(10);
		if(GPIO_KEY!=0x0000)//再次检测键盘是否按下
		{	

			keyInit_column();
			GPIO_KEY=GPIO_ReadInputData(GPIOA)&0x000f;
			switch(GPIO_KEY)
			{
				case(0X0008):	KeyValue=0;break;
				case(0X0004):	KeyValue=1;break;
				case(0X0002): KeyValue=2;break;
				case(0X0001):	KeyValue=3;break;
			}
			//测试行
			keyInit_row();
			GPIO_KEY=GPIO_ReadInputData(GPIOA)&0x00f0;
			switch(GPIO_KEY)
			{
				case(0X0080):	KeyValue=KeyValue+12;break;
				case(0X0040):	KeyValue=KeyValue+8;break;
				case(0X0020): KeyValue=KeyValue+4;break;
				case(0X0010):	KeyValue=KeyValue;break;
			}
		}
	}
	if(KeyValue != keycount)//实现点按和长按的功能 
	{						//第一次扫描时候keyvalue和keycount(长按值返回参数)不相等进入到此if中
		keysingle=KeyValue;
	}
	else //第二次扫描时keycount和keyvalue相等改变keysingle 
	{
		keysingle=0x20;
	}
	keycount=KeyValue;//一般常用的while会使系统全部消耗在while该方法实现的长按和点按可以使系统不必消耗在while中
	return KeyValue;
}

```c主函数测试代码 按键oled显示数字并且自加
u8 key;
extern u8 keycount;
extern u8 keysingle;
void IN_Num(void);
int main(void)
{ 


	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2
	uart_init(115200);		//初始化串口波特率为500000
	delay_init(84);  //初始化延时函数
	OLED_Init();
	
 	while(1)
	{	
	scan();
	if(keysingle == 1)
	{
		OLED_ShowNum(0,1,key++,5,16);
	}
	if(keycount == 2)
	{
		OLED_ShowNum(0,1,key++,5,16);
	}
	}		```

  • 1
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值