打印

MSP430usart波特率设置问题

[复制链接]
1371|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
活力的小冰|  楼主 | 2013-8-26 16:16 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
如何设置波特率使误差最小,在网上找了一些资料。但是没看懂,请大神给解解惑。
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);
        }
    }
}

相关帖子

沙发
dirtwillfly| | 2013-8-26 19:30 | 只看该作者
看这个文档,里面有表格,按表格设置就行。也可以自己计算
http://wenku.baidu.com/view/d73645d233d4b14e852468bb.html

使用特权

评论回复
板凳
活力的小冰|  楼主 | 2013-8-26 20:45 | 只看该作者
dirtwillfly 发表于 2013-8-26 19:30
看这个文档,里面有表格,按表格设置就行。也可以自己计算
http://wenku.baidu.com/view/d73645d233 ...

我看的就是这份,就是可能不明白它是怎么算出来的?求助求助啊

使用特权

评论回复
地板
dirtwillfly| | 2013-8-26 20:59 | 只看该作者
活力的小冰 发表于 2013-8-26 20:45
我看的就是这份,就是可能不明白它是怎么算出来的?求助求助啊

里面不是有公式吗?耐心看看吧

使用特权

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

本版积分规则

1

主题

4

帖子

0

粉丝