eric2013 发表于 2023-11-11 00:43:04

STM32F4串口在8倍过采样和16倍过采样下,各种常用波特率误差和实际速度

串口外设主频配置为84MHz。

LUA代码:


BspArray =
{
        110,
        300,
        600,
        1200,
        2400,
        4800,
        9600,
        19200,
        38400,
        57600,
        115200,
        128000,
        230400,
        256000,
        460800,
        500000,
        512000,
        600000,
        750000,
        921600,
        1000000,
        1152000,
        1500000,
        2000000,
        2500000,
        3000000
}

Fclk = 84000000-- 用户设置串口外设主频
OVER8 = 0       -- 0表示16过采样,1表示8倍过采样

print("所需波特率    实际值波特率    误差   BRR寄存器数值")

for i=1, #BspArray, 1 do

        Bsp = BspArray

        UARRTDIV = Fclk/(8*(2-OVER8))/Bsp
        mantissa = UARRTDIV//1
        fraction = UARRTDIV - mantissa

        if(OVER8 == 0) then
                fraction_adj = (fraction*16 + 0.5)//1

                if(fraction_adj > 16) then
                fraction_adj = 0
                mantissa = mantissa + 1
                end

                Bsp1 = Fclk/(8*(2-OVER8))/(mantissa+ fraction_adj/16)

                str = string.format("%-13d %-13.4f %7.4f%%   %-.4f = 0x%08x",
                                                                Bsp, Bsp1, (Bsp1-Bsp)/Bsp*100, mantissa+fraction_adj/16, mantissa*16+fraction_adj)
                print(str)
        else
                fraction_adj = (fraction*8 + 0.5)//1

                if(fraction_adj > 8) then
                fraction_adj = 0
                mantissa = mantissa + 1
                end

                Bsp1 = Fclk/(8*(2-OVER8))/(mantissa+ fraction_adj/8)

                str = string.format("%-13d %-13.4f %7.4f%%   %-.4f=0x%08x",
                                                                Bsp, Bsp1, (Bsp1-Bsp)/Bsp*100, mantissa+fraction_adj/8, mantissa*16+fraction_adj)
                print(str)

        end
end


16过采样的误差率和实际速度如下 :

所需波特率    实际值波特率    误差   BRR寄存器数值
110         110.0001       0.0000%   47727.2500 = 0x000ba6f4
300         300.0000       0.0000%   17500.0000 = 0x000445c0
600         600.0000       0.0000%   8750.0000 = 0x000222e0
1200          1200.0000      0.0000%   4375.0000 = 0x00011170
2400          2400.0000      0.0000%   2187.5000 = 0x000088b8
4800          4800.0000      0.0000%   1093.7500 = 0x0000445c
9600          9600.0000      0.0000%   546.8750 = 0x0000222e
19200         19200.0000   0.0000%   273.4375 = 0x00001117
38400         38391.2249    -0.0229%   136.7500 = 0x0000088c
57600         57613.1687   0.0229%   91.1250 = 0x000005b2
115200      115226.3374    0.0229%   45.5625 = 0x000002d9
128000      128048.7805    0.0381%   41.0000 = 0x00000290
230400      230136.9863   -0.1142%   22.8125 = 0x0000016d
256000      256097.5610    0.0381%   20.5000 = 0x00000148
460800      461538.4615    0.1603%   11.3750 = 0x000000b6
500000      500000.0000    0.0000%   10.5000 = 0x000000a8
512000      512195.1220    0.0381%   10.2500 = 0x000000a4
600000      600000.0000    0.0000%   8.7500 = 0x0000008c
750000      750000.0000    0.0000%   7.0000 = 0x00000070
921600      923076.9231    0.1603%   5.6875 = 0x0000005b
1000000       1000000.0000   0.0000%   5.2500 = 0x00000054
1152000       1150684.9315-0.1142%   4.5625 = 0x00000049
1500000       1500000.0000   0.0000%   3.5000 = 0x00000038
2000000       2000000.0000   0.0000%   2.6250 = 0x0000002a
2500000       2470588.2353-1.1765%   2.1250 = 0x00000022
3000000       3000000.0000   0.0000%   1.7500 = 0x0000001c




8过采样的误差率和实际速度如下 :

所需波特率    实际值波特率    误差   BRR寄存器数值
110         110.0001       0.0000%   95454.5000=0x00174de4
300         300.0000       0.0000%   35000.0000=0x00088b80
600         600.0000       0.0000%   17500.0000=0x000445c0
1200          1200.0000      0.0000%   8750.0000=0x000222e0
2400          2400.0000      0.0000%   4375.0000=0x00011170
4800          4800.0000      0.0000%   2187.5000=0x000088b4
9600          9600.0000      0.0000%   1093.7500=0x00004456
19200         19200.0000   0.0000%   546.8750=0x00002227
38400         38391.2249    -0.0229%   273.5000=0x00001114
57600         57613.1687   0.0229%   182.2500=0x00000b62
115200      115226.3374    0.0229%   91.1250=0x000005b1
128000      128048.7805    0.0381%   82.0000=0x00000520
230400      230136.9863   -0.1142%   45.6250=0x000002d5
256000      256097.5610    0.0381%   41.0000=0x00000290
460800      461538.4615    0.1603%   22.7500=0x00000166
500000      500000.0000    0.0000%   21.0000=0x00000150
512000      512195.1220    0.0381%   20.5000=0x00000144
600000      600000.0000    0.0000%   17.5000=0x00000114
750000      750000.0000    0.0000%   14.0000=0x000000e0
921600      923076.9231    0.1603%   11.3750=0x000000b3
1000000       1000000.0000   0.0000%   10.5000=0x000000a4
1152000       1150684.9315-0.1142%   9.1250=0x00000091
1500000       1500000.0000   0.0000%   7.0000=0x00000070
2000000       2000000.0000   0.0000%   5.2500=0x00000052
2500000       2470588.2353-1.1765%   4.2500=0x00000042
3000000       3000000.0000   0.0000%   3.5000=0x00000034


页: [1]
查看完整版本: STM32F4串口在8倍过采样和16倍过采样下,各种常用波特率误差和实际速度