stm32 USART serial communication operation register + library function

Publisher:QianfengLatest update time:2016-06-07 Source: eefocusKeywords:stm32  USART Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Although serial communication is used less and less in today's computers because its communication speed and distance are no longer suitable for PC requirements, it has been replaced by USB port. However, USART is still widely used in the embedded field. 
 
STM32 can provide up to 5 serial ports, with fractional baud rate generator, support for synchronous single-line communication and half-duplex single-line communication, DMA, etc. When using USART, the I/O port of STM32 is connected to the serial port of the computer through the RS232 level conversion circuit. 
 
To use the serial port, you only need to start the serial port clock, set the corresponding I/O port mode, and configure the baud rate, data bit length, parity bit and other information before you can use it.
 
I used three ways to use serial communication, and only one can be enabled: 
  • USART sends information by using the printf() function; 
  • USART communicates with the host computer and outputs the original data after receiving it; 
  • USART actively sends data. 
 
Operation Register
    The serial port is reset by configuring the 14th bit of the APB2RSTR register. When a peripheral fails, it can be reset through the reset register. When the system is initialized, a reset operation is performed.
 
    The baud rate setting of the serial port is in the USART_BRR register. In fact, this register configures the value of the baud rate division trigger factor. The baud rate is the number of characters passing through in one second, and the baud rate is the number of binary bits passing through in one second. Therefore, setting the baud rate requires a period of algorithm processing to obtain the clock division value that achieves this baud rate under a specific clock.
 
    There are three serial port control registers USART_CR1~3, and the most commonly used one is USART_CR1. The descriptions of each register are as follows:
stm32 USART serial communication operation register + library function
   UE:USART enable
   M: Word length This bit defines the length of the data word, 0: one start bit, 8 data bits, n stop bits;
1: One start bit, 9 data bits, n stop bits. n is set by USART_CR2.
   WAKE: Wakeup method 0: Wake up by idle bus; 1: Wake up by address mark.
   PCE: Parity control enable
   PS: Parity selection 0: Even parity; 1: Odd parity.
   PEIE: PE interrupt enable (PE interrupt enable)
  TXEIE: Transmit buffer empty interrupt enable (TXE interrupt enable)
  TCIE: Transmission complete interrupt enable
  RXNEIE: Receive buffer not empty interrupt enable (RXNE interrupt enable)
  IDLEIE: IDLE interrupt enable 0: Disable interrupt; 1: Generate USART interrupt when IDLE in USART_SR is '1'.
 TE: Transmitter enable
 RE: Receiver enable
 RWU: Receiver wakeup 0: The receiver is in normal working mode; 1: The receiver is in silent mode.
Note: 1. Before putting USART into silent mode (setting RWU bit), USART must have received a data byte first. Otherwise, it cannot be awakened by idle bus detection in silent mode.
2. When configured as address mark detection wake-up (WAKE bit = 1), the RWU bit cannot be modified by software when the RXNE bit is set.
 SBK: Send break frame
 
  The sending and receiving of data is realized in USART_DR, which is a double register, including TDR and RDR. When data is written to this register, the serial port will automatically send the data; when data is received, it is also stored in this register and can be read directly. Only the lower 9 bits of this register are valid (8:0), and the other bits are reserved.

 

 

 
 The serial port status is read through the status register USART_SR, and each bit is described as follows:
stm32 USART serial communication operation register + library function
    TXE: Transmit data register empty
        This bit is set by hardware when the data in the TDR register is transferred to the shift register by hardware. If TXEIE in the USART_CR1 register is 1, an interrupt is generated. A write operation to USART_DR clears this bit.
        0: Data has not yet been transferred to the shift register;
        1: Data has been transferred to the shift register.
    TC: Transmission complete
    This bit is set to '1' by hardware when a frame containing data is sent and TXE = 1. If TCIE in USART_CR1 is '1', an interrupt is generated. This bit is cleared by software sequence (read USART_SR first, then write USART_DR). The TC bit can also be cleared by writing '0'. This clearing procedure is only recommended in multi-buffered communication.
    RXNE: Read data register not empty
    When the data in the RDR shift register is transferred to the USART_DR register, this bit is set by hardware to indicate that the data has been received. If the RXNEIE bit in the USART_CR1 register is 1, an interrupt is generated. A read operation on the USART_DR can clear this bit. The RXNE bit can also be cleared by writing 0, which is only recommended in multi-buffer communication.
 
The code for directly operating registers is as follows: (For system.h, stm32f10x_it.h and other related codes, refer to the stm32 direct operation register development environment configuration)
 
User/main.c
view source
#endif

Keywords:stm32  USART Reference address:stm32 USART serial communication operation register + library function

Previous article:Learning and experience of STM32 USART serial port
Next article:STM32 independent watchdog (IWDG) and window watchdog (WWDG)

Recommended ReadingLatest update time:2024-11-17 03:08

stm32 SPI communication operation register
The SPI (Serial Peripheral Interface) bus system is a synchronous serial peripheral interface that enables the MCU to communicate with various peripheral devices in a serial manner to exchange information. SPI was first defined by Freescale (formerly Motorola) on its processors.     SPI is a high-speed, master-slave,
[Microcontroller]
STM32 PWM & Timer Summary
After learning, I found that the timer function of stm32 is really powerful. A small summary is convenient for reference in the future. Stm32 timers are divided into three types: tim1 and tim8 are advanced timers, 6 and 7 are basic timers, and 2-5 are general timers. The main functional differences can be seen from th
[Microcontroller]
Solution to the problem that STM32 can no longer burn after setting the idle pins to analog state
Use STM32's isp download. Set the boot to ISP download mode, and then use the FLYMCU tool to perform ISP download. The new program cannot set the pins to analog state. Otherwise, the pins cannot be recognized when downloading using other methods.
[Microcontroller]
STM32 study notes: simple Bootloader serial port upgrade design
Concept Introduction Before learning how to make a serial port upgrade Bootloader, let's first understand STM32's IAP (In Application Programming). IAP is the user's own program burning part of the User Flash area during operation. The purpose is to easily update the firmware program in the product through the reser
[Microcontroller]
STM32 study notes: simple Bootloader serial port upgrade design
stm32 spi slave mode configuration solution
Target: SPI transmission between stm32 (battleship) and stm32 (mini) (battleship as slave, mini as master) Result: The pass was successful I won't write the code, you can use the one you searched online; But tip: I use (the master and slave chip selects are all software configured); Key points: The important thi
[Microcontroller]
STM32 SST25VF016B Driver
///** //  ****************************************************************************** //  * @file    stm32f10x_SST25VF016B.c  // * @brief SST25VF10B driver //  ****************************************************************************** //  * @ // * Interface definition (STM32 SPI1) // * CE--PA4 SO--P
[Microcontroller]
STM32 enables Bootloader support configuration
1. Program Settings Add the FLASH offset address setting in the first line after entering the main() function, as shown in the figure:  2. Project Settings The space occupied by the Bootloader is 0x4000, so set the Start value to 0x8004000 and the Size value to: original size - 0x4000. Taking STM32F103C8 as a
[Microcontroller]
STM32 enables Bootloader support configuration
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号