MSP430F5438 Study Notes UART ACLK 9600-8-N-1

Publisher:云自南国来Latest update time:2017-02-21 Source: eefocusKeywords:MSP430F5438  UART  ACLK Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Before initializing UART0, you need to initialize ACLK, SMCLK and MCLK. In the sample code, XT1 is used, ACLK is 32768, and SMCLK and MCLK are about 8MHZ.

2. The UART clock can refer to ACLK or SMCLK. In this example, ACLK is used. Since the ACLK clock is used, the serial port rate cannot exceed 32768. 9600 is a more appropriate choice.

3. There are two modes for generating baud rate of MSP430, low-frequency baud rate generation and oversampling baud rate generation. Low-frequency baud rate generation is used in the code.

4. The beginning of the code calls stdio, and the putchar function is rewritten in the function macro to direct the single-byte output to UART.

5. After the code is initialized, it outputs Hello MSP430, and then directly reflects the data received by the serial port. For example, if 123456 is sent, 123456 will be returned.


  1. // Clock defaults  

  2. // FLL clock FLL select XT1  

  3. // Auxiliary clock ACLK selects XT1 32768Hz  

  4. // Main system clock MCLK selection DCOCLKDIV 8000000Hz  

  5. // Subsystem clock SMCLK selection DCOCLKDIV 8000000Hz  

  6. //UART clock selection ACLK  

  7. // Low frequency baud rate generation 9600-8-N-1  

  8. #include   

  9. #include   

  10. void clock_config(void);  

  11. void select_xt1(void);  

  12. void dco_config(void);  

  13. void uart_config(void);  

  14.   

  15. int main(void)  

  16. {  

  17.     clock_config(); // Initialize the clock  

  18.     uart_config();  

  19.     _UNITE();  

  20.     P4DIR |= BIT0; //P4.0 output  

  21.   

  22.     printf("Hello MSP430!\r\n");  

  23.     while(1)  

  24.     {  

  25.         P4OUT ^= BIT0;  

  26.         __delay_cycles(1000000);  

  27.     }  

  28. }  

  29.   

  30. void clock_config(void)  

  31. {  

  32.     WDTCTL = WDTPW + WDTHOLD; // Stop watchdog  

  33.     select_xt1(); // Select XT1  

  34.     dco_config();                               // ACLK = XT1 = 32.768K  

  35.                                                 // MCLK = SMCLK = 8000K  

  36. }  

  37.   

  38. void select_xt1(void)  

  39. {  

  40.     // Start XT1  

  41.     P7SEL |= 0x03; // P7.0 P7.1 peripheral function  

  42.     UCSCTL6 &= ~(XT1OFF); // XT1 is turned on  

  43.     UCSCTL6 |= XCAP_3; // Internal capacitor  

  44.     do  

  45.     {  

  46.         UCSCTL7 &= ~XT1LFOFFG; // Clear XT1 error flag  

  47.     }while (UCSCTL7&XT1LFOFFG); // Check XT1 error flag  

  48. }  

  49.   

  50. void dco_config(void)  

  51. {  

  52.     __bis_SR_register(SCG0); // Disable FLL function  

  53.     UCSCTL0 = 0x0000;                           // Set lowest possible DCOx, MODx  

  54.     UCSCTL1 = DCORSEL_5; // DCO maximum frequency is 16MHz  

  55.     UCSCTL2 = FLLD_1 + 243; // Set DCO frequency to 8MHz  

  56.                                                 // MCLK = SMCLK= Fdcoclkdiv = (N+1)X(Ffllrefclk/n)  

  57.                                                 // N is the only value that needs to be calculated  

  58.                                                 // Ffllrefclk FLL reference clock, default is XT1  

  59.                                                 // n takes the default value, which is 1  

  60.                                                 // (243 + 1) * 32768 = 8MHz  

  61.     __bic_SR_register(SCG0); // Enable FLL function  

  62.   

  63.     // Necessary delay  

  64.     __delay_cycles(250000);  

  65.   

  66.     // Clear the error flag  

  67.     do  

  68.     {  

  69.         UCSCTL7 &= ~(XT2OFFG + XT1LFOFFG + XT1HFOFFG + DCOFFG);  

  70.                                                 // Clear all oscillator error flags  

  71.         SFRIFG1 &= ~OFIFG; // Clear oscillator error  

  72.     }while (SFRIFG1&OFIFG); // Wait for clear completion  

  73. }  

  74.   

  75. void uart_config(void)  

  76. {  

  77.     P3SEL = 0x30; // Select the multiplexing function of P3.4 and P3.5  

  78.   

  79.     UCA0CTL1 |= UCSWRST; // Software reset  

  80.     UCA0CTL1 |= UCSSEL_1; // Select ACLK clock  

  81.     UCA0BR0 = 3; // Get from table  

  82.     UCA0BR1 = 0; // UCA0BRX and UCA0MCTL values  

  83.     UCA0MCTL |= UCBRS_3 + UCBRF_0;              //  

  84.     UCA0CTL1 &= ~UCSWRST;                       //  

  85.   

  86.     UCA0IE |= UCRXIE; // Enable receive interrupt  

  87. }  

  88.   

  89. int putchar(int ch)  

  90. {  

  91.     UCA0TXBUF = ch;  

  92.     while(!(UCA0IFG & UCTXIFG));  

  93.     return ch;  

  94. }  

  95.   

  96. #pragma vector=USCI_A0_VECTOR  

  97. __interrupt void USCI_A0_ISR(void)  

  98. {  

  99.   switch(__even_in_range(UCA0IV,4))  

  100.   {  

  101.   case 0:break;  

  102.   case 2: //Receive interrupt  

  103.     while (!(UCA0IFG&UCTXIFG)); // Wait for sending to complete  

  104.     UCA0TXBUF = UCA0RXBUF; // Receive buffer  

  105.     break;  

  106.   case 4:break; // Send interrupt  

  107.   default: break;  

  108.   }  

  109. }  



Keywords:MSP430F5438  UART  ACLK Reference address:MSP430F5438 Study Notes UART ACLK 9600-8-N-1

Previous article:MSP430F5438 Study Notes UART SMCLK 115200-8-N-1
Next article:MSP430Ware study notes UART SMCLK 115200-8-N-1

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号