• The last:【STM32Cube_06】 Use USART to send and receive data (query mode)

For a better reading experience, please go:Mculover666’s personal blog.

1. Preparation

Hardware preparation

Firstly, we need to prepare a development board, here I prepare the development board of STM32L4 (BearPi) :

Software to prepare

  • Keil-mdk and the corresponding chip package need to be installed in order to compile and download the generated code;
  • Prepare a serial debugging assistant, the one I used hereSerial Port Utility;

Keil MDK and Serial Port Utility installation packages are available at the end of this article.

2. Generate MDK project – initialize GPIO as input

Select the chip model

Open STM32CubeMX, open MCU selector:



Search for and select the chipSTM32L431RCT6:

Configuring the Clock Source

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

Here I use the external clock:

Configure a serial port

Bear Pai developed onboard ST-link and virtual a serial port, the schematic diagram is as follows:





Let’s start configuringUSART1:

NVIC configuration

Configure USART interrupt priority in NVIC:

Configuring the Clock Tree

The maximum main frequency of STM32L4 is 80M, so PLL is configured and HCLK = 80Mhz at last:

Build project Settings

Code generation Settings

Finally set to generate a separate initialization file:

The generated code

Click on theGENERATE CODEMdk-v5 project can be generated:

3. Write, compile and download user code in MDK

Define send and receive buffers

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint8_t hello[] = "USART1 is ready... \n";
uint8_t recv_buf[13] = {0};
/* USER CODE END 0 */
Copy the code

Re-implement the interrupt callback function

HAL_UART_RxCpltCallback = HAL_UART_RxCpltCallback = HAL_UART_RxCpltCallback = HAL_UART_RxCpltCallback = HAL_UART_RxCpltCallback

/* USER CODE BEGIN 4 */
/* Interrupt the callback function */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	/* Determine which serial port triggered the interrupt */
	if(huart ->Instance == USART1)
	{
		// Send the received data
		HAL_UART_Transmit_IT(huart, (uint8_t*)recv_buf, 13);
		// Re-enable serial port reception interrupt
		HAL_UART_Receive_IT(huart, (uint8_t*)recv_buf, 13); }}/* USER CODE END 4 */
Copy the code

Modify main function

In the main function, first enable the serial port interrupt receiving, and then send a prompt message:

int main(void)
{
  HAL_Init();

  SystemClock_Config();

  MX_GPIO_Init();
  MX_USART1_UART_Init();

  /* USER CODE BEGIN 2 */
  // Enable serial port interrupt reception
  HAL_UART_Receive_IT(&huart1, (uint8_t*)recv_buf, 13);
  // Send a prompt message
  HAL_UART_Transmit_IT(&huart1, (uint8_t*)hello, sizeof(hello));
  /* USER CODE END 2 */

  while (1) {}}Copy the code

Compile the code

Compile the whole project:

Set up the downloader

The experimental phenomena

After downloading and running, the experimental phenomena are as follows:

Now that you’ve learned how to configure USART to send and receive data using interrupt mode, the next section describes how to configure USART to send and receive data using DMA mode.

For more exciting articles and resources, please pay attention to my wechat official number: “McUlover666”.