Beginner learning STM32, first contact with USART serial communication.
A personal summary of the USART communication hardware principle. The TTL level from the MCU (Micro Controller Unit, also known as the ST chip) is converted to the USB level through a level conversion chip, thereby achieving communication with the PC.
USART (Universal Synchronous/Asynchronous Receiver/Transmitter) Universal synchronous/asynchronous serial receiver/transmitter.
Here we only talk about asynchronous communication. The main difference between asynchronous communication and synchronous communication is the clock, so SLCK is turned off here. USART has a total of 5 external pins: TX, RX, nRTS, nCTS, and SCLK (SW_RX in the block diagram is internal, and n means low level is valid). When learning to communicate with the PC host computer, the hardware flow control mode is turned off, so asynchronous communication only involves the two pins TX (Transmit) and RX (Receive).
The general idea is: data is transferred to the transmit data register USART_DR (TRD) by software, and TC (Transmit complete) is judged to be set to 1. If it is set to 1, it means that the transmit shift register has completed the transmission, and the data in the transmit data register is sent to the transmit shift register to start transmission. TXE (Transmit data register empty) is judged, and the next data is read to TDR; if TC is 0, wait. TC is set to 0 by the software sequence (reading the USART_SR register and then writing to the USART_DR register), or it can be achieved by writing "0" to this bit. When the hardware is reset, TC is set to 1 by hardware.
When receiving data, the data is transferred to the receive shift register and then to the read data register. At this time, RXNE (Read data register not empty) is set to 1, and the interrupt flag RXNEIE can be generated.
Data format: 1 start bit, 8 data bits (USART_CR1, bits 12-M in control register 1 are set to 0), no check bit (USART_SR, bits 1-PE in status register are set to 0 - no parity error), 1 stop bit (USART_CR2, bits 13:12-STOP in control register 2 are assigned to 00). Because the communication is between the development board and the PC, the distance is short and the communication environment is good, so no parity check is set.
USART1 is used, PA9 is selected for TX and PA10 is selected for RX.
Code flow: 1. Initialization a. Turn on the peripheral clock (if you want to use any peripheral, you must enable the clock first), turn on the clock of GPIOA mounted on AHB1 and USART1 mounted on APB2, and call the firmware library functions RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE) and RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE)
b. Initialize the GPIO port related to the serial port, assign values to the GPIO_InitTypeDef GPIO_InitStruct structure, and then map it to the register through GPIO_Init(GPIOA,&GPIO_InitStruct)
c. Configure and select the multiplexing function, call the firmware library function GPIO_PinAFConfig (form parameter, form parameter, form parameter) to determine the multiplexing function of the pin
d. Initialize the serial port, initialize the USART structure, set the baud rate, data bit length, stop bit, check bit selection, hardware flow control mode selection, USART mode control (enable receiving/transmitting), and finally complete the configuration through USART_Init(...)
e. If interrupt execution service is required, initialize NVIC (Nested Vectored Interrupt Controller). If not, skip
f. Enable the serial port and call USART_Cmd (.....) to enable USART1
2. Write a data transmission function. Since USART_DR has only 8 bits valid, it is divided into byte transmission, double-byte transmission, string transmission, and array transmission. These four transmissions are based on the firmware library function USART_SendData (...). When sending a byte, determine whether TXE is 1. If it is 1, prepare to send the next data to the transmission data register; if it is 0, wait. The firmware library function for obtaining the interrupt status bit is ITStatus USART_GetITStatus (...). Finally, it can be determined whether TC is 1, that is, whether the transmission is completed.
3. Write the data receiving function and call the firmware library function USART_ReceiveData (...) to obtain the latest USARTx data. Judge RXNE, 1 means the reception is complete, 0 means no data is received, so the content of the data receiving function should be written under the condition that RXNE is judged to be 1.
4. Just call the main() function.
According to the Wildfire tutorial, reference manual, and novice understanding, I just reviewed it for my own reference. If there is anything wrong, please point it out. If it is not well written, please forgive me.
As a novice, there are still many questions I don't understand. 1. If parity check is set, should the receiver handle it? How to handle it? If this is the case for MCU to PC communication, what about MCU to MCU?
2. To send 8-bit data, you need to write a function, and to send 16-bit data, you need to write another function. When sending an array, you can only send an array of 8 bytes. Maybe I don’t know enough and I don’t understand it.
Previous article:STM32F429 library function I2C read EEPROM
Next article:Necessary configuration when using the firmware library to operate STM32F4
Recommended ReadingLatest update time:2024-11-23 11:33
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- 【GD32E503 Review】——04.MCU CoreMark Performance Test
- [RTT & Renesas high performance CPK-RA6M4] 3. PWM to achieve breathing light evaluation
- Comparison of IIC data and logic diagram between the reference prototype and your own product
- MicroPython Newsletter Issue 6
- Why is the line so thick? How can I remove the two dots? Reducing the grid size doesn't seem to work, and I can't capture the endpoints of the component.
- Please help me write a program to monitor the status of 2 buttons. Is it really that difficult? I just can't figure it out!
- The underlying technology that opens the LPWAN 2.0 era: Advanced M-FSK (Part 2)
- Design of TPS53355 ripple injection circuit
- Most of the Harbour employees were recruited by Huawei
- Ultra-low power multi-sensor data logger with NFC interface