This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

MSP430FR2311: Send integer value over UART

Part Number: MSP430FR2311
Other Parts Discussed in Thread: ENERGIA

Hi, I;m having problem sending an int value of 1023 on the Serial Monitor. I don't see 1023 on the Serial with this code if ADC_Result is 1023 or 1 or any other number.

#include <msp430.h>

int ADC_Result;
#include <stdio.h>
void init_uart();
void init_led();

int main(void)
{
    WDTCTL = WDTPW | WDTHOLD;                                 // Stop WDT

    init_uart();
    init_led();

    // Configure GPIO
    //P1DIR |= BIT0;                                            // Set P1.0/LED to output direction
    //P1OUT &= ~BIT0;                                           // P1.0 LED off

    // Configure ADC A1 pin
    P1SEL0 |= BIT1;
    P1SEL1 |= BIT1;

    // Disable the GPIO power-on default high-impedance mode to activate
    // previously configured port settings
    PM5CTL0 &= ~LOCKLPM5;

    // Configure ADC10
    ADCCTL0 |= ADCSHT_2 | ADCON;                              // ADCON, S&H=16 ADC clks
    ADCCTL1 |= ADCSHP;                                        // ADCCLK = MODOSC; sampling timer
    ADCCTL2 |= ADCRES;                                        // 10-bit conversion results
    ADCIE |= ADCIE0;                                          // Enable ADC conv complete interrupt
    ADCMCTL0 |= ADCINCH_1 | ADCSREF_1;                        // A1 ADC input select; Vref=1.5V

    // Configure reference
    PMMCTL0_H = PMMPW_H;                                      // Unlock the PMM registers
    PMMCTL2 |= INTREFEN;                                      // Enable internal reference
    __delay_cycles(400);                                      // Delay for reference settling

    while(1)
    {
        ADCCTL0 |= ADCENC | ADCSC;                            // Sampling and conversion start
        __bis_SR_register(LPM0_bits | GIE);                   // LPM0, ADC_ISR will force exit
        if (ADC_Result < 0x155)
            P1OUT &= ~BIT0;                                   // Clear P1.0 LED off
        else
            P1OUT |= BIT0;                                    // Set P1.0 LED on
        while (!(UCA0IFG & UCTXIFG)); // wait for USCI_A0 TX buffer to ready

        char ADC_Char[16];
        sprintf(ADC_Char, "%d", ADC_Result);
        UCA0TXBUF = ADC_Result;

        __delay_cycles(5000);
    }
}

//This function intiilizes the UART so that it can have 8 data bits, no parity, 1 stop bit and baudrate of 9600

void init_uart()
{
    // Configure UART pins
    P1SEL0 |= BIT6 | BIT7;                    // set 2-UART pin as second function

    // Configure UART
    UCA0CTLW0 |= UCSWRST;                     //Sets softare reset enable
    UCA0CTLW0 |= UCSSEL__SMCLK;               // Set SMCLK as BRCLK to be used for baud rate of 115200

    // Baud Rate calculation. Setting BaudRate to 115200
    UCA0BR0 = 8;                              // 1000000/115200 = 8.68 INT(N) = 8
    UCA0MCTLW = 0xD600;                       // 1000000/115200 - INT(1000000/115200)=0.68
    UCA0BR1 = 0x00;                              // UCBRSx value = 0xD6

    UCA0CTLW0 &= ~UCSWRST;                    // Initialize eUSCI

}



void init_led(){
    // Configure GPIO
    P1DIR |= BIT0;                                            // Set P1.0/LED to output direction
    P1OUT &= ~BIT0;                                           // P1.0 LED off
}


