1. Configure the project through STM32CubeMX
1. Set up RCC
2. Set up SYS
3. Set up USART
4. Set up NVIC
Click GENERATE CODE to generate code
2. Set the receive interrupt in the main function
Function description:
Serial port interrupt reception, receiving data of specified length in interrupt mode. The general process is to set the data storage location, receive the data length, and then enable the serial port reception interrupt. When data is received, the serial port interrupt will be triggered. Then, the serial port interrupt function processes until the specified length of data is received, then turns off the interrupt, enters the interrupt reception callback function, and no longer triggers the reception interrupt. (Only triggers an interrupt once)
3.while(1) code
while (1) { if(flag==1){ \t\t\t//send Message HAL_UART_Transmit( & amp;huart1, (uint8_t *) & amp;message, strlen(message),0xFFFF); \t\t\t //delay HAL_Delay(1000); } }
5. Interrupt function
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { \t //When the input command is 0, send a prompt and change the flag if(c=='0'){ flag=0; HAL_UART_Transmit( & amp;huart1, (uint8_t *) & amp;tips2, strlen(tips2),0xFFFF); } \t //When the input command is 1, send a prompt and change the flag else if(c=='1'){ flag=1; HAL_UART_Transmit( & amp;huart1, (uint8_t *) & amp;tips1, strlen(tips1),0xFFFF); } \t //When the input command does not exist, send a prompt and change the flag else { flag=0; HAL_UART_Transmit( & amp;huart1, (uint8_t *) & amp;tips, strlen(tips),0xFFFF); } //reset interrupt HAL_UART_Receive_IT( & amp;huart1, (uint8_t *) & amp;c, 1); }
main.c
#include "main.h" #include "usart.h" #include "gpio.h" #include <string.h> void SystemClock_Config(void); char c;//Instruction 0: Stop 1: Start char message[]="hello Windows\\ ";//Output message char tips[]="CommandError\\ ";//Tip 1 char tips1[]="Start.....\\ ";//Tips 2 char tips2[]="Stop......\\ ";//Tips 3 int flag=0;//Flag 0: Stop sending 1. Start sending int main(void) { HAL_Init(); SystemClock_Config(); MX_GPIO_Init(); MX_USART1_UART_Init(); \t //Set up to accept interrupt HAL_UART_Receive_IT( & amp;huart1, (uint8_t *) & amp;c, 1); \t //When flag is 1, send information once per second //When flag is 0, stop while (1) { if(flag==1){ \t\t\t//send Message HAL_UART_Transmit( & amp;huart1, (uint8_t *) & amp;message, strlen(message),0xFFFF); \t\t\t //delay HAL_Delay(1000); } } } void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { \t //When the input command is 0, send a prompt and change the flag if(c=='0'){ flag=0; HAL_UART_Transmit( & amp;huart1, (uint8_t *) & amp;tips2, strlen(tips2),0xFFFF); } \t //When the input command is 1, send a prompt and change the flag else if(c=='1'){ flag=1; HAL_UART_Transmit( & amp;huart1, (uint8_t *) & amp;tips1, strlen(tips1),0xFFFF); } \t //When the input command does not exist, send a prompt and change the flag else { flag=0; HAL_UART_Transmit( & amp;huart1, (uint8_t *) & amp;tips, strlen(tips),0xFFFF); } //reset interrupt HAL_UART_Receive_IT( & amp;huart1, (uint8_t *) & amp;c, 1); } /* USER CODE END 4 */ /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; if (HAL_RCC_OscConfig( & amp;RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB buses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig( & amp;RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) { Error_Handler(); } } /* USER CODE BEGIN 4 */ /* USER CODE END 4 */ /** * @brief This function is executed in case of error occurrence. * @retval None */ void Error_Handler(void) { /* USER CODE BEGIN Error_Handler_Debug */ /* User can add his own implementation to report the HAL error return state */ __disable_irq(); while (1) { } /* USER CODE END Error_Handler_Debug */ }
7. Compile and burn
Then access .hex
3. Physical effect
Then open the Wildfire Serial Port Debugging Assistant
Send 1
Send 0, stop sending hello Windows
View waveforms:
Configuration Magic Wand
Click Setup
Output results:
When the UART communication protocol transmits one byte of data, it generates eight combinations of high and low levels on the signal line. The data transfer rate is expressed by the baud rate, which is 150bit and takes 1.28675ms, so the actual baud rate The rate is 116573.