Skip to content

Instantly share code, notes, and snippets.

@spirilis
Created November 17, 2017 03:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spirilis/33eb5165fc8403a8b2883ea65d946c9b to your computer and use it in GitHub Desktop.
Save spirilis/33eb5165fc8403a8b2883ea65d946c9b to your computer and use it in GitHub Desktop.
Basic example of MSP430FR2433 UART usage with driverlib
void main() {
...
// eUSCI_A0 support
P1SEL1 &= ~(BIT4 | BIT5);
P1SEL0 |= BIT4|BIT5;
PMM_unlockLPM5();
...
// MCLK, SMCLK are both at 16MHz running off DCO with FLL bound to XT1 crystal
...
// Initialize eUSCI_A0
EUSCI_A_UART_initParam ea0Init;
ea0Init.uartMode = EUSCI_A_UART_MODE;
ea0Init.selectClockSource = EUSCI_A_UART_CLOCKSOURCE_SMCLK;
ea0Init.parity = EUSCI_A_UART_NO_PARITY;
ea0Init.overSampling = EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION;
ea0Init.msborLsbFirst = EUSCI_A_UART_LSB_FIRST;
ea0Init.numberofStopBits = EUSCI_A_UART_ONE_STOP_BIT;
uint32_t brDiv, brMod;
brDiv = (16000000 << 4) / 115200;
brMod = brDiv & 0xFFF0;
brDiv >>= 8;
brMod >>= 4;
ea0Init.clockPrescalar = brDiv;
ea0Init.firstModReg = brMod & 0x0F;
ea0Init.secondModReg = brMod >> 4;
EUSCI_A_UART_init(EUSCI_A0_BASE, &ea0Init);
EUSCI_A_UART_enable(EUSCI_A0_BASE);
EUSCI_A_UART_resetDormant(EUSCI_A0_BASE);
EUSCI_A_UART_enableInterrupt(EUSCI_A0_BASE, EUSCI_A_UART_TRANSMIT_INTERRUPT | EUSCI_A_UART_RECEIVE_INTERRUPT);
__enable_interrupt();
...
}
// ISR
#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector=USCI_A0_VECTOR
__interrupt void USCI_A0_ISR(void)
#elif defined(__GNUC__)
void __attribute__ ((interrupt(USCI_A0_VECTOR))) USCI_A0_ISR (void)
#else
#error Compiler not supported!
#endif
{
switch(__even_in_range(UCA0IV,USCI_UART_UCTXCPTIFG))
{
case USCI_NONE: break;
case USCI_UART_UCRXIFG:
P1OUT |= BIT0; // Switch on red LED when byte received; TX vector will switch it off, it should be a short flicker
while(UCA0STATW&UCBUSY); // Busy-waits inside ISR is bad code, but this is a stupid simple example
UCA0TXBUF = UCA0RXBUF;
__no_operation();
break;
case USCI_UART_UCTXIFG:
P1OUT &= ~BIT0;
break;
case USCI_UART_UCSTTIFG: break;
case USCI_UART_UCTXCPTIFG: break;
default: break;
}
}
@3DgeekStore
Copy link

I don't know why but the line 19: brDiv = (16000000 << 4) / 115200; only works if adjust the division to match 104 us bit time who will be like brDiv = (16000000 << 4) / 145000; in my launchpad, confirmed with a scope. with the supposed correct 115200 the time is 128 us instead of 104 us. That is the way i found for a reliable communication

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment