如何设置波特率使误差最小

[复制链接]
2784|39
手机看帖
扫描二维码
随时随地手机跟帖
heweibig| | 2018-7-6 19:54 | 显示全部楼层
在网上找些资料

使用特权

评论回复
zhaoxqi| | 2018-7-6 19:58 | 显示全部楼层
找了。但是没看懂,请大神给解解惑。
EXAMPLE: a baud rate of 4800 baud is required with a crystal frequency of
32,768 Hz. This is necessary because the UART also has to run during low power
mode 3. With only the ACLK available, the theoretical division factor—the
truncated value is the content of baud-rate register UBR (UBR1/UBR0)—is:
UBR
32768
4800
6.82667
This means that the baud-rate register UBR1 (MSBs) is loaded with zero, and the
UBR0 register contains a 6. To get a rough estimate of the 8-bit modulation
register UMCTL, the fractional part 0.826667 is multiplied by 8 (the number of bits
in register UMCTL):
UMCTL 0.82667 8 6.613
The rounded result 7 is the number of  ones to be placed into the modulation
register UMCTL. The corrected baud rate with the UMCTL register containing 7
ones is:
baud rate 
32768

7 7 1 6
8

4766.2545
This results in an average baud rate error of:
baud rate error 
4766.2545 4800
4800
100 0.703%
To get the best-fitting bit sequence for modulation register UMCTL, the following
algorithm can be used: the fractional part of the theoretical division factor is
summed up eight times;  the actual m-bit is set if a carry to the integer part occurs,
and is cleared otherwise. An example using the fraction 0.82667 previously
calculated follows:
Fraction Addition Carry to next integer UMCTL Bits
0.82667 + 0.82667 = 1.65333 Yes m0 1
1.65333 + 0.82667 = 2.48000 Yes m1 1
2.48000 + 0.82667 = 3.30667 Yes m2 1
3.30667 + 0.82667 = 4.13333 Yes m3 1
4.13333 + 0.82667 = 4.96000 No m4 0
4.96000 + 0.82667 = 5.78667 Yes m5 1
5.78667 + 0.82667 = 6.61333 Yes m6 1
6.61333 + 0.82667 = 7.44000 Yes m7 1
The result of the calculated bits m7...m0 is EFh (11101111b). Section 3.3.2
contains a software macro (CALC_UMCTL) that uses this algorithm to calculate
the optimum value for the modulation register UMCTL for every combination of
USART clock and desired baud rate. For the above example, the algorithm also
finds EFh with its seven ones.
A second software macro (CALC_UBR) calculates the values of the two UBR
registers.

还有相应的函数:
void SetBaudRateRegisters(long clk,long baud)
{
    int n = clk / baud;     //整数波特率
    char mSum = 0;            //Σmi
    int txEr0;              //对应位为0时错误率
    int txEr1;              //对应位为1时错误率
    char i = 0;             //循环计数

    UxBR1 = n >> 8;         //高8位
    UxBR0 = n & 0xff;       //低8位
    UxMCTL = 0;
   
    //循环 比较错误率大小 设置UxMCTL
    for(;i < 8;i++)
    {
        txEr0 = 100 * baud * ((i + 1) * n + mSum) / clk - 100 * (i + 1);
        txEr1 = 100 * baud * ((i + 1) * n + mSum + 1) / clk - 100 * (i + 1);
        if(abs(txEr1) < abs(txEr0))
        {
            mSum++;
            UxMCTL |= (1<<i);
        }
    }
}

使用特权

评论回复
shimx| | 2018-7-6 20:01 | 显示全部楼层

看这个文档,里面有表格,按表格设置就行。也可以自己计算
http://wenku.baidu.com/view/d73645d233d4b14e852468bb.html

使用特权

评论回复
dengdc|  楼主 | 2018-7-6 20:04 | 显示全部楼层
我看的就是这份,就是可能不明白它是怎么算出来的?求助求助啊

使用特权

评论回复
dengdc|  楼主 | 2018-7-6 20:06 | 显示全部楼层
我看的就是这份,就是可能不明白它是怎么算出来的?求助求助啊

使用特权

评论回复
午夜粪车| | 2018-7-6 20:09 | 显示全部楼层
里面不是有公式吗?耐心看看吧

使用特权

评论回复
yszong| | 2018-7-6 20:12 | 显示全部楼层

波特率与她的定时器设置和振荡频率有关,凡是能够影响振荡稳定性的都影响。

使用特权

评论回复
shimx| | 2018-7-6 20:14 | 显示全部楼层
这个最好设置的波特率是所用的频率的公约数的

使用特权

评论回复
jiaxw| | 2018-7-6 20:18 | 显示全部楼层
若不是公约数的,那么就是余数越小越好。。。这样说大家能明白不?

使用特权

评论回复
zhaoxqi| | 2018-7-6 20:20 | 显示全部楼层
这个算法的比较复杂的啦,不过应该很有效果的。

使用特权

评论回复
yszong| | 2018-7-6 20:23 | 显示全部楼层

波特率误差在2.5%的误差范围下,可以保证可靠传输

使用特权

评论回复
jiaxw| | 2018-7-6 20:25 | 显示全部楼层

主要还是看晶振

使用特权

评论回复
jiaxw| | 2018-7-6 20:27 | 显示全部楼层
你用的是什么芯片?

使用特权

评论回复
myiclife| | 2018-7-7 11:55 | 显示全部楼层
,最大偏差一般在5% 以内都可以正常通信

使用特权

评论回复
uytyu| | 2018-7-7 11:56 | 显示全部楼层
在921600速率下传输几百KB的数据,非常稳定可靠。

使用特权

评论回复
iyoum| | 2018-7-7 11:56 | 显示全部楼层
有相对误差

使用特权

评论回复
wwppd| | 2018-7-7 11:57 | 显示全部楼层
考虑到通信的时候的波特率

使用特权

评论回复
jkl21| | 2018-7-7 11:57 | 显示全部楼层
合理计算波特率误差率

使用特权

评论回复
maqianqu| | 2018-7-7 11:57 | 显示全部楼层
选择合适的晶振就可以的。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

892

主题

13885

帖子

7

粉丝