Embedded blue bridge cup key programming, press once and execute only once

Embedded blue bridge cup key programming

First of all, remember the clock IO port of the button.
KEY_RESET PA0
KEY_1 PA8
KEY_2 PB1
KEY_3 PB2

The button is initialized with a pull-up input, and
the IO is pulled up to a high level, so that when the button is pressed, the iO is directly grounded. The voltage of the iO port instantly becomes low. At
this time, judging whether the IO is low can determine whether the button is pressed.

void KEY_Init(void)
{
    
    
	GPIO_InitTypeDef GPIO_InitStrure;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB,ENABLE);
	
	GPIO_InitStrure.GPIO_Mode=GPIO_Mode_IPU;
	GPIO_InitStrure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_8;
	GPIO_InitStrure.GPIO_Speed=GPIO_Speed_10MHz;
	GPIO_Init(GPIOA,&GPIO_InitStrure);
	
	GPIO_InitStrure.GPIO_Pin=GPIO_Pin_1|GPIO_Pin_2;
	GPIO_Init(GPIOB,&GPIO_InitStrure);
	
}

We use a function that can directly read the value of the IO port. To
determine whether the IO is low, you can determine whether the button is pressed.
GPIO_ReadInputDataBit()
Remember that this function has a bit and don’t write it as GPIO_ReadInputDataBit().
At this time, we use a macro Define the iO port corresponding to the marked button

#define KEY_0     GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0);
#define KEY_1     GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_8);
#define KEY_2     GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1);
#define KEY_3     GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_2);

Then judge the level of the IO port

uint8_t KEY_Scan(void)
{
    
    
	 u8 key_value = 0xff;
	if (KEY_0 ==0) 
		key_value='0';
	else if (KEY_1 == 0)
		key_value='1';
	else if (KEY_2 == 0)
		key_value='2';
  else if (KEY_3 == 0)
		key_value='3';
  return key_value;
}

At this point, you must write the code of the button according to the actual situation. Whether to press and hold the
short press
or not to execute
it only once. Here is the result of pressing the execution.
Specifically, if the scan function and the execution function are separately pressed once, the scan is released and
executed. The execution of the function will be fed back to the original function.
If the release flag is not received, it will not be executed
. The complete code is as follows

#include "key.h"
#include "tim.h"
void KEY_Init(void)
{
    
    
	GPIO_InitTypeDef GPIO_InitStruct;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IPU;
	GPIO_InitStruct.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_8;
	GPIO_InitStruct.GPIO_Speed=GPIO_Speed_10MHz;
	GPIO_Init(GPIOA,&GPIO_InitStruct);

	
}
	u8 key_status = 1;//0ÊÇûÓа´Ï 1ÊÇ°´ÏÂÁË
u8 key_release = 1;//1°´ÏÂÖ®ºóËÉ¿ª ¿ÉÒÔ´¥·¢×´Ì¬ 0 ±íʾ²»ÄÜ´¥·¢×´Ì¬ 
u8 KEY_Scan(void)
{
    
    

	if((KEY_0&&KEY_1)!=1)
	{
    
    
		
			Delay_Ms(20);
	if((KEY_0&&KEY_1)!=1)
			{
    
    
				if(KEY_0==0)

					return '1';
				else if(KEY_1==0)

					return '2';
			}
		
	}
	else 
	{
    
    
		key_status = 1;
	}

}
void KEY_Read(void)
{
    
    
	u8 t;
	t=KEY_Scan();
	if(key_status)
	{
    
    

	    if(t=='1')
			{
    
    
			printf("°´¼ü1°´ÏÂÁË");
			key_status=0;
			}
			if(t=='2')
			printf("°´¼ü2°´ÏÂÁË");
		
	}
}

Guess you like

Origin blog.csdn.net/m0_46179894/article/details/108108228