STM32 microcontroller development - UART&USART serial port transceiver (2) variable length data transceiver DMA serial port transceiver - STM32CubeMx project

Table of contents

I. Overview

2. Program implementation

3. Unfixed length data sending and receiving

Four. Summary 


I. Overview

        In this article, we enter the second part of the teaching of the STM32 serial port transceiver function, mainly explaining the DMA transceiver mode, and the source code will be provided for free at the end .

        Development tools: STM32CubeMx, STM32 development board (model is not limited)

        Basic article: STM32 microcontroller development - UART & USART serial port transceiver - STM32CubeMx project generation


2. Program implementation

        First, we continue to open the last STM32CubeMx project file.

        Open the DMA settings of USART1 and add it as shown below:

        We then regenerate and open the file.

        There is no need to make more changes to the code. On the original basis, only the IT at the end of the function name is changed to DMA.

uint8_t receive[10];
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	HAL_UART_Transmit_DMA(&huart1,receive,10);//发送函数
	HAL_UART_Receive_DMA(&huart1,receive,10);//接收函数
}
/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */
	
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
	HAL_UART_Receive_DMA(&huart1,receive,10);//接收函数
	while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
		
		
		
  }
  /* USER CODE END 3 */
}

        We compile and debug the code, and open the serial port debugging assistant. The debugging method has been introduced in the previous article.

         The function is realized, but there is another problem at present. If we send data with five digits or less than the specified digit, the program will not respond, and it needs to reach the digit before giving feedback.


        Therefore, we need a different way of writing.

3. Unfixed length data sending and receiving

        To receive variable-length data, we need to use the idle interrupt. As the name suggests, when the serial port is in the idle state, it enters the interrupt, and then the variable-length reception can be realized.

        We need to add an initialization statement in the main function:

__HAL_UART_ENABLE_IT(&huart1,UART_IT_IDLE);//使能空闲中断

        Then, we write our own idle interrupt handler in the interrupt handler of USART1 (ie: USART1_IRQHandler).

        We first open the stm32f1xx_it.c file and define external variables (ie our data array):

        Then we write the following code in the interrupt handler function:

void USART1_IRQHandler(void)
{
  /* USER CODE BEGIN USART1_IRQn 0 */	
	if(__HAL_UART_GET_FLAG(&huart1,UART_FLAG_IDLE)==SET)//判断是不是空闲中断
	{
		__HAL_UART_CLEAR_IDLEFLAG(&huart1);//清 中断标志位
		HAL_UART_DMAStop(&huart1);//停止DMA接收
		uint8_t len=255-__HAL_DMA_GET_COUNTER(huart1.hdmarx);//求已接收数据的长度
		HAL_UART_Transmit_DMA(&huart1,receive,len);//发送接收的数据
		HAL_UART_Receive_DMA(&huart1,receive,255);//继续等待接收
	}
  /* USER CODE END USART1_IRQn 0 */
  HAL_UART_IRQHandler(&huart1);
  /* USER CODE BEGIN USART1_IRQn 1 */

  /* USER CODE END USART1_IRQn 1 */
}

        Recently, let's take a look at the code structure in the main.c file, only one reception is enabled in the main function.

        Compile and burn the program, open the serial debugging assistant.

        The effect is perfectly realized.


Four. Summary 

        This article mainly explains the receiving and sending of variable-length data. So far, the serial communication of STM32 has been completed. Finally, the source code is attached for everyone to learn. If you have any questions or comments, please leave a message in the comment area.

         Source code: STM32 serial port variable length data communication source code-article supporting resources

Guess you like

Origin blog.csdn.net/qq_39724355/article/details/127422271
Recommended