当前位置:   article > 正文

STM32实现4X4矩阵按键_c8t6和矩阵按键怎么连

c8t6和矩阵按键怎么连

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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/302166
推荐阅读
相关标签
  

闽ICP备14008679号