Serial communication based on STM32CubeMX-HAL library development (DMA mode)

STM32CubeMX configuration

First, open STM32CubeMX and select the chip model you developed. Here I choose the F103R6Tx chip. Then the first step is to select the debug mode in SYS. If the HAL library is not used for SWD or JTAG configuration, the microcontroller can only download the program once. To download the second or more programs, you need to press the reset button (if your If the MCU has a reset button), or use tweezers to clamp the capacitor on the reset circuit to make it short, click Keil to download, and then loosen the tweezers.

Insert picture description hereThen select the external high-speed clock as the RCC clock source, connect the external crystal oscillator,
Insert picture description hereconfigure the clock tree,
Insert picture description hereand then open USART1 and select the asynchronous communication mode to
Insert picture description hereopen the DMA addition. Note that the RX DMA transfer mode is selected as circular mode, otherwise it will only be accepted once.
Insert picture description hereIf you use DMA to send, you need to enable the interrupt of USART1. Note: If the serial port interrupt is not turned on, the program can only send data once, and the program cannot judge whether the DMA transfer is complete. USART is always in a busy state.
Insert picture description here Here, please pay attention to the selection environment and path Don't show Chinese name
Insert picture description hereInsert picture description here

related functions

HAL_UART_Transmit(); Serial port polling to send data, using timeout management mechanism
HAL_UART_Receive(); Serial port polling to receive data, using timeout management mechanism
HAL_UART_Transmit_IT(); serial port interrupt mode sending
HAL_UART_Receive_IT(); serial port interrupt mode receiving
HAL_UART_Transmit_DMA(); serial port DMA Mode sending
HAL_UART_Receive_DMA(); serial port DMA mode receiving
HAL_UART_DMAPause() Pause serial port DMA
HAL_UART_DMAResume(); Resume serial port DMA
HAL_UART_DMAStop(); End serial port DMA

Reconfigure printf and scanf

Include #include <stdio.h> in stm32f1xx_hal.c

#include "stm32f1xx_hal.h"
#include <stdio.h>
extern UART_HandleTypeDef huart1;   //声明串口

Rewrite the fget and fput functions in stm32f1xx_hal.c

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

Configuration code

Add code in the main function

uint8_t buffer[20];
HAL_UART_Transmit_DMA(&huart1,buffer,20);

Turn on DMA acceptance, and then write the operations needed after acceptance in the callback function, for example, use DMA to send the received data

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) 
{
    
    
if(huart->Instance == USART1)  //判断是否是串口1接受中断
 {
    
       
      HAL_UART_Transmit_DMA(&huart1,buffer,20);
 }
}

Guess you like

Origin blog.csdn.net/lxzdsg/article/details/113101417