SCM MSP430G2553 computer serial port baud rate

User Manuals link

These registers:
Here Insert Picture Description

Baud rate generator, relying divider (prescaler / divider) and a modulator (modulator).

Is selected from the set of 8MHZ SMCLK source as the clock, to set the baud rate of 9600, the divider value:
N = B R C L K B a u d R a t e = 8 M H Z 9600 = 833.3333333333 ( d e c ) = 001101000001 ( b i n ) N=\frac{B R C L K}{B a u d R a t e}=\frac{8MHZ}{9600}=833.3333333333(dec)=‭001101000001‬(bin)
UCA0BR1 high eight, UCA0BR0 low eight.
833.3333333333 (dec) removed to give the binary representation of the decimal, and directly to the divider UCA0BR0 UCA0BR1 combined to complete the baud rate configuration.
When N is greater than equal to 16, may be used to set UCOS16 oversampling baud rate generation mode.
Modulator for the fractional part as close as possible.
Manual as well as a variety of complex problems and solutions. It gives a reference configuration table, great value, so count yourself blind and debugging.
Here Insert Picture Description

Reference configuration table:

Table 15-4. Commonly Used Baud Rates, Settings, and Errors, UCOS16 = 0
Here Insert Picture Description
Here Insert Picture Description

Reference Code:

Here Insert Picture Description
Here Insert Picture Description

//                MSP430G2xx3
//             -----------------
//         /|\|              XIN|-
//          | |                 |
//          --|RST          XOUT|-
//            |                 |
//            |     P1.2/UCA0TXD|------------>
//            |                 | 9600 - 8N1
//            |     P1.1/UCA0RXD|<------------

#define CPU_F ( (double) 8000000)
#define delay_us( x )   __delay_cycles( (long) (CPU_F * (double) x / 1000000.0) )
#define delay_ms( x )   __delay_cycles( (long) (CPU_F * (double) x / 1000.0) )
/* 串口波特率计算,当BRCLK=CPU_F时用下面的公式可以计算,否则要根据设置加入分频系数 */
#define baud        9600                                          /* 设置波特率的大小 */
#define baud_setting    (uint) ( (ulong) CPU_F / ( (ulong) baud) )      /* 波特率计算公式 */
#define baud_h      (uchar) (baud_setting >> 8)                     /* 提取高位 */
#define baud_l      (uchar) (baud_setting)                          /* 低位 */

void initUSART(void)
{
    P1SEL = BIT1 + BIT2;                     // P1.1 = RXD, P1.2=TXD
    P1SEL2 = BIT1 + BIT2;                    // P1.1 = RXD, P1.2=TXD
    UCA0CTL1 |= UCSSEL_2;                     // SMCLK
    UCA0BR0 = baud_l;                            // 8MHz 9600
    UCA0BR1 = baud_h;                              // 8MHz 9600
    UCA0MCTL = UCBRS1;                        // Modulation UCBRSx   2=010= UCBRS2  UCBRS1  UCBRS0
    UCA0CTL1 &= ~UCSWRST;                   // **Initialize USCI state machine**
    IE2 |= UCA0RXIE;                          // Enable USCI_A0 RX interrupt
}
Published 105 original articles · won praise 8 · views 10000 +

Guess you like

Origin blog.csdn.net/x1131230123/article/details/104534313