Introduction to serial port and implementation of serial port communication

Table of contents

Basic understanding of serial port

About electrical standards and protocols

RS-232

RS-422

RS-485

About the level of the serial port

RS232 level

TTL level

Serial port wiring method

Configuration of related registers and working mode of serial port

Introduction to common functions of stm32HAL library

Serial port sending and receiving functions

Serial port interrupt callback function

Status tag variable: USART_RX_STA

Serial port reception interrupt process

Serial port experiment (non-interruption)

Programming implementation

Serial port experiment (interruption)

Programming implementation


Basic understanding of serial port

Serial interface, referred to as serial port, is also called serial communication interface or serial communication interface (usually referred to as COM interface). It is an expansion interface that uses serial communication. Serial Interface refers to the sequential transmission of data bit by bit. Its characteristic is that the communication line is simple, and only a pair of transmission lines can achieve two-way communication (telephone lines can be directly used as transmission lines), thus greatly reducing costs, and is especially suitable for long-distance communication, but the transmission speed is slow
Features:
        It is a method of wired communication between devices.
        Data is transmitted sequentially bit by bit
        Two-way communication, full duplex
        Transfer speed is relatively slow

About electrical standards and protocols

Serial interfaces are classified according to electrical standards and protocols, including RS-232-C, RS-422, RS485, etc. RS-232-C, RS-422 and RS-485
The standard only specifies the electrical characteristics of the interface and does not involve connectors, cables or protocols.

RS-232

Also called standard serial port, the most commonly used [serial communication interface, such as the 9-pin serial port of our computer host, the maximum rate is 20kb/s
RS-232 is designed for point-to-point (that is, only one pair of receiving and sending devices) communication, and its maximum transmission distance is about 15 meters. So RS-232 is suitable for communication between local devices

RS-422

Since the receiver uses high input impedance and the transmit driver has stronger driving capability than RS232, it allows multiple receiving nodes to be connected on the same transmission line, up to 10 nodes. That is, there is one master device (Master) and the rest are slave devices (Slave). The slave devices cannot communicate with each other, so RS-422 supports point-to-many two-way communication. The maximum transmission distance of RS-422 is 1219 meters, and the maximum transmission rate is 10Mb/s. The length of a balanced twisted pair is inversely proportional to the transmission rate.

RS-485

It is developed on the basis of RS-422. No matter the four-wire or two-wire connection method, up to 32 devices can be connected to the bus.

About the level of the serial port

Asynchronous serial refers to UART (Universal Asynchronous Receiver/Transmitter), universal asynchronous reception/transmission.
UART includes TTL level serial port and RS232 level serial port

RS232 level

Logic 1 is a voltage of -3~-15V, logic 0 is a voltage of 3~15V
        The notebook communicates with the microcontroller through RS232 levels

TTL level

TTL is Transistor-Transistor Logic , which is the abbreviation of Transistor - Transistor Logic. It is a device controlled by a computer processor.
Standard technology for communication between internal parts. TTL level signals are widely used because their data representation adopts binary regulations.
+5V is equivalent to a logic "1" and 0V is equivalent to a logic "0". In digital circuits, the level of a circuit composed of TTL electronic components is a voltage range, stipulated:
Output high level >=2.4V , output low level <=0.4V ;
Input high level >=2.0V , input low level <=0.8V;
The laptop communicates with the microcontroller through TTL levels
TX transmit line (port) 3.1
RX receiving line ( port) 3.0
USB to TTL , using ch340 communication

 

Serial port wiring method

RXD: data input pin, data acceptance

TXD: data transmission pin, data transmission

Configuration of related registers and working mode of serial port

 How is the character 'a' uploaded from the microcontroller to the PC ?

The ASSII code of a is 97 , which is 0x61 in hexadecimal and 01010001 in binary . These 8 bits are the data bits.
Serial port working mode 1, one frame of data has 10 bits, start bit 0 + data bit + stop bit 1
Then one frame of data of a is 0 1000 1010 1
The secret codes of both parties match before sending data, so there is the concept of start bit and stop bit.

 There are 8 bits in a byte. For example, the ASSII code of the letter 'a' is 97 in decimal and 0110 0001 in binary. It is sent from the bit to the high bit at a time and received as well.

Introduction to common functions of stm32HAL library

Serial port sending and receiving functions

HAL_UART_Transmit(); // Serial port sends data, using timeout management mechanism

HAL_UART_Receive();//The serial port receives data and uses the timeout management mechanism

HAL_UART_Transmit_IT();//Send data in serial port interrupt mode

HAL_UART_Receive_IT(); //Serial port interrupt mode receives data

HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart,
uint8_t *pData, uint16_t Size, uint32_t Timeout)
Function: Send specified bytes of data in a blocking manner
        Formal parameter 1 : UART_HandleTypeDef structure type pointer variable
        Formal parameter 2 : Points to the data address to be sent
        Formal parameter 3 : The size of the data to be sent, in bytes
        Formal parameter 4 : Set timeout time, in ms
HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart,
uint8_t *pData, uint16_t Size)
Function: Receive specified bytes of data in an interrupt manner
        Formal parameter 1: UART_HandleTypeDef structure type pointer variable
        Formal parameter 2: Points to the receive data buffer
        Formal parameter 3: Size of data to be received, in bytes
