STM8S_004_UART basic data transmission and reception

Publisher:温柔的心情Latest update time:2017-09-16 Source: eefocusKeywords:STM8S Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Ⅰ. Write in front

People who do software development know the importance of printing information. When it comes to printing information, we have to talk about UART serial port printing. For people who do microcontroller development, the main source of printing information is UART serial port printing. Therefore, knowledge about UART is more important.

 

Standard functions such as printf and scanf can not only be used on computers, but also on our microcontrollers. This article only describes the basic knowledge about UART. I will write another article about printf and scanf later.

 

This article will combine the "STM8S Reference Manual" and software source code to tell you the basic knowledge and usage of UART.

 

For your convenience, the content of this article has been organized into a PDF file:

http://pan.baidu.com/s/1i5uWhJR

 

Author:strongerHuang

All rights reserved. Any other commercial use is prohibited without permission!!!

 

Ⅱ. UART Basics

Universal Asynchronous Receiver/Transmitter (Universal Asynchronous Receiver/Transmitter), commonly known as UART, is an asynchronous receiver/transmitter.

 

The universal synchronous asynchronous receiver/transmitter (UART1, UART2 or UART3) of the STM8S microcontroller family provides a flexible method for full-duplex data exchange with external devices using the industry standard NZR asynchronous serial data format. The STM8 UART provides a wide range of baud rate selections and supports multi-processor communication.

 

Key Features:

Ø Full-duplex, asynchronous communication

Ø High-precision baud rate generator system: programmable baud rate for both sending and receiving, up to 2.5Mbits/s

Ø Programmable data word length (8 bits or 9 bits)

Configurable stop bits - supports 1 or 2 stop bits

Ø Single-line half-duplex communication (UART1)

Ø Detection flag: receive buffer full, send buffer empty, transmission end flag

Ø 6 interrupt sources with flags

Ø 2 interrupt vectors

Ø Low power mode

 

UART Synchronous Mode

The UART_CK pin is the output of the UART transmitter clock. There is no clock pulse on the UART_CK pin during the start and stop bits. Depending on the state of the LBCL bit in the UART_CR3 register, the transmitter decides to generate or not generate a clock pulse during the last valid data bit. The CPOL bit in the UART_CR3 register allows the user to select the clock polarity, and the CPHA bit in the UART_CR3 register allows the user to select the phase of the external clock.

 

In the bus idle frame and disconnect frame, the external CK clock is inactive.

 

Notice:

1.UART_CK pin works together with UART_TX pin. When the UART transmitter is disabled (TEN and REN = 0), UART_CK and UART_TX pins are in high impedance state.

 

2. When both the transmitter and receiver of the UART are disabled (TEN=REN=0), the LBCL, CPOL and CPHA bits must be configured correctly to ensure that the clock pulse works correctly; these bits cannot be changed when the transmitter or receiver is activated.

 

3. It is recommended to set TE and RE in the same instruction to reduce the receiver's setup time and hold time.

 

4. UART only supports master mode: it cannot use the input clock from other devices to receive or send data (SCLK can only be configured as output state).

 

5. The data given in this section is valid only when UART_DIV[3:0] in register UART_BRR2 is 0. Otherwise, the setup time and duration are no longer 1/16 bit time, but 4/16 bit time.

This function option can serially control peripherals composed of shift registers without losing the asynchronous communication function, that is, it can still communicate with other asynchronous transmitters and receivers.

 

UART synchronous transmission example:


 

III. Software Engineering Source Code

1. About the project

The engineering code provided in this article is based on the previous software project "STM8S-A02_TIM Precise Delay (Blocking)" and modified to add UART serial port. Beginners can refer to my previous corresponding basic articles, which are more detailed.

 

The source code of the project mainly implements the following functions: UART serial port basic output + interrupt receiving data. The UART serial port prints the string "Demo..." at an interval of 1s. The serial port receives data through interrupts, and sends a character through the serial port when it is received.

 

This article focuses on the following contents about UART:

UART_Initializes: UART serial port initialization

UART1_SendByte: UART serial port sends byte data

 

2. Code Analysis Description

A.UART_Initializes: Initialization

void UART_Initializes(void)

