Use stm32tubemx interrupt mode to light up LED and serial communication

Keywords: Single-Chip Microcomputer stm32

1, Stm32subemx interrupt mode LED on

1. Interruption and its function

      It is precisely because of the interrupt mechanism that I can methodically "complete multiple tasks at the same time". The interrupt mechanism essentially helps me improve my concurrent "processing" ability. It can also bring the same benefits to the computer system: if you get an interrupt signal when the keyboard is pressed, the CPU does not have to wait for the keyboard input; If the hard disk sends an interrupt signal after reading and writing, the CPU can free up its hands to focus on "serving the public" - whether it is human tapping the fingertips of the keyboard or the magnetic head reading and writing media back and forth, it is too slow compared with the processing speed of the CPU.

2. Interrupt priority

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

3. Project creation

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.

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 PB5 pin corresponding to the LED, the default setting is OK. The name is set to LED and PA1 is set to A1_EXTI and set to rising edge trigger:

 

  Enable the corresponding external interrupt line and check enable:

  Clock set to 36mhz:

  Generate the project file, but before that, you need to determine the storage address and name of the file:

4. Code writing

Under the main function in main.c, write 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
	}
}

  Then compile the generated HEX file

5. Circuit connection

LED long pin - 3V3
LED short pin - PB5
PA1 - 3V3 - light on
PA1 - GND - light off

Then burn:

 

2, Interrupt mode serial communication

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.

1. Project creation

Set RCC and select external clock source:

  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, both receive and transmit are enabled
(4) GPIO pin setting USART1_RX/USART_TX (it is generally set automatically here)
(5) The NVIC Settings column enables to receive interrupts

 

  Clock set to 72mhz:

  2. Code writing

2.1. First, in main.c:

Add header file: #include "stdio.h"

Then add send data:

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

Add definition 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 receive interrupt:

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

Add a callback function (note that it needs to be added after the main function):

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 */

2.2. In usart.c

Add header file: #include "stdio.h"

Then add:

//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 */

    After that, compile and generate the hex file, and then communicate

 

3, References

(50 messages) what is an interrupt and why_ oathevil's column - CSDN blog_ What is an interrupt(50 messages) STM32 interrupt details_ Life all the way, bit by bit record - CSDN blog

(50 messages) [embedded 11] HAL library experiment interrupts switch lighting and serial communication_ Poof poof jar blog - CSDN blog

(50 messages) use STM32CubeMX to realize key control LED (interrupt mode)_ txmnQAQ blog - CSDN blog

4, Summary

From this experiment, I learned the function and application of interrupt

Posted by eagle1771 on Fri, 05 Nov 2021 16:12:25 -0700