2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
The serial port of STM32 is quite rich and powerful. It can provide up to 5 serial ports, with fractional baud rate generator, support single-line optical communication and half-duplex single-line communication, support LIN, smart card protocol and IrDA SIRENDEC specification (only supported by serial port 3), DMA, etc.
The most basic setting of the serial port is the baud rate setting. The STM32 serial port is quite simple to use. As long as you turn on the serial port clock, set the mode of the corresponding IO port, and then configure the baud rate, data bit length, parity bit and other information, you can use it. Below, we will briefly introduce these registers directly related to the basic configuration of the serial port.
1. Enable the serial port clock.As a peripheral of STM32, the clock of the serial port is controlled by the peripheral always-enable register. Here, the serial port 1 we use is in the 14th bit of the APB2ENR register. Except for the clock enable of serial port 1 in the APB2ENR register, the clock enable bits of other serial ports are all in APB1ENR.
2. Serial port resetWhen an abnormality occurs in a peripheral, the corresponding bit in the reset register can be set to reset the peripheral, and then reconfigure the peripheral to make it work again.
Execute the operation of resetting the peripheral. The reset of serial port 1 is achieved by configuring the 14th bit of the APB2RSTR register.
3. Set the serial port baud rate.Each serial port has its own independent baud rate register USART BRR, which can be used to configure different baud rates. The description of each bit of this register is as follows:
4. Serial port control. Each serial port of STM32 has three control registers USART CR1~3. Many configurations of the serial port are set through these three registers.
5. Data transmission and reception. The transmission and reception of STM32 is realized through the data register USART_DR, which is a double register containing TDR and RDR. When data is written to this register, the serial port will automatically send it.
When, it is also stored in this register.
It can be seen that although it is a 32-bit register, only the lower 9 bits (DR[8:0]) are used, and the rest are reserved. DR[8:0] is the serial port data, which contains the data to be sent or received. Since it is composed of two registers, one for sending (TDR) and one for receiving (RDR), this register has both read and write functions. The TDR register provides a parallel interface between the internal bus and the output shift register. The RDR register provides a parallel interface between the input shift register and the internal bus.
When the parity bit is enabled (PCE bit in USART CR1 is set) for transmission, the value written to the MSB (MSB is the 7th or 8th bit depending on the length of the data) will be replaced by the subsequent parity bit. When the parity bit is enabled for reception, the MSB bit read is the received parity bit.
6. The status of the serial port can be read through the status register USART SR.
Here we focus on two bits, the 5th and 6th bits RXNE and TC.
RXNE (read data register not empty), when this bit is set to 1, it indicates that data has been received and can be read. At this time, what we need to do is to read USART DR as soon as possible. By reading USART DR, this bit can be cleared to 0, or by writing 0 to this bit to clear it directly.
TC (transmission complete), when this bit is set, it means that the data in USARTDR 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: 1: read USART SR and write USARTDR. 2: write 0 directly to this bit.
- void uart_init(u32 pclk2,u32 bound)
- {
- float temp;
- u16 mantissar
- u16 fraction;
- temp=(float)(pclk2*1000000)/(bound*16);//得到USARTDIV
- mantissa=temp;//得到整数部分
- fraction=(temp-mantissa)*16://得到小数部分
- mantissa<<=4;
- mantissa+=fraction;
- RCC->APB2ENRI=1<<2;//使能PORTA口时钟
- RCC->APB2ENRI=1<<14;//使能串口时钟
- GPIOA->CRH&=0XFFFFF00F;
- GPIOA->CRH|=0X000008B0;//IO状态设置
- RCC->APB2RSTRI=1<<14;//复位串口1
- RCC->APB2RSTR&=~(1<<14)://停止复位
- //波特率设置
- USART1->BRR=mantissa;//波特率设置
- USART1->CR1|=0X200C;//1位停止,无校验位,
-
- USART1->CR1|=1<<8;//PE中断使能
- USART1->CR11=1<<5;//接收缓冲区非空中断使能
- MY_NVIC_Init(3,3,USART1 IRQChannel,2);//组2,最低优先级
- }
Initialize the serial port hardware device and enable interrupts:
Configuration steps:
(1) Turn on the clocks of GPI0 and USART1
(2) Set the GPI0 mode of the two pins of USART1
(3) Configure USART1 data format, baud rate and other parameters
(4) Enable USART1 receive interrupt function
(5) Finally, enable the USART1 function