STM32 serial port detailed explanation

Publisher:seanxnieLatest update time:2019-04-12 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

What is a Serial Port

UART: Universal Asynchronous Receiver/Transmitter

USART: Universal Synchronous Asynchronous Receiver/Transmitter


One is the most commonly used and simplest serial data transmission protocol. Only two data lines are needed to achieve full-duplex.


Tx: Send data line

Rx: Receive data line


    A B

  TX -----------> Rx

  Rx <------------Tx


Full-duplex: Two devices can send and receive at the same time

Serial data: Only one wire is used for transmission and only one bit can be sent at a time. One bit is sent and received one bit after another.


Module communication: upper computer lower computer

Communication generally requires two devices, which we call the upper computer and the lower computer.

Host computer: A machine with strong processing performance is called host computer. Most of the data processing is done on the host computer.

Lower computer: The terminal for data collection, a machine with single processing performance, is called the lower computer.


The serial port has only one data transmission line. If A wants to send a character data 10101010 to B


 A -------- ---------

  -------- ------- ...

   

 How long is the high level period? Even if no data is sent, there is a level state on the Tx line.

 How does it know you are sending? ....


How is UART data transmitted?

UART protocol serial port protocol.

The serial port sends and receives data in frames.


1 frame = 1 start bit + 5-9 bits of data + 0/1 bit of parity + stop bits (0.5, 1,1,5,2)

Start bit: one cycle of low level

Data bits: 5-9 bits. The exact number of bits needs to be negotiated by both parties. The transmission is LSB (least significant bit) first…MSB

Check Digit:

 0 bit: no check bit

1 bit: check bit. "Lai Zi" X

D0 D1 D2 … Dn X

Odd parity: ensure that the number of transmitted 1s is an odd number

D0 D1 D2 … Dn X Make sure the number of 1s in this string of data is an odd number

1 0 1 0 1 0 1 0 X(1)

1 0 1 0 1 0 1 1 X(0)


Even parity: To save the transmitted 1 as an even number



Stop bit: High level

0.5 stop bit. High level for half a cycle

1 stop bit    

1.5 stop bits

2 stop bits


Baudrate: Transmission rate.

Determine the time period.


115200 bps: bits per second


the physical layer standards


 TTL level UART: TTL level serial port

 RS-232:

 RS-422:

 RS-485:

 

  TTL level UART:

  Logic low (0) 0V

  Logic High(1) 5V/3.3V

  RS-232: Suitable for longer distance transmission

  Logic low level (0) +3v~+15V

  Logic high level (1) -3v~-15v

 


  TTL UART RS-232 RS-422 RS-485


Level 1 3.3V/5V 1 -5V ~ -15V +/- 2v +/- 1.5v

0 0V 0 5V ~ +15V


Signal Single-ended signal Single-ended signal Differential signal Differential signal


Transmission length < 2m <15m <1200m <1200m


 Different serial port standards have different pins. TX/Rx must exist.

  TTL

  RX

  TX

  GND

  VCC


STM32F4xx UART controllers


TX

RX


Hardware flow control:

RTS: Request To Send

The terminal tells the other party that it can transmit data to me.


 CTS: Clear To Send Clear to Send signal

  The other party tells the terminal, I want to send you data

 

 RTS -------> CTS (other party)

 CTS <------- RTS (other party)


Flags:


 TXE: Transmit data Register Empty

  The transmit data register is empty.

  It does not mean that the last data has been sent, because the data may still be in the shifter.

  But now you can write data to TDR.

 

 TC : Transmit Complete

  Sending completed.

  The data in the transmit shift register has been sent to the Tx pin.

 

 RXNE: Read Data register Not Empty

  The receive data register is not empty, which means you can read data.


STM32F4xx serial port code flow

(1) GPIO port configuration

The TX/RX pins of the serial port are multiplexed by the GPIO port.

a. Enable the clock of the GPIO group

RCC_AHB1PeriphClockCmd

b. Configure the GPIO port function GPIO_Init

c. Select specific reuse functions

GPIO_PinAFConfig

(2) usart configuration

a. Enable usart clock

b. USART_Init


  USART_Init(USART_TypeDef * USARTx, USART_InitTypeDef * USART_InitStruct);

 

  typedef struct

  {

  uint32_t USART_BaudRate; // baud rate


  uint16_t USART_WordLength; //Transmission word length, choose one of the following two:

  USART_WordLength_8b

  USART_WordLength_9b

  In STM32: Transmission word length = number of data bits + number of check bits


  uint16_t USART_StopBits; //Stop bit number, as follows:

  USART_StopBits_1 1bit stop bit

  USART_StopBits_1_5 1.5bit stop bit

  USART_StopBits_2 2 bits stop bit

  USART_StopBits_0_5 0.5bit stop bit


  uint16_t USART_Parity; //Verification method, as follows:

  USART_Parity_No No parity

  USART_Parity_Odd odd parity

  USART_Parity_Even Even parity


  uint16_t USART_Mode; //Serial port mode, as follows: can be combined

  USART_Mode_Tx Transmit mode

  USART_Mode_Rx Receive mode


  USART_Mode_Tx | USART_Mode_Rx Transmit and Receive modes

 

  uint16_t USART_HardwareFlowControl; //Hardware flow control

  USART_HardwareFlowControl_None No hardware flow control

  USART_HardwareFlowControl_RTS RTS Request to send. You can receive data from the other party.

  USART_HardwareFlowControl_CTS CTS Clear to send, you can send data to the other party.

  USART_HardwareFlowControl_RTS_CTS RTS_CTS Use flow control for both sending and receiving

  } USART_InitTypeDef;


