STM32CubeMX Series|Key Input

Key input

1. Introduction

This experiment introduces how to use the IO port of STM32F103 as input. When the IO port is used as an input, the status of the IO port is read by reading the content of the IDR.
This experiment uses the 4 buttons on the board to control the on and off of the two LEDs and the buzzer. Among them, KEY_UP controls the buzzer; K1 controls D1, press once to turn on and then press to turn off; K2 controls D2, press once to turn on and then press to turn off; K3 controls D1 and D2 at the same time, press once to flip their states once

2. Hardware design

The hardware resources used are:

  • Indicator lights D1, D2
  • Buzzer BUZ
  • 4 buttons: KEY_UP, K1, K2, K3

The hardware connection schematic diagram is as follows. Note that K1, K2, K3 are active low, and KEY_UP is active high, and there are no pull-up and pull-down resistors externally, so it is necessary to set up and pull-down in STM32F1;
STM32F103 chip single IO port is the largest The output current is 25mA, and the driving current of the buzzer is about 30mA. The maximum output current of the whole chip is 150mA. If 30mA is consumed on the buzzer, the current of the other IO ports and peripherals of the chip will be relatively tight. Therefore, the IO port is not used to drive the buzzer directly, but the current is amplified by the transistor and then the buzzer is driven, so that the IO port only needs to provide less than 1mA current to control the buzzer

Insert picture description here

3. Software design

3.1 STM32CubeMX settings
  • RCC set external HSE, clock is set to 72M
  • PC0 and PC2 are set to GPIO push-pull output mode, pull-up, high-speed, and the default output level is high
  • PB5 is set to GPIO push-pull output, high-speed mode
  • PA0 is set to GPIO input mode, pull-down mode; PE2/PE3/PE4 is set to GPIO input mode, pull-up mode

Insert picture description here

  • Enter the project name, select the project path (no Chinese), select MDK-ARM V5; check Generated periphera initialization as a pair of'.c/.h' files per IP; click GENERATE CODE to generate the project code
3.2 MDK-ARM software programming
  • You can see the initialization function of PC0/PC1/PB5/PA0/PE2/PE3/PE4 in the gpio.c file
void MX_GPIO_Init(void)
{
    
    
  GPIO_InitTypeDef GPIO_InitStruct = {
    
    0};
  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOE_CLK_ENABLE();		//开启GPIOE时钟
  __HAL_RCC_GPIOC_CLK_ENABLE();		//开启GPIOC时钟
  __HAL_RCC_GPIOA_CLK_ENABLE();		//开启GPIOA时钟
  __HAL_RCC_GPIOB_CLK_ENABLE();		//开启GPIOB时钟
  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOC, LED1_Pin|LED2_Pin, GPIO_PIN_SET);	//LED1/LED2默认输出是电平为高电平
  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(BUZ_GPIO_Port, BUZ_Pin, GPIO_PIN_RESET);	//BUZ默认输出是电平为低电平	
  /*Configure GPIO pins : PEPin PEPin PEPin */
  GPIO_InitStruct.Pin = K_LEFT_Pin|K_DOWN_Pin|K_RIGHT_Pin;		//PE2/PE3/PE4
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;						//输入
  GPIO_InitStruct.Pull = GPIO_PULLUP;							//上拉
  HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
  /*Configure GPIO pins : PCPin PCPin */
  GPIO_InitStruct.Pin = LED1_Pin|LED2_Pin;						//PC0/PC1
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;					//推挽输出
  GPIO_InitStruct.Pull = GPIO_PULLUP;							//上拉
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;					//高速
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  /*Configure GPIO pin : PtPin */
  GPIO_InitStruct.Pin = K_UP_Pin;								//PA0
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;						//输入
  GPIO_InitStruct.Pull = GPIO_PULLDOWN;							//下拉
  HAL_GPIO_Init(K_UP_GPIO_Port, &GPIO_InitStruct);
  /*Configure GPIO pin : PtPin */
  GPIO_InitStruct.Pin = BUZ_Pin;								//PB5
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;					//推挽输出
  GPIO_InitStruct.Pull = GPIO_NOPULL;							//无上下拉
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;					//高速
  HAL_GPIO_Init(BUZ_GPIO_Port, &GPIO_InitStruct);
}
  • Create a key.c file in the application file directory of the project and write key-related functions
