MSP430usart波特率设置问题

2019-08-06 17:00发布

如何设置波特率使误差最小,在网上找了一些资料。但是没看懂,请大神给解解惑。
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);
        }
    }
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。