STM32 notes - USART serial communication

1. Introduction to USART

  • USART (Universal Synchronous/Asynchronous Receiver/Transmitter) universal synchronous/asynchronous receiver and transmitter
  • USART is an integrated hardware peripheral inside STM32. It can automatically generate data frame timing based on one byte of data in the data register and send it out from the TX pin. It can also automatically receive the data frame timing of the RX pin and splice it into one byte of data. , stored in the data register
  • Comes with built-in baud rate generator, up to 4.5Mbits/s
  • Configurable data bit length (8/9), stop bit length (0.5/1/1.5/2)
  • Optional parity bit (no parity/odd parity/even parity)
  • Supports synchronous mode, hardware flow control, DMA, smart card, IrDA, LIN
  • STM32F103C8T6 USART source: USART1, USART2, USART3

2. USART block diagram

  • Sending data: STM32 writes the data to be sent into the transmit data register (TDR), waits for the hardware circuit to transfer the data in the transmit data register to the transmit shift register, and then sends the data from TX.
  • Receive data: The received data enters the receive shift register from RX. There is a hardware circuit to transfer the data to the receive data register. The RXNE (read data register is not empty) bit will be set by hardware. At this time, the interrupt can be triggered through RXNE and the data can be read out.

3. USART data frame

3.1 Word length setting

Usual data frame mode:

  •  9-bit word length (11-bit data): one start bit, one stop bit, eight data bits, and one check bit.
  •  8-bit word length (10-bit data): one start bit, one stop bit, eight data bits, no parity bit.

3.2 Configure stop bit

  •  You can choose 1 stop bit, 1.5 stop bits, 2 stop bits or 0.5 stop bits.

4. Basic structure of USART

  1. Turn on peripheral clock
  2. Initialize GPIO port
  3. Initialize USART
  4. USART interrupt control output (select trigger signal)
  5. NVIC interrupt priority grouping
  6. Initialize NVIC
  7. Enable USART

The procedure is as follows:

void Serial_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//开启USART外设时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStruct;
	GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF_PP;//PA9复用推挽输出
	GPIO_InitStruct.GPIO_Pin=GPIO_Pin_9;
	GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStruct);
	GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IPU;//PA10上拉输入
	GPIO_InitStruct.GPIO_Pin=GPIO_Pin_10;
	GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStruct);
	
	USART_InitTypeDef USART_InitStruct;
	USART_InitStruct.USART_BaudRate=9600;//波特率
	USART_InitStruct.USART_HardwareFlowControl=USART_HardwareFlowControl_None;//硬件流控制
	USART_InitStruct.USART_Mode=USART_Mode_Tx|USART_Mode_Rx;//串口模式
	USART_InitStruct.USART_Parity=USART_Parity_No;//奇偶校验
	USART_InitStruct.USART_StopBits=USART_StopBits_1;//停止位一位
	USART_InitStruct.USART_WordLength=USART_WordLength_8b;//数据位数
	USART_Init(USART1,&USART_InitStruct);
	
	USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//中断输出控制
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//中断优先级分组
	NVIC_InitTypeDef NVIC_InitStruct;
	NVIC_InitStruct.NVIC_IRQChannel=USART1_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=1;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority=1;
	NVIC_Init(&NVIC_InitStruct);
	
	USART_Cmd(USART1,ENABLE);
}

Guess you like

Origin blog.csdn.net/ssssshhbh/article/details/129571821