/***********************************************************
*函 数 名: KEY_Scan(uint8_t mode)
*功    能: 按键处理函数
*形    参: mode:0 不支持连续按;1 支持连续按
*返 回 值: 返回按键值   0:表示未有按键按下
*					  1:KEY_RIGHT按下   2:KEY_DOWN按下
*					  3:KEY_LEFT按下    4:KEY_UP按下
***********************************************************/
uint8_t KEY_Scan(uint8_t mode)
{
    
    
	static uint8_t key = 1;		//按键松开标志
	if(mode == 1){
    
    				//支持连按
		key = 1;
	}
	
	if(key&&(KEY_UP==1||KEY_DOWN==0||KEY_LEFT==0||KEY_RIGHT==0)){
    
    
		HAL_Delay(10);			//按键消抖
		key = 0;
		if(KEY_UP==1)			return K_UP_PRES;
		else if(KEY_DOWN==0)	return K_DOWN_PRES; 
		else if(KEY_LEFT==0)	return K_LEFT_PRES; 
		else if(KEY_RIGHT==0)	return K_RIGHT_PRES; 
	}
	else if(KEY_UP==0&&KEY_DOWN==1&&KEY_LEFT==1&&KEY_RIGHT==1){
    
    
		key = 1;
	}
	
	return 0;
}
  • Create a key.h file in the header file directory of the project and write related statements
#ifndef _KEY_H_
#define _KEY_H_

#include "stm32f1xx.h"

//操作HAL库函数读取IO口状态
#define KEY_RIGHT	HAL_GPIO_ReadPin(GPIOE,GPIO_PIN_4)
#define KEY_DOWN    HAL_GPIO_ReadPin(GPIOE,GPIO_PIN_3)
#define KEY_LEFT    HAL_GPIO_ReadPin(GPIOE,GPIO_PIN_2)
#define KEY_UP      HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0)
//定义按键按下时的键值
#define KEY_RIGHT_PRES 	1
#define KEY_DOWN_PRES	2
#define KEY_LEFT_PRES	3
#define KEY_UP_PRES   	4

uint8_t KEY_Scan(uint8_t mode);

#endif
  • Write related programs in the main.c function
int main(void)
{
    
    
  /* USER CODE BEGIN 1 */
	uint8_t key;
  /* USER CODE END 1 */
  /* MCU Configuration--------------------------------------------------------*/
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
  /* USER CODE BEGIN Init */
  /* USER CODE END Init */
  /* Configure the system clock */
  SystemClock_Config();
  /* USER CODE BEGIN SysInit */
  /* USER CODE END SysInit */
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  /* USER CODE BEGIN 2 */
  /* USER CODE END 2 */
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    
    
		key = KEY_Scan(0);
		switch(key){
    
    
			case KEY_UP_PRES:
				HAL_GPIO_WritePin(GPIOB,BUZ_Pin,GPIO_PIN_SET);
				HAL_Delay(300);
				HAL_GPIO_WritePin(GPIOB,BUZ_Pin,GPIO_PIN_RESET);
				HAL_Delay(300);
				break;
			case KEY_LEFT_PRES:
				HAL_GPIO_TogglePin(GPIOC,LED1_Pin);
				break;
			case KEY_DOWN_PRES:
				HAL_GPIO_TogglePin(GPIOC,LED2_Pin);
				break;
			case KEY_RIGHT_PRES:
				HAL_GPIO_TogglePin(GPIOC,LED1_Pin);
				HAL_GPIO_TogglePin(GPIOC,LED2_Pin);
				break;
		}
		HAL_Delay(10);
    /* USER CODE END WHILE */
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

4. Download verification

After compiling without error, download to the development board, press KEY_UP and the buzzer will sound once; K1 controls D1, press once to turn on and then press to turn off; K2 controls D2, press once to turn on and press again to turn off; K3 simultaneously controls D1 and D2, press once Their state is flipped once

Guess you like

Origin blog.csdn.net/Chuangke_Andy/article/details/108569293