MSP430F5438 Study Notes UART ACLK 9600-8-N-1[Copy link]
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 clock of UART can refer to ACLK or SMCLK. In this example, ACLK is referenced. Since the ACLK clock is referenced, the serial port rate cannot exceed 32768. It is more appropriate to choose 9600. 3. There are two modes for generating the 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. At the beginning of the code, stdio is called, and the putchar function is rewritten in the function macro to direct it to the UART single-byte output. 5. After the code is initialized, Hello MSP430 is output, and then the data received by the serial port is directly reflected. For example, sending 123456 will return 123456. // Clock default // FLL clock FLL selects XT1 // Auxiliary clock ACLK selects XT1 32768Hz // Main system clock MCLK selects DCOCLKDIV 8000000Hz // Subsystem clock SMCLK selects DCOCLKDIV 8000000Hz // UART clock selects ACLK // Low frequency baud rate generation 9600-8-N-1 #include
#include
void clock_config(void); void select_xt1(void); void dco_config(void); void uart_config(void); int main(void) { clock_config(); // initialize clock uart_config(); _EINT(); P4DIR |= BIT0; // P4.0 outputs printf("Hello MSP430!\r\n"); while(1) { P4OUT ^= BIT0; __delay_cycles(1000000); } } void clock_config(void) { WDTCTL = WDTPW + WDTHOLD; // stop watchdog select_xt1(); // select XT1 dco_config(); // ACLK = XT1 = 32.768K // MCLK = SMCLK = 8000K } void select_xt1(void) { // start XT1 P7SEL |= 0x03; // P7.0 P7.1 peripheral function UCSCTL6 &= ~(XT1OFF); // XT1 turns on UCSCTL6 |= XCAP_3; // internal capacitor do { UCSCTL7 &= ~XT1LFOFFG; // clear XT1 error flag }while (UCSCTL7&XT1LFOFFG); // detect XT1 error flag } void dco_config(void) { __bis_SR_register(SCG0); // disable FLL function UCSCTL0 = 0x0000; // Set lowest possible DCOx, MODx UCSCTL1 = DCORSEL_5; // DCO maximum frequency is 16MHz UCSCTL2 = FLLD_1 + 243; // Set DCO frequency to 8MHz // MCLK = SMCLK= Fdcoclkdiv = (N+1)X(Ffllrefclk/n) // N is the only value that needs to be calculated // Ffllrefclk FLL reference clock, default is XT1 // n takes the default value, which is 1 // (243 + 1) * 32768 = 8MHz __bic_SR_register(SCG0); // Enable FLL function // Necessary delay __delay_cycles(250000); // Clear error flag do { UCSCTL7 &= ~(XT2OFFG + XT1LFOFFG + XT1HFOFFG + DCOFFG); // Clear all oscillator error flags SFRIFG1 &= ~OFIFG; // Clear oscillator error }while (SFRIFG1&OFIFG); // Wait for clearing to complete } void uart_config(void) { P3SEL = 0x30; // Select the multiplexing function of P3.4 and P3.5 UCA0CTL1 |= UCSWRST; // Software reset UCA0CTL1 |= UCSSEL_1; // Select ACLK clock UCA0BR0 = 3; // Look up the table to get UCA0BR1 = 0; // UCA0BRX and UCA0MCTL values UCA0MCTL |= UCBRS_3 + UCBRF_0; // UCA0CTL1 &= ~UCSWRST; // UCA0IE |= UCRXIE; // Enable receive interrupt } int putchar(int ch) { UCA0TXBUF = ch; while(!(UCA0IFG & UCTXIFG)); return ch; } #pragma vector=USCI_A0_VECTOR __interrupt void USCI_A0_ISR(void) { switch(__even_in_range(UCA0IV,4)) { case 0:break; case 2: // Receive interrupt while (!(UCA0IFG&UCTXIFG)); // Wait for sending to complete UCA0TXBUF = UCA0RXBUF; // Receive buffer break; case 4:break; // Send interrupt default: break; } }
Very good, this is very good, thanks for sharing, it explains the serial port usage of MSP430F5438 concisely and to the point
Details
Published on 2018-12-25 08:56