Detailed explanation of STM32 serial port sending data

Publisher:pcwgLatest update time:2016-10-10 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Serial port interrupt mode:

/**
* @brief Enables or disables the specified USART interrupts.
* @param USARTx: where x can be 1 or 2 to select the USART peripheral.
* @param USART_IT: specifies the USART interrupt sources to be enabled or disabled.
* This parameter can be one of the following values:
* @arg USART_IT_WU: Wake up interrupt. //Wake-up interrupt
* @arg USART_IT_CM: Character match interrupt.//Character match interrupt
* @arg USART_IT_EOB: End of block interrupt. //Block end interrupt
* @arg USART_IT_RTO: Receive time out interrupt. //Receive timeout interrupt
* @arg USART_IT_CTS: CTS change interrupt. //CTS change interrupt
* @arg USART_IT_LBD: LIN Break detection interrupt. //Termination detection interrupt* @arg USART_IT_TXE
: Tansmit Data Register empty interrupt. //Transmit data register empty interrupt
* @arg USART_IT_TC: Transmission complete interrupt. //Transmission completion interrupt
* @arg USART_IT_RXNE: Receive Data register not empty interrupt. //Receive register not empty interrupt
* @arg USART_IT_IDLE: Idle line detection interrupt. //Idle line detection interrupt
* @arg USART_IT_PE: Parity Error interrupt. //Parity error interrupt
* @arg USART_IT_ERR: Error interrupt(Frame error, noise error, overrun error) //Error interrupt
* @param NewState: new state of the specified USARTx interrupts.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/

 
 
First, post this part of the data sheet.
 
Character transmission procedure
1. Program the M bits in USARTx_CR1 to define the word length.
2. Select the desired baud rate using the USARTx_BRR register.
3. Program the number of stop bits in USARTx_CR2.
4. Enable the USART by writing the UE bit in USARTx_CR1 register to 1.
5. Select DMA enable (DMAT) in USARTx_CR3 if Multi buffer Communication is to take 
place. Configure the DMA register as explained in multibuffer communication.
6. Set the TE bit in USARTx_CR1 to send an idle frame as first transmission.
7. Write the data to send in the USARTx_TDR register (this clears the TXE bit). Repeat 
this for each data to be transmitted in case of single buffer. 
8. After writing the last data into the USARTx_TDR register, wait until TC=1. This 
indicates that the transmission of the last frame is complete. This is required for 
instance when the USART is disabled or enters the Halt mode to avoid corrupting the 
last transmission.
 
Character transmission process 
1. In the scheme, the M bit of USARTx_CR1 is used to define the word length. 
2. Select the desired baud rate using the USARTx_BRR register. 
3. Program the number of stop bits in USARTx_CR2. 
4. Enable the USART by writing the UE bit in register 1 USARTx_CR1. 
5. Select DMA Enable (DMAT) in USARTx_CR3 if multi-buffer communication is to be taken 
Configuring DMA registers is explained in Multi-buffer Communication. 
6. Set the TE bit in USARTx_CR1 to send an idle frame as the first transmission. 
7. Write the data to be transmitted in the USARTx_TDR register (this will clear the TXE bit). Repeat 
This is done for each data transfer in a single buffer. 
8. After writing the last data to the USARTx_TDR register, wait until TC=1. 
Indicates that the transmission of the last frame is complete. This is required 
For example, when the USART is disabled or put into suspend mode, to avoid corruption 
Last transmission.
 
Detailed explanation of sending data via STM32 serial port - Dahai - Dahai's blog
 
First, take a closer look at the timing diagram of the data being sent.
It is found that when USART_TDR has no data, that is, it is empty, TXE will be set. When writing data to USART_TDR, TXE is low.
List the conditions for TXE and TCflag changes
TXE: Set as long as USART_TDR has no data
TCflag: The interruption will only occur when all TXline data is sent out.
Sending process: write data into USART_TDR->TXline sends out.
That is, TCflag is set one character later than TXEflag.
 
 
Character transmission process 
 
(change)

1. Enable the serial port to send TE. At this time, USART_DR is empty. At this time, check whether TXE is set to 1. TXE is set to 1. The TX pin first sends an idle frame, writes the F1 frame to USART_DR, and TXE is cleared. Because the idle frame is being sent at this time, the data written to USART_DR is placed in the TDR register and has not yet been copied to the shift register.
2. After the idle frame is sent, the data in the TDR register is copied to the shift register. At this time, check whether TXE is set to 1. TXE is set to 1, indicating that TDR is empty and the next data can be placed. At this time, the data of the F1 frame will be sent on the TX pin, and the software writes the data of the F2 frame to USART_DR, and TXE is cleared.
3. After the stop bit of the F1 frame is sent, because F2 in the TDR register has not been copied into the shift register, TXE is still 0 at this time, and TC is not set to 1. At this time, check whether TXE is set to 1. TXE is set to 1, indicating that TDR is empty and the next data can be placed. At this time, the data of F2 frame will be sent on TX pin, and the software will write the data of F3 frame into USART_DR, and TXE will be cleared.
4. After the stop bit of F2 frame is sent, because F3 in TDR register has not been copied into shift register, TXE is still 0, and TC is not set to 1. At this time, check whether TXE is set to 1. TXE is set to 1, indicating that TDR is empty, and no data is written to USART_DR later. TXE remains high, and the data of F3 frame will be sent on TX pin.
5. After the stop bit of F3 frame is sent, because TXE is 1 at this time, the TC flag will be set to 1. If TCIE is 1, an interrupt will be generated.

Note a few points when sending via the serial port:
1. If data is being sent, writing data into USART_DR will write the data into the TDR buffer register. After the current transmission is completed, the data in TDR will be copied into the shift register.
2. If data is not being sent, writing data into USART_DR will put the data directly into the shift register without passing through TDR. TXE is cleared, and then the transmission starts, and TXE is set to 1 by hardware.
3. TC will only be set to 1 when the stop bit of a frame of data is sent and TXE is 1.


Keywords:STM32 Reference address:Detailed explanation of STM32 serial port sending data

Previous article:When configuring the STM32 clock, pay attention to setting the FLASH wait cycle
Next article:Understanding of several STM32 startup files

Recommended ReadingLatest update time:2024-11-16 14:27

STM32 clock, external 16M setting
The general clock setting is an external 8M clock, but when it is set to an external 16M clock, the following configuration is required: (1) For the stm32f103xx chip, modify HSE_VALUE in the stm32f10x.h library at around line 119 to 16000000 (the default is 8000000), then set it in system_stm32f10x.c, around line 1054
[Microcontroller]
STM32 study notes memory structure
This article takes STM32F103ZE as a prototype to analyze its memory structure, so as to understand its memory physical address and allocate structure to expand applications. The STM32F103ZE chip has built-in 32KB SRAM and 512KB Flash. Its mapping address is shown in the figure below, which comes from its data sheet. T
[Microcontroller]
STM32 study notes memory structure
stm32 touch screen XPT2046
Pin Function Description  Control word control bit command  Description of each bit of control byte  Single-ended input configuration  Differential Mode Input Configuration  Timing  The first 8 clocks are used to input the control byte through the DIN pin, the next 12 clock cycles will complete th
[Microcontroller]
stm32 touch screen XPT2046
STM32 reset and system clock
First of all, we should understand that reset is a very important part. The quality of the reset part design is related to the stability of the entire system. Generally speaking, STM32F10 series chips have system reset, power reset and backup area reset. Here we will explain the basic knowledge points of the three res
[Microcontroller]
STM32 controls the rotation of the servo
Servo model: SG5010 servo Since servos generally operate at 5V or higher voltage, I was worried whether the STM32 output voltage is 3.3V and whether it can be driven. It turns out that it can. Use Time3 channel 2 as the PWM timer to output a 50HZ square wave with a pulse width between 0.5ms and 2.5ms. The prescaler is
[Microcontroller]
STM32 ADC single conversion example
ADC initialization steps: 1. Initialize the channel IO used by ADC 2. Turn on the ADC clock and initialize the ADC structure 3. Call the ADC_Cmd function to enable ADC, so that the ADC register can be read and written 4. Calibrate the ADC (this step is not necessary. When calibrating the ADC, the ADC hardware will gen
[Microcontroller]
STM32 ADC single conversion example
stm32 software system upgraded from bare metal to ucos
As the title says, the company's project, stm32 was originally naked, and the driver and other parts were written directly. This week, the driver and upper-level application were upgraded to the ucos version. Since the ucos system is very simple (5000 lines), and others have already done the ready-made stm32 porting,
[Microcontroller]
STM32 MCU hardware key foundation essence and precautions
A brief introduction to STM32 1. Background If you are having a hard time choosing a processor for your project: on the one hand, you are complaining about the limited instructions and performance of 16-bit microcontrollers, and on the other hand, you are complaining about the high cost and high power
[Microcontroller]
STM32 MCU hardware key foundation essence and precautions
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号