(3) Interrupt configuration

USART_ITConfig <- configure serial interrupt

In STM32, one USART corresponds to only one interrupt channel, but the interrupt that causes the serial port

There are many events, such as:

TXE -> The transmit register is empty, which can cause a serial port interrupt

TC -> Sending completed, can cause serial port interruption

RXNE -> The receiving register is not empty, which can cause a serial port interrupt

However, these events require "interrupt control bit enable"


  USART_ITConfig(USART1, USART_IT_RXNE,ENABLE);

  USART_ITConfig is used to configure a serial port's XX event to cause a serial port interrupt.

 

  In the serial port interrupt function, you need to determine which serial port event caused the interrupt!!!

 

  NVIC_Init()


(4) Enable the serial port

USART_Cmd


 Receive (interrupt function)

  USART1_IRQHanlder()

  {

  if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)

  {

  //There is data to read

 

  data = USART_ReceiveData(USART1); //Read the received data

  }

 

 

 

  // Clear the interrupt flag of USART1

  USART_GetITStatus

  }

 USART_GetITStatus is used to obtain the xx event flag of the serial port


 send data

 USART_SendData(USART1, 0x55);

 while (USART_GetFlagStatus(USRAT1, USART_FLAG_TXE) == RESET); //Wait for sending to end 


Keywords:STM32 Reference address:STM32 serial port detailed explanation

Previous article:STM32F4 USART1 TX RX FIFO
Next article:Data exchange between STM32F103 serial port 1 and serial port 2 at different baud rates

Recommended ReadingLatest update time:2024-11-16 13:54

STM32 uses SysTick for accurate timing
SysTick is briefly mentioned in ST's data sheet but not in detail. Let's take a closer look here. Please correct any errors.   SysTick is located in NVIC. It is mainly used in the operating system, so we rarely use it normally, but we can use it to do simple delay, which is relatively accurate.   Then let's take a loo
[Microcontroller]
STM32 uses SysTick for accurate timing
stm32.cube(四)——HAL.ADC
1. Adc characteristics 1.1 Adc Overview The Adc of Stm32 has 12-bit precision, with 16 external channels and 2 internal channels. A/D conversion of different channels can be performed in single, continuous, scan or intermittent mode. Its other features include support for analog watchdog and DMA. 1.2 Adc initializat
[Microcontroller]
STM32 ADC clock configuration
1. Determination of STM32 ADC sampling frequency First, let’s look at some information to determine the STM32 ADC clock: (1) The ADCCLK clock provided by the clock controller is synchronized with PCLK2 (APB2 clock). The CLK controller provides a dedicated programmable prescaler for the ADC clock. (2) In general, the
[Microcontroller]
Design of intelligent parameter tester based on STM32
    0Introduction     Product testing is a concern for both manufacturers and users. In the process of product production, testing is an indispensable part, and sometimes even a process step. Electromagnetic relays are commonly used switching elements in power systems and other electrical control systems. Thei
[Microcontroller]
Design of intelligent parameter tester based on STM32
My MCU Methodology 2: Special Edition of STM32 MCU
My MCU Methodology Part 2 STM32 MCU Special Edition Written by zzw YanJun.tech Because I was really busy during this period, and of course because of my procrastination, I have not started writing this STM32 microcontroller learning experience. On the afternoon of the third day of the Chinese New Year, I was a little
[Microcontroller]
Thoughts on STM32 port initialization
Problem Description: When working on a BCM project, I found a problem. That is, at the moment of power-on, I found that the light flickered. From the phenomenon, it should be the default value of the port during the initialization process of the BCM controller that caused the external light to flicker. problem solved:
[Microcontroller]
STM32 standard library SPI initialization
The initialization of SPI is relatively simple, but it is best to understand the principle, after all, it is easier to troubleshoot problems. SPI initialization process: 1. Initialize GPIO 2. Initialize the SPI_InitStructure structure 3. Enable SPI peripherals The SPI initialization structure is shown in the figure be
[Microcontroller]
Some issues about STM32 inexplicable crashes
Problem Description ZET6 runs the ucosII system. Sometimes it freezes during operation. After hardware debugging, it is found that this problem is caused by the delay function. There is no problem with the delay function, and this problem occurs occasionally. Troubleshooting After the crash, the pointer points to
[Microcontroller]
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号