// ADC interrupt service routine
#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector=ADC_VECTOR
__interrupt void ADC_ISR(void)
#elif defined(__GNUC__)
void __attribute__ ((interrupt(ADC_VECTOR))) ADC_ISR (void)
#else
#error Compiler not supported!
#endif
{
    switch(__even_in_range(ADCIV,ADCIV_ADCIFG))
    {
        case ADCIV_NONE:
            break;
        case ADCIV_ADCOVIFG:
            break;
        case ADCIV_ADCTOVIFG:
            break;
        case ADCIV_ADCHIIFG:
            break;
        case ADCIV_ADCLOIFG:
            break;
        case ADCIV_ADCINIFG:
            break;
        case ADCIV_ADCIFG:
            ADC_Result = ADCMEM0;
            __bic_SR_register_on_exit(LPM0_bits);              // Clear CPUOFF bit from LPM0
            break;
        default:
            break;
    }
}

  • You are putting the int ADC_Result into UCA0TXBUF. AT the very least you want ADC_Char. You actually want to step through ADC_Char and send a byte at a time.
  • How do I step through ADC_Char and send one byte at a time? If I send ADC_Char I still don't get what I expect. I'm confused on how to send on byte at a time.
  • I tried doing this but I'm getting errors  doing the splitting with high and low.



    #include <msp430.h> int ADC_Result; char random = 1; #include <stdio.h> void init_uart(); void init_led(); int main(void) { WDTCTL = WDTPW | WDTHOLD; // Stop WDT init_uart(); init_led(); // Configure GPIO //P1DIR |= BIT0; // Set P1.0/LED to output direction //P1OUT &= ~BIT0; // P1.0 LED off // Configure ADC A1 pin P1SEL0 |= BIT1; P1SEL1 |= BIT1; // Disable the GPIO power-on default high-impedance mode to activate // previously configured port settings PM5CTL0 &= ~LOCKLPM5; // Configure ADC10 ADCCTL0 |= ADCSHT_2 | ADCON; // ADCON, S&H=16 ADC clks ADCCTL1 |= ADCSHP; // ADCCLK = MODOSC; sampling timer ADCCTL2 |= ADCRES; // 10-bit conversion results ADCIE |= ADCIE0; // Enable ADC conv complete interrupt ADCMCTL0 |= ADCINCH_1 | ADCSREF_1; // A1 ADC input select; Vref=1.5V // Configure reference PMMCTL0_H = PMMPW_H; // Unlock the PMM registers PMMCTL2 |= INTREFEN; // Enable internal reference __delay_cycles(400); // Delay for reference settling while(1) { ADCCTL0 |= ADCENC | ADCSC; // Sampling and conversion start __bis_SR_register(LPM0_bits | GIE); // LPM0, ADC_ISR will force exit if (ADC_Result < 0x155) P1OUT &= ~BIT0; // Clear P1.0 LED off else P1OUT |= BIT0; // Set P1.0 LED on while (!(UCA0IFG & UCTXIFG)); // wait for USCI_A0 TX buffer to ready char ADC_Char[16]; sprintf(ADC_Char, "%d", ADC_Result); char high = (unsigned char)(ADC_Char>>8); char low = ADC_Char & 0xff; UCA0TXBUF = high; while (!(UCA0IFG & UCTXIFG)); // wait for USCI_A0 TX buffer to ready UCA0TXBUF = low; __delay_cycles(5000); } } //This function intiilizes the UART so that it can have 8 data bits, no parity, 1 stop bit and baudrate of 9600 void init_uart() { // Configure UART pins P1SEL0 |= BIT6 | BIT7; // set 2-UART pin as second function // Configure UART UCA0CTLW0 |= UCSWRST; //Sets softare reset enable UCA0CTLW0 |= UCSSEL__SMCLK; // Set SMCLK as BRCLK to be used for baud rate of 115200 // Baud Rate calculation. Setting BaudRate to 115200 UCA0BR0 = 8; // 1000000/115200 = 8.68 INT(N) = 8 UCA0MCTLW = 0xD600; // 1000000/115200 - INT(1000000/115200)=0.68 UCA0BR1 = 0x00; // UCBRSx value = 0xD6 UCA0CTLW0 &= ~UCSWRST; // Initialize eUSCI } void init_led(){ // Configure GPIO P1DIR |= BIT0; // Set P1.0/LED to output direction P1OUT &= ~BIT0; // P1.0 LED off } // ADC interrupt service routine #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__) #pragma vector=ADC_VECTOR __interrupt void ADC_ISR(void) #elif defined(__GNUC__) void __attribute__ ((interrupt(ADC_VECTOR))) ADC_ISR (void) #else #error Compiler not supported! #endif { switch(__even_in_range(ADCIV,ADCIV_ADCIFG)) { case ADCIV_NONE: break; case ADCIV_ADCOVIFG: break; case ADCIV_ADCTOVIFG: break; case ADCIV_ADCHIIFG: break; case ADCIV_ADCLOIFG: break; case ADCIV_ADCINIFG: break; case ADCIV_ADCIFG: ADC_Result = ADCMEM0; __bic_SR_register_on_exit(LPM0_bits); // Clear CPUOFF bit from LPM0 break; default: break; } }

  • Can you send *any* serial data? Do the baud rates on the MSP and PC match up? Do you have a scope or logic analyzer to verify what is being sent on the serial port?

    (What you did here was send the *address* of ADC_Char down the serial port. I think you need to review C char strings.)

  • If i send an ASCII charcater like 'R' or anything else I can send data and it receives it. I can see 'R' being displayed and the baud rates are matched up. I'm not sure how I can split up the ADC_Char into two bytes and send it on the TX buffer. Not sure of C char strings too much.
  • for (i=0; i<strlen(ADC_Char); i++) {
    UCA0TXBUF = ADC_Char[i];
    // if serial is not buffered, pause to send character
    }
  • You might want to look at Energia. While it takes away low level control, it takes care of a lot of the hassles like writing strings to serial.

**Attention** This is a public forum