Reading analog values on the msp430

I finally got all the components to use the bag of electret microphones I got last year. Using a LM36 audio amp I set up a simple circuit to read values from the microphone and set LED's corresponding to the value read.

I had trouble finding a solid example of doing analog reads in a loop for the msp430. This program, sets up LED's(On P1.1 and P1.6), sets up the ADC, then sits in a loop. If the value on the ADC is grater than 512 the LED will go red.

I tested this by shouting at the micro controller, something I have done many times before, but never with such satisfying results.

#include <msp430g2553.h>

unsigned int adcvalue = 0;

void configureADC(void);

int
main(void)
{
    WDTCTL = WDTPW + WDTHOLD;       //Stop the Watch dog

    P1DIR |= 0x41;                  //Enabled P1.0 and P1.6
    P1OUT = 0x41;

    BCSCTL1 = CALBC1_1MHZ;          //Set range
    BCSCTL2 &= CALBC1_1MHZ;         //SMCLK = DCO = 1MHz

    P1SEL |= BIT3;

    configureADC();
    __enable_interrupt();

    while(1) {
        __delay_cycles(20000);
        ADC10CTL0 |= ENC + ADC10SC;         //Sampling and conversion start
        adcvalue = ADC10MEM;                //Read from the ADC

        if(adcvalue > 512)
            P1OUT = 0x40;
        else
            P1OUT = 0x01;

    }

    return 0;
}

void
configureADC(void)
{
    ADC10CTL1 = INCH_3 + ADC10DIV_3;
    ADC10CTL0 = SREF_0 + ADC10SHT_3 + ADC10ON + ADC10IE;
    ADC10AE0 |= BIT3;
}

hacking msp430

Posted on