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.

adc serial

Other Parts Discussed in Thread: MSP430G2553

#include <msp430g2553.h>
#include <stdbool.h>
////////////////Defines////////////////

char buffer[4];
float temp;
int value=0;

////////////////Function Protos////////////////
void TX(char *tx_message);
static char *i2a(unsigned i, char *a, unsigned r);
char *itoa(int i, char *a, int r);
void ADC_init(void);

static char *i2a(unsigned i, char *a, unsigned r)
{
    if (i/r > 0) a = i2a(i/r,a,r);
    *a = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[i%r];
    return a+1;
}

char *itoa(int i, char *a, int r)
{
    if ((r < 2) || (r > 36)) r = 10;
    if (i < 0)
    {
        *a = '-';
        *i2a(-(unsigned)i,a+1,r) = 0;
    }
    else *i2a(i,a,r) = 0;
    return a;
}

void TX(char *tx_message)
{
    while(*tx_message)
    {
        UCA0TXBUF = *tx_message;
        __delay_cycles(10000);
        tx_message++;
    }
}

void ADC_init()
{
    /* Configure ADC  Channel */
    ADC10CTL1 = INCH_5 + ADC10DIV_3 +ADC10SSEL_3;         // Channel 5, ADC10CLK/4
    ADC10CTL0 = SREF_0 + ADC10SHT_3 + ADC10ON + ADC10IE;  //Vcc & Vss as reference
    ADC10AE0 |= BIT5;                         //P1.4 ADC option


}

////////////////////////////////MAIN PROGRAM LOOP//////////////////////////////////////////////////
void main(void)
{
    WDTCTL = WDTPW + WDTHOLD;         // Stop WDT
    BCSCTL1 = CALBC1_1MHZ;            // Set DCO to 1MHz
    DCOCTL = CALDCO_1MHZ;
    ////////////////USCI setup////////////////
    P1SEL = BIT1 + BIT2 ;            // Set P1.1 to RXD and P1.2 to TXD
    P1SEL2 = BIT1 + BIT2;            //
    UCA0CTL1 |= UCSSEL_2;            // Have USCI use SMCLK AKA 1MHz main CLK
    UCA0BR0 = 104;                  // Baud: 9600, N= CLK/Baud, N= 10^6 / 9600
    UCA0BR1 = 0;                  // Set upper half of baud select to 0
    UCA0MCTL = UCBRS_1;               // Modulation UCBRSx = 1
    UCA0CTL1 &= ~UCSWRST;             // Start USCI

    //////////////SETUP ADC10 TO READ FROM CH A4 (P1.4)/////////////////////////////////////////////
    ADC_init();
    __enable_interrupt();                     // Enable interrupts.
    TX("welcome");


////////////////Main Loop////////////////
    while(1)
    {
        __delay_cycles(1000);                   // Wait for ADC Ref to settle
        ADC10CTL0 |= ENC + ADC10SC;             // Sampling and conversion start
        __bis_SR_register(CPUOFF + GIE);        // LPM0 with interrupts enabled
        ADC10CTL0 &= ~ENC;        // Disable ADC conversion
        ADC10CTL0 &= ~(REFON + ADC10ON);
        value = ADC10MEM;
        value /= 284;
        itoa(value, buffer, 10);
        TX("threshold value is:");
        TX(buffer);
        _delay_cycles(50000);

    } // End While
} // End Main Program

// ADC10 interrupt service routine
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR (void)
{
  __bic_SR_register_on_exit(CPUOFF);        // Return to active mode

}

im trying display my adc value in my serial monitor but its not shown the value. i dont know where i done the error.guys any one tell me as soon as possible.its urgent...........

  • prabhakaran krishnan said:
    static char *i2a(unsigned i, char *a, unsigned r)
    {
        if (i/r > 0) a = i2a(i/r,a,r);
        *a = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[i%r];
        return a+1;
    }

    Wow, a recursive function that uses lots of stack space and does three divisions per iteration. Short, slow and ineffective. On an MSP with quite a small ram. I can really smell stack overflows and lack of CPU power. ;)

    If you search th eforum, you'll find several ITOA implementations (fixed radix of 10) which do only use subtractions and comparisons and don't use stack space.

    prabhakaran krishnan said:
        while(*tx_message)
        {
            UCA0TXBUF = *tx_message;
            __delay_cycles(10000);
            tx_message++;
        }

    Instead of waiting for a fixed number of Cu cycles, you should check of the UCTXIFG flag which tells you when the TXBUF is ready to receive the next byte. Depending on baudrate, you may waste time or overwrite the previous, not yet sent byte.

    prabhakaran krishnan said:
    im trying display my adc value in my serial monitor but its not shown the value.

    Do you see the 'welcome' message?

    prabhakaran krishnan said:
    value = ADC10MEM;
            value /= 284;
            itoa(value, buffer, 10);

    Why is value a float when you implicitely convert it to int on the very next instruction anyway? It does some unnecessarily complex float division.

**Attention** This is a public forum