ARM技术william hill官网
直播中

ljw

8年用户 238经验值
擅长:自动化
私信 关注
[资料]

STM32 USART 波特率计算

本帖最后由 蓝剑威 于 2017-4-21 13:53 编辑


<转>www.cnblogs.com/shangdawei/p/4641805.html<转>www.cnblogs.com/shangdawei/p/4641805.html
<转>www.cnblogs.com/shangdawei/p/4641805.html


STM32 USART 波特率计算

The baud rate for the receiver and transmitter (Rx and Tx) are both set to the same value
as programmed in the Mantissa and Fraction values of USARTDIV.
从上图可以看出,该寄存器高 16 位无效,最低 4 位为小数部分,其余部分为整数部分。
这样的设计可以使波特率更加精确。关于波特率的产生,有这么一段话来解释:
分数波特率的产生:  接收器和发送器(RX和TX)都是设置成为 USARTDIV 整数和小数寄存器中配置的值。
This give the following equation for baud rate:
Tx/Rx baud = CK_APB1 / (8 x (2 - OVER8) x USARTDIV)
Where USARTDIV is an unsigned fixed point number that is coded on the USART_BRR register.
  • When OVER8=0, the fractional part is coded on 4 bits and programmed by the DIV_fraction[3:0] bits in the USART_BRR register
  • Tx/Rx baud = CK_APB1 / (16 x USARTDIV)
  • When OVER8=1, the fractional part is coded on 3 bits and programmed by the DIV_fraction[2:0] bits in the USART_BRR register,
    and bit DIV_fraction[3] must be kept cleared.
  • Tx/Rx baud = CK_APB1 / (8  x USARTDIV)
The baud counters are updated to the new value in the baud registers after a write operation to USART_BRR.
Hence the baud rate register value should not be changed during communication.

The USARTs are on the APB1 bus, Figure 12 in the reference manual is the clock tree,
which shows how the APB1 clock is derived from the PLL clock.
The maximum APB1 clock is 32MHz.
OVER8=1 is required for higher speeds, giving: baud = 32000000 / (USARTDIV x 8).
So USARTDIV = 32000000 / (baud x 8)
For 4Mbps therefore, USARTDIV=1 (see table 138 S.No.12 for details).
For 2Mps, USARTDIV=2.
To achieve 3Mbps you will have to reduce the APB1 clock to 24MHz and set USARTDIV=1 (see table 131).
But note that the clock rate changes for all other APB1 peripherals too.
The simplest way to correctly program the USART baud rate is via the STM32L1xx standard peripheral library.
Also to determine the correct peripheral clock settings (and more), and generate initialisation code,
you can use STM's MicroXplorer tool.
Fpclk = 12 MHz, Baud = 460800 Bps : OVER8 = 0
DIV = 12000000 / ( 16 * 460800 ) = 1.6276
DIV_Mantissa = 1
DIV_Fraction = 0.6276 * 16 = 10
USARTDIV = ( 1 << 4 ) | 10 = 0x001 A = 1.625
12000000 / ( 16 * 1.625 ) = 12000000 / 26 = 461538.5 Bps

Fpclk = 12 MHz, Baud = 460800 Bps : OVER8 = 1
DIV = 12000000 / ( 8 * 460800 ) = 3.255
DIV_Mantissa = 3
DIV_Fraction = 0.255 * 8 = 2.04
USARTDIV = ( 3 << 4 ) | 2 = 0x003 2 = 3.25
12000000 / ( 8 * 3.25 ) = 12000000 / 26 = 461538.5 Bps

STM32波特率设置,在115200时候,实际是115384,会有0.15%的误差, 不过还是可以接受的。
其实stm32的uart还是蛮简单的,初始化4个寄存器完事,就是波特率方面需要算算。 原文:
stm32 中文资料中有如下计算公式 :
Tx / Rx 波特率 = fPCLKx/(16*USARTDIV);
这里的fPCLKx(x=1、2)是给外设的时钟(PCLK1用于USART2、3、4、5,PCLK2用于USART1)
USARTDIV是一个无符号的定点数。这12位的值设置在USART_BRR寄存器。
如果使用USART1,系统时钟为72MHZ,USART1使用PCLK2时钟,也定义为72MHz.
定义波特率=9600,fPCLK2=72MHz,则: 计算USARTDIV=72MHz/9600/16=468.75
取整数468=0x1D4. 小数0.75*16=12=0x0C. 所以写入寄存器USART_BRR中的值为:USART_BRR=0x1D4C.
如果使用USART2,USART2使用PCLK1时钟,PCLK1时钟为36MHz.
定义波特率=9600,fPCLK1=36MHz,则: 计算USARTDIV=36MHz/9600/16=234.375
取整数234=0xEA.小数0.375*16=6=0x06.所以写入寄存器USART_BRR中的值为:USART_BRR=0xEA6.




回帖(1)

蔼嗨磊

2017-7-13 11:09:09
举报

更多回帖

发帖
×
20
完善资料,
赚取积分