硬汉嵌入式论坛

 找回密码
 立即注册
查看: 415|回复: 1
收起左侧

[Lua] 使用LUA小程序计算串口误差率和实际波特率

[复制链接]

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106757
QQ
发表于 2023-11-11 00:47:23 | 显示全部楼层 |阅读模式
比如STM32H7的串口外设主频配置为100MHz。

LUA代码:

[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 = 100000000  -- 用户设置串口外设主频
OVER8 = 0         -- 0表示16过采样,1表示8倍过采样
 
print("所需波特率    实际值波特率    误差     BRR寄存器数值")
 
for i=1, #BspArray, 1 do
 
    Bsp = BspArray[i]
 
    if(OVER8 == 0) then
 
        UARRTDIV = Fclk/Bsp
 
        mantissa = (UARRTDIV + 0.5)//1
         
        Bsp1 = Fclk/mantissa
 
        str = string.format("%-13d %-13.4f %7.4f%%   %-.4f = 0x%08x",
                                Bsp, Bsp1, (Bsp1-Bsp)/Bsp*100, mantissa, mantissa)
        print(str)
    else
 
        UARRTDIV = 2*Fclk/Bsp
 
        mantissa = (UARRTDIV + 0.5)//1
 
        mantissa = mantissa & 0xfff0
        fraction = mantissa & 0x0f
        fraction = fraction >> 1
 
        Bsp1 = 2*Fclk/(mantissa+fraction)
 
        str = string.format("%-13d %-13.4f %7.4f%%   %-.4f=0x%08x",
                                Bsp, Bsp1, (Bsp1-Bsp)/Bsp*100, mantissa+fraction, mantissa+fraction)
        print(str)
    end
end



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

[C] 纯文本查看 复制代码
所需波特率    实际值波特率    误差     BRR寄存器数值
110           110.0000      -0.0000%   909091.0000 = 0x000ddf23
300           300.0003       0.0001%   333333.0000 = 0x00051615
600           599.9988      -0.0002%   166667.0000 = 0x00028b0b
1200          1200.0048      0.0004%   83333.0000 = 0x00014585
2400          2399.9808     -0.0008%   41667.0000 = 0x0000a2c3
4800          4800.0768      0.0016%   20833.0000 = 0x00005161
9600          9599.6928     -0.0032%   10417.0000 = 0x000028b1
19200         19201.2289     0.0064%   5208.0000 = 0x00001458
38400         38402.4578     0.0064%   2604.0000 = 0x00000a2c
57600         57603.6866     0.0064%   1736.0000 = 0x000006c8
115200        115207.3733    0.0064%   868.0000 = 0x00000364
128000        128040.9731    0.0320%   781.0000 = 0x0000030d
230400        230414.7465    0.0064%   434.0000 = 0x000001b2
256000        255754.4757   -0.0959%   391.0000 = 0x00000187
460800        460829.4931    0.0064%   217.0000 = 0x000000d9
500000        500000.0000    0.0000%   200.0000 = 0x000000c8
512000        512820.5128    0.1603%   195.0000 = 0x000000c3
600000        598802.3952   -0.1996%   167.0000 = 0x000000a7
750000        751879.6992    0.2506%   133.0000 = 0x00000085
921600        917431.1927   -0.4523%   109.0000 = 0x0000006d
1000000       1000000.0000   0.0000%   100.0000 = 0x00000064
1152000       1149425.2874  -0.2235%   87.0000 = 0x00000057
1500000       1492537.3134  -0.4975%   67.0000 = 0x00000043
2000000       2000000.0000   0.0000%   50.0000 = 0x00000032
2500000       2500000.0000   0.0000%   40.0000 = 0x00000028
3000000       3030303.0303   1.0101%   33.0000 = 0x00000021




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

[C] 纯文本查看 复制代码
所需波特率    实际值波特率    误差     BRR寄存器数值
110           4106.4389     3633.1263%   48704.0000=0x0000be40
300           17705.3824    5801.7941%   11296.0000=0x00002c20
600           35410.7649    5801.7941%   5648.0000=0x00001610
1200          5620.5036     368.3753%   35584.0000=0x00008b00
2400          11241.0072    368.3753%   17792.0000=0x00004580
4800          4800.3072      0.0064%   41664.0000=0x0000a2c0
9600          9600.6144      0.0064%   20832.0000=0x00005160
19200         19201.2289     0.0064%   10416.0000=0x000028b0
38400         38461.5385     0.1603%   5200.0000=0x00001450
57600         57603.6866     0.0064%   3472.0000=0x00000d90
115200        115740.7407    0.4694%   1728.0000=0x000006c0
128000        128865.9794    0.6765%   1552.0000=0x00000610
230400        231481.4815    0.4694%   864.0000=0x00000360
256000        260416.6667    1.7253%   768.0000=0x00000300
460800        462962.9630    0.4694%   432.0000=0x000001b0
500000        500000.0000    0.0000%   400.0000=0x00000190
512000        520833.3333    1.7253%   384.0000=0x00000180
600000        625000.0000    4.1667%   320.0000=0x00000140
750000        781250.0000    4.1667%   256.0000=0x00000100
921600        961538.4615    4.3336%   208.0000=0x000000d0
1000000       1041666.6667   4.1667%   192.0000=0x000000c0
1152000       1250000.0000   8.5069%   160.0000=0x000000a0
1500000       1562500.0000   4.1667%   128.0000=0x00000080
2000000       2083333.3333   4.1667%   96.0000=0x00000060
2500000       2500000.0000   0.0000%   80.0000=0x00000050
3000000       3125000.0000   4.1667%   64.0000=0x00000040









回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106757
QQ
 楼主| 发表于 2023-11-11 00:50:35 | 显示全部楼层
同样的方法,STM32F4的串口主频84MHz时测试。

LUA代码和STM32H7的代码不同:

[C] 纯文本查看 复制代码
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[i]
 
    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过采样的误差率和实际速度如下 :

[C] 纯文本查看 复制代码
所需波特率    实际值波特率    误差     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过采样的误差率和实际速度如下 :

[C] 纯文本查看 复制代码
所需波特率    实际值波特率    误差     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
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-5-4 18:24 , Processed in 0.246689 second(s), 25 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表