{

  UART1_Init((uint32_t)115200,UART1_WORDLENGTH_8D,UART1_STOPBITS_1,UART1_PARITY_NO, UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TXRX_ENABLE);

  http://pan.baidu.com/s/1o7Tb9Yq

 

Software source code project (STM8S-A04_UART basic data transmission and reception):

http://pan.baidu.com/s/1c2EcRo0


Keywords:STM8S Reference address:STM8S_004_UART basic data transmission and reception

Previous article:STM8S_005_ADC ​​collects single channel voltage
Next article:STM8S_003_TIM timing interrupt

Recommended ReadingLatest update time:2024-11-16 08:59

Briefly introduce several low power modes of STM8S
There are four low-power modes for STM8S105: wait mode, stop mode, fast active stop mode and slow active stop mode. 1. Waiting mode: You can execute instruction wif() to enter waiting mode. In this mode, the main CPU stops working, but its peripherals do not stop. Strictly speaking, it can only be regarded as reduci
[Microcontroller]
Briefly introduce several low power modes of STM8S
STM8S delay function
/* MAIN.C file  *   * Copyright (c) 2002-2005 STMicroelectronics  */  //Delay function #include "stm8s103f3p.h" void delay_40us(void); void delay_1ms(void); void delay_ms(int); void delay_1s(void); void delay_s(int); main() {  PD_DDR=0x0f;  PD_CR1=0x0f;  PD_CR2=0x00;  PD_ODR=0x00;  while (1)  {    PD_ODR^=0x0f;    d
[Microcontroller]
mini2440 uart serial port experiment (fifo mode + interrupt)
This time is an experiment on the FIFO mode of uart0 on S3C2440. The program sets the number of data contained in the input fifo of serial port 0 to trigger a pulse interrupt at the moment when it changes from a state of less than 16 bytes to a state of greater than or equal to 16 bytes. In this interrupt, all the dat
[Microcontroller]
【STM8S】 Window watchdog
The .h file is as follows:     #ifndef __WWDG_H #define __WWDG_H #include "stm8s.h" void Delay();  void WWDG_Configuration(void) ;   void Refresh_WWDG_Window(void); #endif The .c file is as follows:   #include "wwdg.h" #include "stm8s_wwdg.h"   #define CounterInit 0x7f #define window      0x77   void Delay() //
[Microcontroller]
ARM exception---the triggering process of a Uart interrupt
First some definitions: //2440addr.inc INTOFFSET EQU 0x4a000014 ;Interrupt request source offset   //option.inc _ISR_STARTADDRESS EQU 0x33ffff00 //2440init.s          MACRO $HandlerLabel HANDLER $HandleLabel $HandlerLabel     sub sp,sp,#4 ;decrement sp(to store jump address)     stmfd sp!,{r0} ;PUSH the wo
[Microcontroller]
UART function test of s3c2440
/* File name: UART.c /* Function: The most basic UART sending and receiving /* Author: Wujianqi /* Version: 1.0 #include "2440addr.h" //Includes the settings of 2440 related registers #include "def.h" //The four LEDs correspond to GPB5.6.7.8. #define LED1 5 #define LED2 6 #define
[Microcontroller]
stm8s timer 2 measures pulse width (unit: us)
void Init_Timer2(void) {   GPIO_Init(GPIOA, GPIO_PIN_3, GPIO_MODE_IN_PU_NO_IT); //Input no interrupt   TIM2_TimeBaseInit(TIM2_PRESCALER_16,65536-1); //16 division, 65ms overflow   TIM2_Cmd(ENABLE);   } uint16_t TIM2_GetCapture(void) {   /* Get the Capture  Register value */   uint16_t tmpccr = 0;   uint8_t tmpccrl=0,
[Microcontroller]
STM8S self-study notes: using library functions to light up an LED
Preparation and prerequisites  a. IAR or STVD has been installed  b. Download the standard firmware library (StdPeriph_Lib_V2.1.0) for backup;  c. A program template has been established. If not, please refer to the post STVD+STM8 official firmware library to create a project template in my blog;  d. (Optional) The pr
[Microcontroller]
STM8S self-study notes: using library functions to light up an LED
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号