STM32 learning notes (four serial port communication 3 serial port register library function configuration)

Posted by jumpingmatflash on Fri, 28 Jan 2022 18:12:59 +0100

1, Common serial port register

USART_SR status register

Function: the status register is applicable to detect the status of the serial port at this time.
Mainly focus on two bits: RXNE and TC (bits 5 and 6).
RXNE (non empty read data register): when this bit is set to 1, it indicates that data has been received and can be read out (that is, the data in RDR shift register is transferred to USART_DR register). What we need to do at this time is to read USART as soon as possible_ Dr to clear the bit, or write 0 to the bit and clear it directly.

TC (transmission completion): when this bit is set to 1, it indicates USART_ The data in Dr has been sent. If the interrupt of this bit is set, an interrupt will be generated. There are also two ways to clear this bit: reading USART_SR, write USART_DR; Write 0 directly to this bit.

USART_DR data register

USART_DR actually contains two registers, one for sending TDR and one for receiving RDR. When sending data, send it to USART_DR write data will be automatically stored in TDR; When reading data, send it to USART_DR reading data will automatically extract RDR data.

During serial communication, bit by bit is transmitted, so TDR and RDR registers are between system bus and shift register; When sending data, the contents of TDR are transferred to the transmission shift register. When receiving data, the sequence of each received bit is saved in the reception shift register and then transferred to RDR.

USART_BRR baud rate register

The baud rate register includes two parts: DIV_Mantissa (integer part) and DIV_Fraction.

USART_CR1 control register

The control register is mainly used to set USART enable, check control enable, check selection (odd parity check), PE interrupt enable, transmit buffer air interrupt enable, transmit completion interrupt enable, receive buffer non empty enable, transmit enable, receive enable, word length, etc

Calculation of baud rate


Decimal decimals need to be converted directly to hexadecimal × 16, opposite to the integer part ÷ 16.

2, Serial port operation related library functions

void USART_Init()

Reference link

USART_InitStrue.USART_BaudRate=115200;// set baud rate
USART_InitStrue.USART_WordLength=USART_WordLength_8b;// The data word length is 8
USART_InitStrue.USART_HardwareFlowControl=USART_HardwareFlowControl_None;// No hardware flow control should be used when connecting other peripherals
USART_InitStrue.USART_Mode=USART_Mode_Tx|USART_Mode_Rx;
//Adjust to receive or send mode
USART_InitStrue.USART_Parity=USART_Parity_No;// Parity set to none
USART_InitStrue.USART_StopBits=USART_StopBits_1;// Set stop bit

3, General steps of serial port initialization

4, Initialization code

#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "usart.h"
#include "usart.h"
void My_USART1_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStrue;
	USART_InitTypeDef USART_InitStrue;
	NVIC_InitTypeDef NVIC_InitStrue;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//GPIO????
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//??????
	
	GPIO_InitStrue.GPIO_Mode=GPIO_Mode_AF_PP;
	GPIO_InitStrue.GPIO_Pin=GPIO_Pin_9;
	GPIO_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;
	GPIO_Init(GPIOA,&GPIO_InitStrue);
	
	GPIO_InitStrue.GPIO_Mode=GPIO_Mode_IN_FLOATING;
	GPIO_InitStrue.GPIO_Pin=GPIO_Pin_10;
	GPIO_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;
	GPIO_Init(GPIOA,&GPIO_InitStrue);
	
	USART_InitStrue.USART_BaudRate=115200;
	USART_InitStrue.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
	USART_InitStrue.USART_Mode=USART_Mode_Tx|USART_Mode_Rx;
	USART_InitStrue.USART_Parity=USART_Parity_No;
	USART_InitStrue.USART_StopBits=USART_StopBits_1;
	USART_InitStrue.USART_WordLength=USART_WordLength_8b;
	
	USART_Init(USART1,&USART_InitStrue);//
	
	USART_Cmd(USART1,ENABLE);//????1
	
	USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//??????,?????,?????
	
	NVIC_InitStrue.NVIC_IRQChannel=USART1_IRQn;  //(??)
	NVIC_InitStrue.NVIC_IRQChannelCmd=ENABLE;   //(????)
	NVIC_InitStrue.NVIC_IRQChannelPreemptionPriority=1;
	NVIC_InitStrue.NVIC_IRQChannelSubPriority=1;//(???)
	NVIC_Init(&NVIC_InitStrue);
	
}
 
void USART1_IRQhandler(void)
{
	u8 res;    //(??????)
	 if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)  //(?????????)
 {
     res= USART_ReceiveData(USART1);   //????
     USART_SendData(USART1,res);      //????
  }
}
 
 int main(void)
 {	
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	My_USART1_Init();
	 while(1);
	 
 }

Topics: Single-Chip Microcomputer stm32 ARM