UART starts communication with a start bit, which is a low level output from the TX pin. Following the start bit is the 8-bit or 9-bit data to be sent. If there is a parity check, the data is followed by the parity check data information, and finally the stop bit, which can be set to 1, 2, or 1.5.
Sending configuration and single-byte communication process:
Register introduction:
Control Register 1 (UART_CR1):
Control Register 2 (UART_CR2):
Control Register 3 (UART_CR3):
Baud Rate Register 1 (UART_BRR1):
Baud Rate Register 2 (UART_B):
Status Register (UART_SR):
Data Register (UART_DR):
manual:
1: Set data length, parity check, stop bit, interrupt usage, etc. The operation registers are CR1, CR2, CR3. The following is my serial port configuration code.
/*urat2 initialization*/
void uart2_init()
{
UART2_CR1=0X14; //8 data, 1 parity check, 1 stop, enable receive interrupt
UART2_CR2=0X2c;
UART2_CR3=0X00;
UART2_BRR2=0X03; //Baud rate configuration --9600
UART2_BRR1=0X68;
}
2: Send data: The code is as follows. It is very simple. Just write data into the register.
UART2_CR2&=0Xf7;
UART2_DR=uart_put; //Serial port sends
UART2_CR2|=0X08;
while((UART2_SR&0X80)==0);//Wait for sending to complete
3: Receive output: I used the receive interrupt, so I received data in the interrupt processing function
#pragma vector=UART2_R_RXNE_vector
__interrupt void UART2_IRQHandler (void)
{
while((UART2_SR&0X20)==0);//Wait for receiving to complete
uart_get=UART2_DR; //Read the received value and clear the flag bit
}
Notice:
1: In STM8S, the M bit defines the frame length, not the length of the data bit! That is to say, the length located by the M bit is the sum of the number of "data bits + parity bits". When the data bit is 8 bits, when parity check is not used, the length of M is 8 bits; when parity check is used, the length of M should be 9 bits! Therefore, when writing a program, you cannot simply change the setting of the check bit;
2: Looking at the STM8S reference manual, we found that Bit0 in the status register UART_SR is PE, which indicates a parity error: PE=0, no parity error; PE=1, parity error. Under what circumstances is the PE flag cleared? We can see in the reference manual: To clear the PE flag, the software must follow the following sequence of operations: first read UART_SR, then read UART_DR. The program needs to detect PE and then determine whether to receive, because no matter what parity mode the sender uses or no parity, the microcontroller serial port will receive data.
For example, I encountered such a problem during the sending process:
Sender: 9600, N, 8, 1 (the number of data bits sent is 8) sends a byte 0xC5 1100 0101
Recipient: 9600, O, 8, 1
The receiver's PE bit is 0 and can be connected to 0xC5; at this time, the sender's stop bit is used as a check bit by the receiver. Since it is odd parity, and the check bit is 1 at this time, only data containing an even number of 1s can be received correctly.
3: When using the serial port debugging tool, please note that the parity bit of some serial port tools does not work. The parity bit of the sscom42 test can be used!
Previous article:stm8 notes 1-Building the project + lonely little light flashing
Next article:stm8 note 3-ad continuous sampling
Recommended ReadingLatest update time:2024-11-16 14:55
- 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
- [nRF52840 DK Review] Custom BLE Services
- Download and get a gift | Keysight Technologies [Cheats on how to choose and use oscilloscope probes]
- Correct timing
- msp430g2553 microcontroller
- Thank you + my family for your understanding and support! + Thank you to my new colleagues for their generous help along the way! + Thank you to the leaders of my company for their trust in me! + Thank you to the EE forum for providing me with professional knowledge!
- TI releases the latest industrial electronics reference design!
- Understanding wireless communication technology
- Infineon Tmall flagship store offers huge discounts Part 1 - Limited cashback on a first-come, first-served basis, Double 11 special promotion!
- Good evening, experts.
- You may need oscilloscope information urgently, especially analog ones. You can get the information here.