[embedded STM32] use interrupt method to realize lighting and serial communication

1, Interrupt introduction

1. Interrupt concept

The whole interruption process is divided into three steps
1. Interrupt occurrence: when the CPU is processing an event A, another event B occurs and requests the CPU to handle it quickly
2. Interrupt processing: the CPU pauses the current work and transfers to event B
3. Interrupt return: when the CPU finishes processing event B, it returns to the suspended place in event A to continue processing event A

Schematic diagram of interrupt program execution process:

Interrupt response process:

2. Function of interruption

Interruption has four functions
1. Speed matching: it can solve the contradiction between fast CPU and slow external devices
2. Time sharing operation: the CPU can serve multiple external devices in time sharing to improve the utilization of the computer
3. Real time response: the CPU can handle the random events of the application system in time to enhance the real-time performance of the system
4. High reliability: CPU can deal with equipment failure, power failure and other emergencies to improve system reliability

3. Interrupt priority

1. When multiple interrupts occur at the same time, the processor responds to the high priority interrupt first
2. When ISR of low priority interrupt is executed, it can be interrupted again by high priority interrupt
3.ISR has higher execution priority than App Code

2, Experimental topic requirements

1. Topic 1

One pin of the GPIOA end of the stm32F103 core board is connected to an LED, and one pin of the GPIOB port is connected to a switch (replaced by DuPont line simulation). Interrupt mode programming is adopted. When the switch is connected to high level, the LED lights up; When connected to low level, the LED is off.

2. Topic 2

The serial port interrupt mode is adopted to redo the serial port communication operation last week.

3, Lighting in interrupt mode

1.CubeMX project setting

1 Select File - > new project in the main interface or directly click accept to MCU selector
2. Select the chip model. Generally, you can search your own chip model directly in the upper left corner.
3. Peripheral pin setting
Set the indicator LED pin PB5 and set the pin mode to output mode GPIO_Output
Set key pin PA1, set pin to external interrupt function, and PA1 is connected to GPIO with external interrupt line exit1_ EXIT1

For the pin PA1 corresponding to the switch, set its trigger mode as rising edge trigger

Enable the corresponding external interrupt line and click Enabled

4 clock setting

5 generate engineering documents

2. Code part

1. First open the project file generated by cubemx
2. The interrupt service function can be found in the gpio.c file in the Keil file

void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)

When the rising edge is captured and an interrupt is triggered, it will enter this function

Then Hal will be executed_ GPIO_ EXTI_ Callback (gpio_pin) function. This function is a callback function. When we open it, we can find a break in front.
Front__ weak indicates that this function is a virtual function and needs to be rewritten by the user.
Find a location under the main file and add the following code

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
	if( GPIO_Pin == A1_EXTI_Pin)//Determine external interrupt source
	{
		HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);//Flip LED status
	}
}

3. Compilation

4. Burning

5. Result display

4, Interrupt mode to realize serial communication

1. Requirements

Complete a USART serial port communication program of STM32 (query mode is OK, interrupt mode is not required temporarily). Requirements:
1) Set the baud rate to 115200, 1 stop bit and no check bit;
2) STM32 system continuously sends "hello windows!" to the upper computer (win10). Win10 uses the "serial port assistant" tool to receive.

2. Project establishment

Select the chip and set RCC and SYS,

Set serial port
1) Click USART1
2) Set MODE to asynchronous communication
3) Basic parameters: baud rate is 115200 Bits/s. The transmission data length is 8 bits. Parity check none, stop bit 1
The NVIC Settings column enables to receive interrupts

Clock setting

Create project

3. Coding

Add header file #include "stdio.h" in main.c and usart.c
Then, add the following code to the usart.c file for redefinition

/* USER CODE BEGIN 1 */

//Add the following code to support the printf function without selecting use MicroLIB	  
//#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)	
#if 1
//#pragma import(__use_no_semihosting)             
//Support functions required by the standard library                 
struct __FILE 
{ 
	int handle; 
}; 

FILE __stdout;       
//Definition_ sys_exit() to avoid using half host mode    
void _sys_exit(int x) 
{ 
	x = x; 
} 
//Redefine fputc function 
int fputc(int ch, FILE *f)
{ 	
	 HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0x0001);  
	return ch;
}
#endif 

/* USER CODE END 1 */


In the main.c main function, add send data

 /* USER CODE END WHILE */
	  	printf("Hello windows!\r\n");
		HAL_Delay(500);
    /* USER CODE BEGIN 3 */

Add the following definition in main.c to receive serial port data

uint8_t aRxBuffer;			//Receive interrupt buffer
uint8_t Uart1_RxBuff[256];		//Receive buffer
uint8_t Uart1_Rx_Cnt = 0;		//Receive buffer count
uint8_t	cAlmStr[] = "data overflow (Greater than 256)\r\n";

Add a statement that turns on receiving interrupts

/* USER CODE BEGIN 2 */
	HAL_UART_Receive_IT(&huart1, (uint8_t *)&aRxBuffer, 1);
/* USER CODE END 2 */

Add interrupt callback function in the lower part of main.c

/* USER CODE BEGIN 4 */
/**
  * @brief  Rx Transfer completed callbacks.
  * @param  huart pointer to a UART_HandleTypeDef structure that contains
  *                the configuration information for the specified UART module.
  * @retval None
  */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
  /* Prevent unused argument(s) compilation warning */
  UNUSED(huart);
  /* NOTE: This function Should not be modified, when the callback is needed,
           the HAL_UART_TxCpltCallback could be implemented in the user file
   */
 
	if(Uart1_Rx_Cnt >= 255)  //Overflow judgment
	{
		Uart1_Rx_Cnt = 0;
		memset(Uart1_RxBuff,0x00,sizeof(Uart1_RxBuff));
		HAL_UART_Transmit(&huart1, (uint8_t *)&cAlmStr, sizeof(cAlmStr),0xFFFF);	
	}
	else
	{
		Uart1_RxBuff[Uart1_Rx_Cnt++] = aRxBuffer;   //Receive data transfer
	
		if((Uart1_RxBuff[Uart1_Rx_Cnt-1] == 0x0A)&&(Uart1_RxBuff[Uart1_Rx_Cnt-2] == 0x0D)) //Judgment end bit
		{
			HAL_UART_Transmit(&huart1, (uint8_t *)&Uart1_RxBuff, Uart1_Rx_Cnt,0xFFFF); //Send the received information
			Uart1_Rx_Cnt = 0;
			memset(Uart1_RxBuff,0x00,sizeof(Uart1_RxBuff)); //Empty array
		}
	}
	
	HAL_UART_Receive_IT(&huart1, (uint8_t *)&aRxBuffer, 1);   //Restart receive interrupt
}
/* USER CODE END 4 */

4. Compile and burn

Compilation results

Burning results

5. Result display


The serial port outputs Hello windows every 0.5s

When sending data below, such as 123456789, the serial port will enter the interrupt, and then return to the original loop to continue sending Hello windows.

5, Experimental summary

This experiment is mainly about learning and verifying interruptions. I have a deep understanding of interruptions through two small assignments.

6, Reference blog

1.https://blog.csdn.net/cleveryoga/article/details/121130394?utm_source=app&app_version=4.11.0&code=app_1562916241&uLinkId=usr1mkqgl919blen
2. https://blog.csdn.net/qq_46467126/article/details/121055475?utm_source=app&app_version=4.11.0&code=app_1562916241&uLinkId=usr1mkqgl919blen

Tags: stm32 ARM

Posted on Fri, 05 Nov 2021 21:40:37 -0400 by jmarco