STM8--UART2

Publisher:ArtisticSoulLatest update time:2020-09-14 Source: eefocusKeywords:STM8  UART2 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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!


Keywords:STM8  UART2 Reference address:STM8--UART2

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

STM8 MCU ADC continuous sampling mode
The internal ADC of the STM8S003 microcontroller is 12 bits, and each channel of the A/D conversion can perform single and continuous conversion modes. Single conversion mode means that the ADC will stop converting after converting data once. If it needs to continue converting, it is necessary to manually start the
[Microcontroller]
STM8 MCU ADC continuous sampling mode
Teach you how to play with STM8 microcontroller!
It is a very normal step-by-step learning method to learn the entire computer system from the microcontroller. Because until today, the computer system has not escaped this most basic architecture. Unfortunately, I skipped this process and started directly from Intel 486. At that time, I was struggling forward with co
[Microcontroller]
Teach you how to play with STM8 microcontroller!
STM8 EEPROM Experience
For STM8, its internal EEPROM is indeed a good thing, and the price of STM8S103/105 is already very cheap. Of course, it can also be replaced by STM8S003/005, and the price is even cheaper, about 1.2/2.0 yuan, which is about 1 yuan cheaper than 103/105. Some netizens also said that in fact, the wafers of these two ser
[Microcontroller]
STM8 study notes-----uart1 serial port interrupt
Uart1 serial port send and receive interrupts. Target: When the serial port receives 1, LED1 lights up; when it receives 2, LED2 lights up; when other characters are received, LED1 and LED2 are turned off. Proceed as follows: 1. Write led.h and led.c files. 2. Write uart.h and uart.c files, where the serial po
[Microcontroller]
STM8's interrupt system and external interrupt details
STM8 has a maximum of 32 interrupt systems, and the interrupt processing is similar to the cortexm series chips. First, each interrupt vector is solidified inside the system, and the user needs to write the interrupt processing function to the corresponding interrupt vector flash location. Second, each interrupt vecto
[Microcontroller]
Introduction of Weixue Electronics STM8 QFP44 Test Socket
STM8 dedicated programming seat burning seat QFP44 0.8mm original imported seat Only for STM8 QFP44 package 0.8mm pin spacing MCU programming and testing, support models see the introduction Model STM8-QFP44 Product Introduction Product Usage Program and test the STM8 QFP44 package 0.8mm pin spacing microco
[Microcontroller]
Introduction of Weixue Electronics STM8 QFP44 Test Socket
STM8 MCU ADC continuous scanning mode
  When the STM8 microcontroller uses the ADC function to read the values ​​of multiple channels, the continuous mode can be used. However, the continuous mode can only sample the value of one channel at a time. So what if you want to sample multiple channels? STM8 provides a multi-channel continuous sampling scan mode
[Microcontroller]
STM8 MCU ADC continuous scanning mode
STM8 hardware circuit design basics
  In fact, STM8 does not need a crystal oscillator and reset circuit, but in order to ensure the stable operation of the minimum system, it is best to add an external circuit.   ● STM8 reset circuit design   The STM8 microcontroller has a built-in power-on reset (POR), so the STM8 microcontroller can be reset norma
[Microcontroller]
STM8 hardware circuit design basics
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号