Serial communication is one of the most basic and important functions of microcontroller learning. Serial communication can be used indirectly as a debugging interface to achieve communication between the microcontroller and the computer. Of course, it can communicate with some modules (such as Bluetooth, WiFi), and can also be used as a tool for communication with other microcontrollers.
Main features of STM8S's universal asynchronous receiver and transmitter (UART):
● Full-duplex, asynchronous communication
● Programmable data word length (8 or 9 bits)
● Configurable stop bits - supports 1 or 2 stop bits
Generally, when we use serial communication, we are mainly concerned with several parameters: baud rate, stop bit, and parity bit.
The second is how to send and receive data. Here we introduce the use of blocking method to send data and interrupt method to receive data.
The following code is to initialize the serial port, the parameters are: 115200 1 stop bit no parity bit
void Init_UART1(void)
{
CLK_PCKENR1 |= 0X04; //Enable USART1 clock
UART1_CR1=0x00;
UART1_CR2=0x00;
UART1_CR3=0x00;
// BRR2 must be written first
// For example, when the baud rate is 115200, the division factor = 16000000/115200 = 139
// The corresponding hexadecimal number is 008B, BBR1=08, BBR2=0B
UART1_BRR2=0x0B;
UART1_BRR1=0x08;
UART1_CR2=0x2c; // Allow receiving, sending, and enable receiving interrupt
}
The baud rate setting needs to be calculated. The default main frequency of STM8S is 16M, and the number written to the register is 139. Note that BRR2 needs to be written first and then BRR1, and the format is special:
BRR1 should be sandwiched between BRR2! (You need to understand the need to modify the baud rate)
The programming manual describes it as follows:
The blocking send function (sending one byte) is as follows:
void SendChar(u8 dat)
{
while((UART1_SR & 0x80)==0x00);
UART1_DR = dat;
}
Just load the data into the UART1_DR register and it will be OK.
The terminal receives data function as follows:
//Send the received data again
#pragma vector = UART1_R_OR_vector // 0x19
__interrupt void UART1_R_OR_IRQHandler(void)
{
u8 res;
res = UART1_DR;
return;
}
To use interrupts, you need to declare the interrupt function entry and specify the interrupt table number (the serial port 1 receiving interrupt vector is 0x19):
#pragma vector = UART1_R_OR_vector // 0x19
__interrupt void UART1_R_OR_IRQHandler(void)
As soon as the serial port receives a byte of data, it will immediately enter this function and store the data in the UART1_DR register.
Note: To use the interrupt function, you need to add an interrupt enable statement in the main function:
_RIM;
Previous article:Usage of UART in stm8s (four types of UART interrupts)
Next article:STM8L151 series uses timer timing, global ticker jiffes
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- What does this warning mean and how to deal with it?
- I have seen many articles saying that the IO port of STM32 can be set to 8 modes. How to set it? Use the control register to set...
- Xunwei IMX6ULL development board installs VMware Tool
- Problem of filter network with two probes
- Design scheme for realizing communication between upper and lower computers via USB
- Which parameters in the datasheet of the five devices, diode, triode, MOS tube, op amp, and optocoupler, can reflect...
- I want a Zircon A4 FPGA board (without downloader). If you have one, please contact me.
- Is inductance or magnetic beads used to isolate the digital power supply from the analog power supply?
- Introduction to RISC-V Toolchain
- Crazy Shell AI open source drone GPIO (LED flight status light, signal light control)