After this function is executed, the interrupt will be cleared and needs to be called again to re-enable the interrupt.

Serial port interrupt callback function

HAL_UART_IRQHandler(UART_HandleTypeDef *huart); // Serial port interrupt processing function
HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart); // Send interrupt callback function
HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart); // Receive interrupt callback function

Status tag variable: USART_RX_STA

Starting from 0 , the serial port interrupt will increase by 1 when receiving a data (one byte) . When the data reading is all OK (carriage return and line feed
symbol comes), then the highest bit of USART_RX_STA is 1, indicating that all serial port data reception is completed, and then main
Data can be processed in the function.

Serial port reception interrupt process

 

Serial port experiment (non-interruption)

1. Select the serial port

2. Select mode
Asynchronous communication

3. Serial port configuration

 4. Use MicroLIB library

Open from magic wand
Programming implementation
# include <stdio.h>
# include <string.h>
unsigned char ch [ 20 ] = { 0 };
int fputc ( int ch , FILE * f )
{
        unsigned char temp [ 1 ] = { ch };
        HAL_UART_Transmit ( & huart1 , temp , 1 , 0xffff );
        return ch ;
}
In the main function:
unsigned char ch [ 20 ] = { 0 };
HAL_UART_Transmit ( & huart1 , "hello world\n" , strlen ( "hello world\n" ), 100 );
while ( 1 )
{
        HAL_UART_Receive ( & huart1 , ch , 19 , 100 );
        //HAL_UART_Transmit(&huart1, ch, strlen(ch), 100);
        printf ( ch );
        memset ( ch , 0 , strlen ( ch ));
}

Serial port experiment (interruption)

Same as above for the first 4 steps

5. Turn on interrupts
Programming implementation
# include <stdio.h>
// Serial port receiving buffer ( 1 byte)
uint8_t buf = 0 ;
// Define the maximum number of received bytes 200 ( can be adjusted according to needs)
# define UART1_REC_LEN 200
// Receive buffer , the data received by the serial port is placed in this array, the maximum is UART1_REC_LEN bytes
uint8_t UART1_RX_Buffer [ UART1_REC_LEN ];
// Receive status
// bit15 , reception completion flag
// bit14 , received 0x0d
// bit13~0 , the number of valid bytes received
uint16_t UART1_RX_STA = 0 ;
// Reception completion callback function, after receiving a data, process it here
void HAL_UART_RxCpltCallback ( UART_HandleTypeDef * huart )
{
        // This function is called for all triggers to determine which serial port triggered the interrupt.
        if ( huart -> Instance == USART1 )
        {
                // Determine whether the reception is completed (UART1_RX_STA bit15 is 1 )
                if (( UART1_RX_STA & 0x8000 ) == 0 )
                {
                        // If 0x0d (carriage return) has been received,
                        if ( UART1_RX_STA & 0x4000 )
                        {
                                //Then determine whether 0x0a (line feed) is received
                                if ( buf == 0x0a )
                                        //If both 0x0a and 0x0d are received, set bit15 to 1
                                        UART1_RX_STA |= 0x8000 ;
                                else
                                        // Otherwise, consider receiving error and start again
                                        UART1_RX_STA = 0 ;
                        }
                        else // If 0x0d (carriage return) is not received
                        {
                                //Then first determine whether the received character is 0x0d (carriage return)
                                if ( buf == 0x0d )
                                {
                                        //If yes, set bit14 to 1
                                        UART1_RX_STA |= 0x4000 ;
                                }
                                else
                                {
                                        // Otherwise, save the received data in the cache array
                                        UART1_RX_Buffer [ UART1_RX_STA & 0X3FFF ] = buf ;
                                        UART1_RX_STA ++ ; // If the received data is larger than UART1_REC_LEN ( 200 bytes), restart reception
                                        if ( UART1_RX_STA > UART1_REC_LEN - 1 )
                                                UART1_RX_STA = 0 ;
                                }
                        }
                }
                                // Re-enable interrupts
                                HAL_UART_Receive_IT ( & huart1 , & buf , 1 );
        }
}
int fputc ( int ch , FILE * f )
{
        unsigned char temp [ 1 ] = { ch };
        HAL_UART_Transmit ( & huart1 , temp , 1 , 0xffff );
        return ch ;
}
main function part
HAL_UART_Receive_IT ( & huart1 , & buf , 1 );
while ( 1 )
{
        /* USER CODE END WHILE */
        /* USER CODE BEGIN 3 */
        //Judge whether the serial port reception is completed
        if ( UART1_RX_STA & 0x8000 )
        {
                printf ( " Received data: " );
                //Send the received data to the serial port
                HAL_UART_Transmit ( & huart1 , UART1_RX_Buffer , UART1_RX_STA & 0x3fff , 0xffff );
                // Wait for sending to complete
                while ( huart1 . gState != HAL_UART_STATE_READY );
                printf ( "\r\n" );
                //Restart next reception
                UART1_RX_STA = 0 ;
        }
                printf ( "hello liangxu\r\n" );
                HAL_Delay ( 1000 );
}

Guess you like

Origin blog.csdn.net/2301_77164542/article/details/131351699