STM32 serial port sending configuration process

Publisher:码字奇才Latest update time:2018-09-01 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Serial port byte sending process:


Program the M bit of USARTx_CR1 to define the word length.

Program the STOP bit in USARTx_CR2 to define the number of stop bits.

Program the USARTx_BRR register to determine the baud rate.

Enable the UE bit of USARTx_CR1 to enable USARTx.

If multi-buffer communication is performed, configure the DMA enable (DMAT) of USARTx_CR3.

Enabling the TE bit of USARTx_CR1 enables the transmitter.

Write the data to be sent to the transmit data register TDR.

After writing the last data to the TDR register, wait for the TC position of the status register USARTx_ISR to be set to 1 and the transmission is completed.

Configuration steps 1-6: Configure word length, stop bit, parity bit, baud rate, etc.:


HAL_StatusTypeDef HAL_USART_Init(USART_HandleTypeDef * huart); 

The identifier HAL_USART_ENABLE will be referenced inside this function to enable the corresponding serial port.


The routine is as follows:


void uart_init(u32 bound)

{   

    //UART initialization settings

    UART1_Handler.Instance=USART1; //USART1

    UART1_Handler.Init.BaudRate=bound; //Baud rate

    UART1_Handler.Init.WordLength=UART_WORDLENGTH_8B; //Word length 8 bits

    UART1_Handler.Init.StopBits=UART_STOPBITS_1; //One stop bit

    UART1_Handler.Init.Parity=UART_PARITY_NONE; //No parity bit

    UART1_Handler.Init.HwFlowCtl=UART_HWCONTROL_NONE; //No hardware flow control

    UART1_Handler.Init.Mode=UART_MODE_TX_RX; //Transmit and receive mode

    HAL_UART_Init(&UART1_Handler);                      //HAL_UART_Init()会使能UART1


    HAL_UART_Receive_IT(&UART1_Handler, (u8 *)aRxBuffer, RXBUFFERSIZE);//This function will enable the receive interrupt;

}


Step 7-8 Send data and wait for sending to complete


HAL_StatusTypeDef HAL_USART_Transmit(USART_HandleTypeDef * huart,uint8_t *pTxdata,uint16_t Size,uint32_t Timeout);


First of all, we need to mention the __weak keyword: 

A function with the __weak modifier in front of it is called a weak function. For a weak function, the user can redefine a function with the same name in the user file, and the compiler will eventually choose the user-defined function when compiling. If the user does not define it, the content of the function is the content of the weak function definition. 

Write the picture description here

Benefits of the __weak keyword: 
1. For a pre-defined process, we only want to modify a part of the code related to the user in the process. At this time, we can use a weak function to define an empty function, and then let the user define the function. The advantage of this is that we will not make any changes to the existing program flow. 
2. The __weak keyword is widely used in the HAL library to modify peripheral callback functions. 
3. The peripheral callback function is used by users to write MCU-related programs, which greatly improves the versatility and portability of the program.

The serial port handle is as follows:

Write the picture description here

We can see that there is a serial port parameter initialization structure in the serial port handle, the specific content is as follows:

Write the picture description here

Next, we will describe the overall configuration process of the serial port sending program (HAL library):

1. Initialize serial port related parameters and enable serial port: HAL_USRT_Init(); 
2. Configure serial port related IO ports and multiplex configuration: 
call HAL_GPIO_Init() in HAL_UART_MspInit; 
3. Send data and wait for data transmission to be completed: 
HAL_UART_Transmit();


Keywords:STM32 Reference address:STM32 serial port sending configuration process

Previous article:STM32 beginner's guide (day 6) -------Serial communication
Next article:STM32 serial communication sending and receiving

Recommended ReadingLatest update time:2024-11-17 00:48

STM32 introductory study notes EEPROM storage experiment 4
(2) Create the at24cxx.c file and enter the following code. /****************************************************** *************************************************** ******                 EEPROM driver *************************************************** *************************************************** *****/ #
[Microcontroller]
STM32 study notes---EXTI external key interrupt experiment based on UCOSII
After doing the IWDG independent watchdog experiment based on UCOSII, we continued with the 25th experiment - the EXTI external key interrupt experiment based on UCOSII. This experiment adds EXTI to the IWDG experiment based on UCOSII. Three keys are pressed separately to output key information in the serial port! IWD
[Microcontroller]
STM32 study notes---EXTI external key interrupt experiment based on UCOSII
STM32 USART using parity bit
When there is no check bit, the data bit is usually 8 bits. When using the parity bit, the data bit should be set to 9. A parity bit is also included with the data bits. See the reference manual for details:
[Microcontroller]
STM32 USART using parity bit
STM32 ADC_3 (internal channel)
Internal channels of ADC:    In addition to using external channels to collect external analog voltage signals, the ADC of STM32 also has two internal channels 16 and 17. Channel 16 is connected to the temperature sensor on the chip, and channel 17 is connected to the internal power module. (So these two channels can
[Microcontroller]
Understanding of STM32 analog IIC
I read in a book that the hardware IIC of STM32 is far less convenient than the hardware SPI. The examples given in the book also simulate IIC. Since I have only used the quasi-bidirectional port of 51 to control simple IIC devices before, I took a good look at it. The IIC bus is a communication line consisting of two
[Microcontroller]
Understanding of STM32 analog IIC
STM32 stack knowledge
Write the following code on the STM32 platform: int main() { while(1); } BUILD://Program Size: Code=340 RO-data=252 RW-data=0 ZI-data=1632  After compiling, you will find that such a program has used more than 1600 RAM. Where did this 1600 RAM go? Analyzing the map, you will find that it is occupied by the heap and st
[Microcontroller]
STM32 stack knowledge
Interface circuit between STM32 and four-wire resistive touch screen
  As shown in the figure below, STM32F103F103 is directly connected to the four-wire resistive touch screen through its own I/O port to realize the touch screen controller function. Among them, PA8, PA9, PA10, and PA11 are respectively used as the control ends of the four transistors. By controlling the on and off of
[Microcontroller]
Interface circuit between STM32 and four-wire resistive touch screen
STM32 MCU programming printf() function redirection
In STM32 microcontroller programming, the printf() function can be redirected to output debugging information. Writing fputc() function In fact, the printf() function is defined as a macro in the header file, which will call the fputc() function. However, in the programming of the stm32 microcontroller, the fput
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号