【STM32】Initial use of serial port

This article is only used as study notes to provide a brief introduction to the serial port. The correct use method requires actual debugging.

Type of communication:

Synchronous, asynchronous,
simplex, duplex, serial
and parallel

Serial communication of STM32:

When configuring on-chip peripherals 控制寄存器, both communicating parties perform the same configuration and agree on common start bits, stop bits, and check bits, so that the UART peripherals can automatically process sending and receiving data.

Serial communication data packet

Insert image description here
These are configurable via control registers

data register

Only the lower 9 bits of the USART data register (DR) are valid, and 8-bit data is generally used unless 9 bits are set in the control register CR.

USART_DR contains sent data or received data. USART_DR actually contains two registers, a writable TDR specially used for sending, and a readable RDR specially used for receiving. When a sending operation is performed, data written to USART_DR will be automatically stored in TDR; when a reading operation is performed, data read from USART_DR will automatically extract RDR data.

USART supports DMA transfer. Generally, DMA needs to be used to remove the data in USART->DR.

General use of USART

Nested vectored interrupt controller NVIC configuration;

NVIC configuration

static void NVIC_Configuration(void)
{
    
    
	NVIC_InitTypeDef NVIC_InitStructure;

	/* 嵌套向量中断控制器组选择 */
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

	/* 配置 USART 为中断源 */
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	/* 抢断优先级为 1 */
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	/* 子优先级为 1 */
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	/* 打开中断 */
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	/* 初始化配置 NVIC */
	NVIC_Init(&NVIC_InitStructure);
}

Serial port configuration

#UART配置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;

// 打开串口 GPIO 的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
// 打开串口外设的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

// 将 USART Tx 的 GPIO 配置为推挽复用模
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);

// 将 USART Rx 的 GPIO 配置为浮空输入模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);

// 配置串口的工作参数
// 配置波特率
USART_InitStructure.USART_BaudRate = DEBUG_USART_BAUDRATE;

// 配置 帧数据字长
USART_InitStructure.USART_WordLength = USART_WordLength_8b;

// 配置停止位
USART_InitStructure.USART_StopBits = USART_StopBits_1;

// 配置校验位
USART_InitStructure.USART_Parity = USART_Parity_No ;

// 配置硬件流控制
USART_InitStructure.USART_HardwareFlowControl =USART_HardwareFlowControl_None;

// 配置工作模式,收发一起
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

// 完成串口的初始化配置
USART_Init(DEBUG_USARTx, &USART_InitStructure);

// 串口中断优先级配置 打开中断
NVIC_Configuration();

// 使能串口接收中断
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

// 使能串口
USART_Cmd(USART1, ENABLE);

Serial sending

1. Library function method

USART_SendData(USART1, (uint8_t) len);

Guess you like

Origin blog.csdn.net/apythonlearner/article/details/131462292