Microcontroller based - send and receive data using USART (Interrupt Mode)

1. Preparations

Hardware preparation

  • First, we need a development board, and here I was prepared for the development board STM32L4 (BearPi):

 

 

2. Generate MDK project

One is chosen

Open STM32CubeMX, open MCU Selector:

 

 Search and select chip STM32L431RCT6:

 

 

Clock Source

  • If you choose to use external high-speed clock (HSE), you need to configure the RCC in System Core;
  • If you use the default internal clock (HSI), you can skip this step;

Here I have to use an external clock:

 

 

Serial Configuration

Winnie the school development board on-board ST-Link and a virtual serial port, schematics as follows:

 

 

Here I will switch to AT-MCUmode, the connection between the PC's serial port and USART1.

Then start the configuration USART1:

 

 

NVIC Configuration

Configuring USART interrupt priority in the NVIC:

 

 

Configuring the Clock Tree

STM32L4 the highest clock speed to 80M, so the configuration PLL, the last HCLK = 80Mhzto:

 

 

 

 

Generating project settings

 

 

Code generation is provided

The last set generates a separate initialization file:

 

 

Generate code

Click GENERATE CODEto generate MDK-V5 project:

 

 

3. In the MDK write, compile, download user code

Definition of transmit and receive buffers

1 /* Private user code ---------------------------------------------------------*/
2 /* USER CODE BEGIN 0 */
3 uint8_t hello[] = "quot;USART1 is ready...\n";
4 uint8_t recv_buf;
5 /* USER CODE END 0 */

## reimplement callback function NVIC interrupt a lecture we explore the interrupt handling mechanism HAL library, HAL Weak define an interrupt callback `HAL_UART_RxCpltCallback`, we need to redefine the function in the user files, which are placed Yes, here I put in `main.c`:

. 1  / * the USER CODE the BEGIN. 4 * / 
2  / * Interrupt callback function * / 
. 3  void HAL_UART_RxCpltCallback (UART_HandleTypeDef * HUART)
 . 4  {
 . 5      / * determine which serial triggered interrupt * / 
. 6      IF (HUART -> == Instance the USART1)
 7      {
 8          // the received data transmission 
. 9          HAL_UART_Transmit (HUART, & recv_buf, . 1 , 0 );
 10          // re-enable serial port receive interrupt 
. 11          HAL_UART_Receive_IT (HUART, & recv_buf, . 1 );
 12 is      }
13 }
14 /* USER CODE END 4 */
View Code

Modify the main function

In the main function is turned on first serial port interrupt reception, and then send message:

. 1  int main ( void )
 2  {
 . 3    HAL_Init ();
 . 4  
. 5    SystemClock_Config ();
 . 6  
. 7    MX_GPIO_Init ();
 . 8    MX_USART1_UART_Init ();
 . 9  
10    / * the USER CODE the BEGIN 2 * / 
. 11    // enable serial reception interrupt 
12 is    HAL_UART_Receive_IT (& huart1, & recv_buf, . 1 );
 13 is    // send message 
14    HAL_UART_Transmit_IT (& huart1, (uint8_t *) Hello, the sizeof (Hello));
 15    / * the USER CODE 2 the END * / 
16  
. 17   while (1)
18   {
19   }
20 }
View Code

Compiled code

Click the button to compile the project as shown

 

 Compile success

 

 

Set Downloader

As shown in Figure Click the button to open the Settings page

 

 Download settings, select "ST-Link Debugger", and click "Settings".

 

 Check the "Reset and Run" option under "Flash Download" menu, after the burning process has reached the MCU automatically reset and run the purpose of the program.

 

 

Download run

Click "LOAD" button to burn code into the device.

 

 Burning success

 

 

Experimental phenomena

After downloading run, experiments phenomenon as follows:

 

 So far, we have learned how to configure USART interrupt mode to use to send and receive data, the next section will discuss how to configure USART send and receive data using DMA mode.

Guess you like

Origin www.cnblogs.com/YuchuanHuaying/p/12545708.html