Finally, I can't help but complain about the students who translated the STM 32 function library manual. A lot of things were omitted in the middle, and they were very important things. . . . . My heart almost collapsed!!!
Now let's talk about the pitfalls of USART serial communication in STM 32 function library 3.5:
In some tutorials and Chinese manuals, the basic configuration source code of USART is as follows
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_Odd;
USART_InitStructure.USART_HardwareFlowControl =
USART_HardwareFlowControl_RTS_CTS;
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_InitStructure.USART_Clock = USART_Clock_Disable;
USART_InitStructure.USART_CPOL = USART_CPOL_High;
USART_InitStructure.USART_CPHA = USART_CPHA_1Edge;
USART_InitStructure.USART_LastBit = USART_LastBit_Enable;
USART_Init(USART1, &USART_InitStructure);
Only one structure variable is declared above: USART_InitTypeDef USART_InitStructure;
Then we compile and find that the compilation error is reported, and the following clock variables are not defined
USART_InitStructure.USART_Clock = USART_Clock_Disable;
USART_InitStructure.USART_CPOL = USART_CPOL_High;
USART_InitStructure.USART_CPHA = USART_CPHA_1Edge;
USART_InitStructure.USART_LastBit = USART_LastBit_Enable;
Why is this? I clearly wrote it according to what the manual says 55555. . . .
As a last resort, I had to use our great Baidu to find out. Through some information and posts from netizens, I finally found the reason. It turned out that in version 3.5 of the function library, when initializing the USART default value, there is not only one structure variable USART_InitStructure, but also a structure variable for USART clock management:
USART_ClockInitTypeDef USART_ClockInitStructure;
So the final USART configuration function is:
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;
/*Baud rate 9600 8-bit data length 1 stop bit no parity bit
Disable hardware flow control Disable USART clock Clock polarity low Capture data on second edge
The clock pulse of the last bit of clock data is not output from SCLK*/
USART_InitStructure.USART_BaudRate =9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl =USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
USART_ClockInitStructure.USART_CPOL = USART_CPOL_High;
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
USART_Init(USART1, &USART_InitStructure);
USART_ClockInit(USART1 , &USART_ClockInitStructure);
Of course, don't forget to enable USART: USART_Cmd(USART1, ENABLE); and turn on the clock of the corresponding USART.
Compile again and you will find that the compilation is correct, but this does not mean that you can use USART to send a "Hello World" to your computer. Continue down. Since almost all IO ports of STM32 are multiplexed, that is to say, an IO port is not only a regular input and output, it can also be used as an interface for on-chip peripherals such as USART/IIC/ADC. So when you use USART, you are actually sending and receiving data through the IO port. At this time, we also need to configure the corresponding IO port. Take USART1 as an example. Generally, it is multiplexed with ports 9 and 10 in GPIOA. If you are not sure, you can check the official manual of STM32, where GPIOA.9 is the data transmission port, i.e. TXD, and GPIOA.10 is the data receiving port RXD. Then we can configure it. The specific source code is as follows:
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //Note!! There is a very important point here, because USART is multiplexing port GPIOA.9, it is not a normal OUT but multiplexing AF
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
Well, now we have completed all the configurations of USART. Can't wait to send a message to PC through the development board? By using two functions
USART_ SendData and USART_ ReceiveData in the main function, we can realize the two-way communication between the development board and PC~~
Previous article:How to use and program the STM32 serial port USART1
Next article:Using DMA+USART3 to transfer data on STM32
Recommended ReadingLatest update time:2024-11-16 14:32
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- MSP430F123 experience
- Please help me look at this program
- Level conversion
- Request gerber to pcb altium designer format
- What is PWM? Principle of PWM
- Answer questions to win gifts | World Peace Group NXP Solutions Show
- [CH563L Review] Part 3 Based on uCos, using FIFO algorithm to achieve zero wait, any baud rate FreeModbus RTU
- Electrical knowledge encyclopedia
- C6678 Learning - Interrupt Nesting
- The role and selection of input and output capacitors in power supplies