STM32 study notes (5)

STM32F103ZET6 key input experiment



Preface

The learning of STM32 can be divided into 3 versions.
1. Register version
2. Library function version
3. HAL library version
Due to personal reasons, I choose the library function version to learn STM32.


Tip: Problems such as software installation will not be explained! ! !

1. Schematic

The schematic diagram shows that PA0 corresponds to WK_UP, PE3 corresponds to KEY1, and PE4 corresponds to KEY0.
Insert picture description here
Insert picture description here

Second, the program source code

1.key.h

code show as below:

#ifndef _KEY_H
#define _KEY_H

#include "sys.h"

//#define KEY0 PEin(4)	//KEY0 PE4
//#define KEY1 PEin(3)	//KEY1 PE3
//#define WK_UP PAin(0)	//WK_UP PA0

#define KEY0 GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4)	//读取KEY0
#define KEY1 GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_3)	//读取KEY1
#define WK_UP GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)	//读取WK_UP

#define KEY0_PRES 1	//对应键值
#define KEY1_PRES 2	//对应键值
#define WK_UP_PRES 3	//对应键值

void KEY_Init(void);
u8 KEY_SCAN(u8 mode);

#endif

2.key.c

code show as below:

#include "key.h"
#include "stm32f10x.h"
#include "sys.h"
#include "delay.h"

void KEY_Init(void)
{
    
    
	GPIO_InitTypeDef GPIO_Initstr;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOE, ENABLE);//时钟使能
	
	GPIO_Initstr.GPIO_Mode=GPIO_Mode_IPD;//下拉
	GPIO_Initstr.GPIO_Pin=GPIO_Pin_0;
	GPIO_Initstr.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_Initstr);
	
	GPIO_Initstr.GPIO_Mode=GPIO_Mode_IPU;//上拉
	GPIO_Initstr.GPIO_Pin=GPIO_Pin_4|GPIO_Pin_3;
	GPIO_Initstr.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOE, &GPIO_Initstr);
}
u8 KEY_SCAN(u8 mode)
{
    
    
	static u8 key_up=1;
	if(mode==1) key_up=1;
	if(key_up&&(KEY0==0||KEY1==0||WK_UP==1))//按下按键,标志位置0
	{
    
    
		delay_ms(10);
		key_up=0;
		if(KEY0==0) return KEY0_PRES;
		else if(KEY1==0) return KEY1_PRES;
		else if(WK_UP==1) return WK_UP_PRES;
	}
	else if(KEY0==1&&KEY1==1&&WK_UP==0) key_up=1;//松开按键,标志位置1
	return 0;
}

3.main.c

code show as below:

#include "stm32f10x.h"
#include "led.h"
#include "delay.h"
#include "beep.h"
#include "key.h"

#define LED1 PBout(5)
#define LED2 PEout(5)
#define Beep PBout(8)

int main(void)
{
    
    
	u8 key;
	delay_init();
  LED_Init();
	Beep_Init();
	KEY_Init();
	while(1)
	{
    
    	
    key=KEY_SCAN(0);
		if(key)
		{
    
    
			switch(key)
			{
    
    
				case KEY0_PRES:	//键值为KEY0_PRES,则控制LED0亮灭
					LED1=!LED1;
				  break;
				case KEY1_PRES:	//键值为KEY1_PRES,则控制LED1亮灭
					LED2=!LED2;
					break;
				case WK_UP_PRES:	//键值为WK_UP_PRES,则控制蜂鸣器开关
					Beep=!Beep;	
					break;
			}
		}
		else delay_ms(10);
	}
}


3. Experimental results

When the KEY_UP button is pressed, the buzzer switch is controlled.
When the KEY1 button is pressed, the control LED2 turns on and off.
When the KEY0 button is pressed, the LED1 turns on and off.


to sum up

Persistence is victory! ! !

The program implementation steps are as follows:
1. Clock enable
2. GPIO initialization
3. Read button status, that is, read high and low levels

Guess you like

Origin blog.csdn.net/weixin_44935259/article/details/112549037