STM32CubeMx tutorial (3) - serial communication, serial protocol

foreword

The previous chapter explained how to configure LEDs through STM32CubeMx. This lesson will introduce the serial port functions commonly used in microcontrollers. The serial port is a communication interface commonly used in many devices such as single-chip microcomputers, sensors, and execution modules. Use the serial port to communicate between the microcontroller and the computer running computer vision.

Through the study of this lesson, you will master how to calculate the baud rate of the serial port through the APB clock, the configuration method of the serial port in cubeMX, the receive interrupt and idle interrupt functions of the serial port, and the send function and send interrupt of the serial port.

preparation tool

Software: STM32CubeMx, Keil5 MDK, serial debugging assistant XCOM

Hardware: STM32F103C8T6 core board, downloader ST_LINK, USB to TTL or J-LINK

The project in this chapter has been uploaded to Baidu Netdisk, and this link is permanently valid

Link: https://pan.baidu.com/s/108QKIltQ-aZ94inuvKylGg?pwd=b8l3 
Extraction code: b8l3

CubeMx configuration

Open the project template created in the previous chapter, configure the serial communication mode as asynchronous communication, the baud rate is 115200, and enable the global interrupt

 

 Finally generate the code and open the project

HAL library UART function library introduction

1. Serial port send/receive function

  1. HAL_UART_Transmit(); The serial port sends data, using the timeout management mechanism 
  2. HAL_UART_Receive(); The serial port receives data, using the timeout management mechanism
  3. HAL_UART_Transmit_IT();Serial port interrupt mode transmission  
  4. HAL_UART_Receive_IT(); Receive in serial port interrupt mode
  5. HAL_UART_Transmit_DMA();Serial port DMA mode transmission
  6. HAL_UART_Transmit_DMA(); Serial port DMA mode reception
HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)

Function: Serial port sends data of specified length. If the timeout is not completed, it will no longer send and return the timeout flag (HAL_TIMEOUT).

parameter:

UART_HandleTypeDef *huart huart handle, such as: UART_HandleTypeDef huart1; alias is huart1  
*pData Data to be sent 
Size Number of bytes to send
Timeout Maximum sending time, send data beyond this time and quit sending   

HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)

Function: serial port interrupt reception, receive specified length data in interrupt mode.
The general process is to set the data storage location, receive the data length, and then enable the serial port receiving interrupt. When data is received, a serial port interrupt will be triggered.
Then, the serial port interrupt function is processed until the specified length of data is received, then the interrupt is turned off, and the interrupt receiving callback function is entered, and the receiving interrupt is no longer triggered. (Only trigger an interrupt)

Parameters: UART_HandleTypeDef *huart huart handle, such as: UART_HandleTypeDef huart1; alias is huart1  
*pData Received data storage address
Size Received bytes

2. Serial interrupt function

  1. HAL_UART_IRQHandler(UART_HandleTypeDef *huart); //Serial interrupt handler
  2. HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart); //Serial port sending interrupt callback function
  3. HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart); //Serial port sends half interrupt callback function (less used)
  4. HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart); //Serial port receiving interrupt callback function
  5. HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart);//Serial port receives half callback function (less used)
  6. HAL_UART_ErrorCallback(); serial port receiving error function
HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart);  

Function: After the interrupt of the HAL library is completed, it will not exit directly, but will enter the interrupt callback function, where the user can set the code. After the serial port interrupt reception is completed, it will enter this function. This function is an empty function. You need to modify it yourself,

Parameter: UART_HandleTypeDef *huart huart handle, such as: UART_HandleTypeDef huart1; alias is huart1  

Serial transceiver

The function initialization part needs to add the serial port receiving interrupt opening function

  /* USER CODE BEGIN 2 */
	UART_Start_Receive_IT(&huart1,(uint8_t *)&rec_buffer,1);
  /* USER CODE END 2 */

 The user-defined serial port reception is performed in the interrupt callback function, so the following code needs to be added to the project

uint8_t rec_buffer;

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)//中断回调函数
{
	UNUSED(huart);
	if(huart->Instance == USART1)//判断串口号
	{
		static uint8_t temp;
		temp = rec_buffer;

		if(temp == 0x01)
			HAL_GPIO_WritePin(LEDD_GPIO_Port, LEDD_Pin, GPIO_PIN_SET);
		else if(temp == 0x02)
			HAL_GPIO_WritePin(LEDD_GPIO_Port, LEDD_Pin, GPIO_PIN_RESET);
		HAL_UART_Transmit(&huart1,(uint8_t *)&rec_buffer,1,100); // 将接收到的数据再通过串口发送出去
		HAL_UART_Receive_IT(&huart1, (uint8_t *)&rec_buffer, 1);   //重新使能接收中断
	}
}

Connect TXD on the serial port CH340 to PA10 on the main control, and connect RXD to PA9 corresponding to the main control. Send 0x01 or 0X02 through XCOM to see the LED change.

Redefine the printf function

Need to include stdio.h library file

#include "bsp_usart.h"
#include <stdio.h>

extern UART_HandleTypeDef huart1;

Redirect the c library function printf

/**
  * 函数功能: 重定向c库函数printf到DEBUG_USARTx
  * 输入参数: 无
  * 返 回 值: 无
  * 说    明:无
  */
int fputc(int ch, FILE *f)
{
  HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xffff);
  return ch;
}

Redirect the c library function getchar, scanf

/**
  * 函数功能: 重定向c库函数getchar,scanf到DEBUG_USARTx
  * 输入参数: 无
  * 返 回 值: 无
  * 说    明:无
  */
int fgetc(FILE *f)
{
  uint8_t ch = 0;
  HAL_UART_Receive(&huart1, &ch, 1, 0xffff);
  return ch;
}

The usage of the printf function is the same as that of the c library, which can output characters, strings and variables, as shown in the figure below

 Send and receive characters through the serial port through XCOM

Guess you like

Origin blog.csdn.net/weixin_49821504/article/details/126848758