STM32矩阵键盘4*4扫描程序(无延时消抖,测试稳定)

直接上源码

key.h文件

#ifndef __KEY_H
#define __KEY_H	 
#include "sys.h"



#define Read_Lie1 GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_0)
#define Read_Lie2 GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_1)
#define Read_Lie3 GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_2)
#define Read_Lie4 GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_3)



void KEY_Init(void);//IO初始化
u8 KEY_Scan(void);  	//按键扫描函数	
void Key_Text(void);
#endif

key.c文件

#include "key.h"
#include "sys.h" 
#include "delay.h"
#include "usart.h"
#include "lcd.h"


u8 tem;


//按键初始化函数
void KEY_Init(void) 
{ 
 	GPIO_InitTypeDef GPIO_InitStructure;
 
 	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC|RCC_AHB1Periph_GPIOE,ENABLE);

	GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; 
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
 	GPIO_Init(GPIOC, &GPIO_InitStructure);


	GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;   
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOE, &GPIO_InitStructure);

}


u8 KEY_Scan(void)//扫描函数
{	
	static u8 key_flag1=1,key_flag2=1,key_flag3=1,key_flag4=1;     //按键松开标志
	u8 KeyVal;
	
	
  GPIO_Write(GPIOE,(( GPIOE->ODR & 0xFFC3 )|0x0004));	//0001
	if(key_flag1&&(Read_Lie1==1||Read_Lie2==1||Read_Lie3==1||Read_Lie4==1))
	{
		 key_flag1=0;
		     if (Read_Lie1==1){ KeyVal= 4; printf("key:%d\r\n",KeyVal);}
		else if (Read_Lie2==1) {KeyVal= 8; printf("key:%d\r\n",KeyVal);}
		else if (Read_Lie3==1) {KeyVal= 12; printf("key:%d\r\n",KeyVal);}
		else if (Read_Lie4==1) {KeyVal= 16; printf("key:%d\r\n",KeyVal);}
	}
	else if(Read_Lie1==0&&Read_Lie2==0&&Read_Lie3==0&&Read_Lie4==0)key_flag1=1;
	else return 0;
	

		GPIO_Write(GPIOE,(( GPIOE->ODR & 0xFFC3 )|0x0008));//0010
	if(key_flag2&&(Read_Lie1==1||Read_Lie2==1||Read_Lie3==1||Read_Lie4==1))
	{
		 key_flag2=0;
		     if (Read_Lie1==1){ KeyVal= 3; printf("key:%d\r\n",KeyVal);}
		else if (Read_Lie2==1) {KeyVal= 7; printf("key:%d\r\n",KeyVal);}
		else if (Read_Lie3==1) {KeyVal= 11; printf("key:%d\r\n",KeyVal);}
		else if (Read_Lie4==1) {KeyVal= 15; printf("key:%d\r\n",KeyVal);}
	}
	else if(Read_Lie1==0&&Read_Lie2==0&&Read_Lie3==0&&Read_Lie4==0)key_flag2=1;
	else return 0;
	


	GPIO_Write(GPIOE,(( GPIOE->ODR & 0xFFC3 )|0x0010));//0100
	if(key_flag3&&(Read_Lie1==1||Read_Lie2==1||Read_Lie3==1||Read_Lie4==1))
	{
		key_flag3=0;
		     if (Read_Lie1==1){ KeyVal= 2; printf("key:%d\r\n",KeyVal);}
		else if (Read_Lie2==1) {KeyVal= 6; printf("key:%d\r\n",KeyVal);}
		else if (Read_Lie3==1) {KeyVal= 10; printf("key:%d\r\n",KeyVal);}
		else if (Read_Lie4==1) {KeyVal= 14; printf("key:%d\r\n",KeyVal);}
	}
	else if(Read_Lie1==0&&Read_Lie2==0&&Read_Lie3==0&&Read_Lie4==0)key_flag3=1;
	else return 0;
	
	
	
				
	 GPIO_Write(GPIOE,(( GPIOE->ODR & 0xFFC3 )|0x0020));//1000
   if(key_flag4&&(Read_Lie1==1||Read_Lie2==1||Read_Lie3==1||Read_Lie4==1))
	 {
		 key_flag4=0;
		     if (Read_Lie1==1){ KeyVal= 1; printf("key:%d\r\n",KeyVal);}
		else if (Read_Lie2==1) {KeyVal= 5; printf("key:%d\r\n",KeyVal);}
		else if (Read_Lie3==1) {KeyVal= 9; printf("key:%d\r\n",KeyVal);}
		else if (Read_Lie4==1) {KeyVal= 13; printf("key:%d\r\n",KeyVal);}
	 }
	 else if(Read_Lie1==0 &&Read_Lie2==0 &&Read_Lie3==0 &&Read_Lie4==0 )key_flag4=1;
	 else return 0;
	 
	 

	
	 return KeyVal;

}


void Key_Text(void)
{
		switch(tem)
		{
			case 1:LCD_Clear(WHITE);       break;
			case 2:LCD_Clear(BLACK);       break;
			case 3:LCD_Clear(BLUE);        break;
			case 4:LCD_Clear(RED);         break;
			case 5:LCD_Clear(MAGENTA);     break;
			case 6:LCD_Clear(GREEN);       break;
			case 7:LCD_Clear(CYAN);        break;
			case 8:LCD_Clear(YELLOW);      break;
			case 9:LCD_Clear(BRRED);       break;
			case 10:LCD_Clear(GRAY);       break;
			case 11:LCD_Clear(LGRAY);      break;
			case 12:LCD_Clear(BROWN);      break;
			case 13:LCD_Clear(DARKBLUE);   break;
			case 14:LCD_Clear(LIGHTBLUE);  break;
			case 15:LCD_Clear(LIGHTGREEN); break;
			case 16:LCD_Clear(RED);        break;			
		}	
	
}
	

 

 

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页