STM32–HAL–USART(UART) serial communication

RS232 physical layer

There are many standards and variants for the physical layer of serial port communication.

RS-232 Standard:

RS232 Protocol Layer

RS232 serial communication protocol

Level standards

-5v~~-15v means logic 1

+ 5~~ + 15v means logic 0

There is a 2v noise tolerance line, which is 3-5v, and -3~-5v can also detect level logic.

-3~ + 3v cannot be detected.

Communication rate

9600bps 115200bps 38400bps The hardware is perfect and the distance is short, reaching 1Mbps

Communication distance

Theoretically, it can reach 30m

Usually 1~3m

The RS232 protocol is used for data transmission between the microcontroller and the computer.

Data transfer rules:

The length of valid data is often agreed to be 5
,
6
,
7
or
8
Bit length.

USART

In STM32
With multiple USARTs (Universal Synchronous Asynchronous Receiver and Transmitter)

Peripherals are used for serial communication. Universal synchronous asynchronous receiver-transmitter allows flexible full-duplex data exchange with external devices. have

different from
USART
, it also has
UART
peripherals
(Universal Asynchronous Receiver and Transmitter)
, it is based on USART and cuts out the synchronous communication function, only asynchronous communication. The simple distinction between synchronous and asynchronous is to see whether it is necessary to provide external clock output during communication. The serial communication we usually use is basically UART
.

UART has fewer clock lines than USART.

USART
Meets industry standards for external devices
NRZ
asynchronous serial data format requirements and occurs using a fractional baud rate

The converter can provide a variety of baud rates, making its application more extensive.
USART
Supports synchronous one-way communication and half-duplex single-line communication

letter; also supports local area interconnection network
LIN
,smart card
(SmartCard)
agreement with
lrDA(
Infrared Data Association
) SIR ENDEC
regulations

Fan.

stm32–cube configuration uart

1.

Asynchronous: full-duplex asynchronous communication

Synchronous: Synchronous communication in which the sender provides a clock for synchronous transmission. Writing the CLKEN bit in the USART_CR2 register selects synchronous mode. The user can control bidirectional synchronous serial communication in master mode and add the CK pin as the output of the USART transmitter clock.

Single Wire: Single wire half-duplex communication. Single-wire half-duplex mode is selected by setting the HDSEL bit in the USART_CR3 register. The RX pin is no longer used. The TX and RX pins are interconnected within the chip and exchange data with the opposite side through a single-wire half-duplex protocol.

Multiprocessor Communication: Multiprocessor communication. Several USARTs can be connected to one network.

2.

Hardware Flow Control:

Disable: Only achieve basic serial communication through RX and TX

CTS Only: Add CTS pin on the basis of RX and TX to send clear signal. If it is high level, the next data transmission will be blocked when the current data transmission ends.

RTS Only: Add RTS pin on the basis of RX and TX to send request signal. If it is low, it indicates that the USART is ready to receive data. In 485 communication, this pin is required.

CTS/RTS: Add CTS and RTS pins to RX and TX.

3. Configure serial port parameters

Parameter Settings:

Baud Rate: Baud rate. Set through the baud rate register (USART_BRR).

Word Length: word length. The data bits can be 8 or 9 bits, set by the M bit in control register 1 (USART_CR1).

Parity: Parity selection. The parity bit can be selected from None, Even or Odd. Set by the PCE bit and PS bit in control register 1 (USART_CR1).

Stop Bits: Stop bits. The stop bit can be 1 or 2 bits. Set by the STOP bit in control register 2 (USART_CR2).

Data Direction: Data direction. Optional Receive and Transmit, Receive Only, and Transmit Only. Set by the TE and RE bits in control register 1 (USART_CR1).

4. Configure serial port interrupt

5. Generate project files

Commonly used serial port 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();//Serial port interrupt mode transmission
HAL_UART_Receive_IT();//Serial port interrupt mode reception
HAL_UART_Transmit_DMA();//Serial port DMA mode transmission
HAL_UART_Transmit_DMA();//Serial port DMA mode reception

HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, uint8_t *pDat, uint16_t Size)

HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)

HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)

Timeout timeout 100

6. Use printf or customize “printf”

1.Basic version

#include // Include standard input and output header files

int fputc(int ch,FILE *f)
{
//Send 1 byte of data in polling mode, and set the timeout to infinite waiting
HAL_UART_Transmit( & amp;huart1,(uint8_t *) & amp;ch,1,HAL_MAX_DELAY);
return ch;
}
int fgetc(FILE *f)
{
uint8_t ch;
// Use polling mode to receive 1 byte data, and set the timeout to infinite waiting
HAL_UART_Receive( & amp;huart1,(uint8_t*) & amp;ch,1, HAL_MAX_DELAY );
return ch;
}

Note: When calling the scanf() function to read serial port data, a space must be used as the end of the input. Therefore, when entering data in the serial port debugging assistant, it must end with a space and then click the send button, otherwise the data cannot be received correctly.

The scanf() function can only receive strings without spaces. If the user needs to receive a string with spaces, he or she needs to receive it byte by byte first. During the reception process, it is necessary to continuously determine whether the two characters ‘\r’ and ‘\\
‘ are received to determine whether the string is received. Finish. At this time, you should enter a complete string in the sending area of the serial port debugging assistant and then press the “ENTER” key on the keyboard to end it.

Check on the magic wand

2.Multiple serial port redirection

.c file added

#include
#include
#include
void UsartPrintf(UART_HandleTypeDef USARTx, char *fmt,…)
{

unsigned char UsartPrintfBuf[296];
va_list ap;
unsigned char *pStr = UsartPrintfBuf;

va_start(ap, fmt);
vsnprintf((char *)UsartPrintfBuf, sizeof(UsartPrintfBuf), fmt, ap);
va_end(ap);

while(*pStr != NULL)
{
HAL_UART_Transmit ( & amp;USARTx ,(uint8_t *)pStr + + ,1,HAL_MAX_DELAY );
}

}

.h file added

#define USART_DEBUG huart1
 
void UsartPrintf(UART_HandleTypeDef USARTx, char *fmt,...);

Define serial port 1 and use serial port 234 similarly.

If USART1 and USART2 are opened at the same time, there will be a huart2 in usrat.h. huart2 can be redefined like huart1, and can be customized to other names to distinguish it from other serial ports.

If you use interrupt sending or interrupt receiving, you need to rewrite the interrupt callback function.

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
static uint8_t i=0;
if(huart == & amp;huart1)//determine the interrupt address
{
if(receive == ‘4’) flag = 1;
receive_num[i] = receive;
i + + ;
if(i == 63) i=0;
HAL_UART_Receive_IT( & amp;huart1, & amp;receive,1);
}
}