Home > Uncategorized > MSP430 Analog to Digital Conversion Tutorial

MSP430 Analog to Digital Conversion Tutorial

February 28th, 2011 Leave a comment Go to comments

Some of the MSP430 series processors ( for example MSP430F149) supports 12 Bit A to D ( Analog to Digital conversion ports ) that can be used to monitor and Analog signal level. This code is slighhtly modified version of a code available at Texas Instruments’s website. The Input port has been changed from A0 to A1 for analog input.

MSP430 A/D conversion example

Here is the MSP430 example code for the analog to digital conversion. Hook up a analog supply source ( for example a battery) at port Pin A1 of the MSP430. When the supply voltage increases beyond 1.5V, you will LED going ON

//******************************************************************************
// MSP-FET430P140 Demo – ADC12, Sample A1, Set P6.2 if A1 > 0.5*AVcc
//
// Starredreviews description
// Description: A single sample is made on A0 with reference to AVcc.
// Software sets ADC10SC to start sample and conversion – ADC12SC
// automatically cleared at EOC. ADC12 internal oscillator times sample (16x)
// and conversion. In Mainloop MSP430 waits in LPM0 to save power until ADC12
// conversion complete, ADC12_ISR will force exit from LPM0 in Mainloop on
// reti. If A0 > 0.5*AVcc, P6.2 set, else reset.
//
// MSP430F149
// —————–
// /|\| XIN|-
// | | |
// –|RST XOUT|-
// | |
// Vin–>|P6.1/A1 P6.2|–> LED
//
// starredreviews.com
// Original code from Texas Instruments
//******************************************************************************

#include

void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop Watch Dog Timer
ADC12CTL0 = SHT0_2 + ADC12ON; // Set sampling time, turn on ADC12
ADC12CTL1 = SHP;
ADC12IE = 0x01; // Enable interrupt
ADC12MCTL0 = 0x01 ;
ADC12CTL0 |= ENC ; // Conversion enabled
P6SEL |= 0x02 ; // P6.1 ADC option select
P6DIR |= 0x04; // P6.2 is output for LED

for (;;)
{
// This should actually happen in a timer interrupt where
// we may like to sample only once in, say 1 second

ADC12CTL0 |= ADC12SC; // Sampling open
_BIS_SR(CPUOFF + GIE); // LPM0, ADC12_ISR will force exit
}
}

// ADC12 interrupt service routine
// Comments by starredreviews.com

#pragma vector=ADC12_VECTOR
__interrupt void ADC12_ISR (void)
{

if (ADC12MEM0 < 0x7FF)
P6OUT &= ~0x04; // Clear P6.2 – LED off
else
P6OUT |= 0x04; // Set P6.2 – LED on
_BIC_SR_IRQ(CPUOFF); // Clear CPUOFF bit from 0(SR)
}

Copy paste this code in your IAR embedded system and run the code.

Some light exercises –

1. Change the program so that instead of the infinite for loop the Analog to digital conversion is done every one second.

2. At every one second output the value of the voltage at the serial port.

Categories: Uncategorized Tags:
  1. Micro
    October 17th, 2011 at 21:44 | #1

    ADC12MEM0 < 0x7FF ?????

    How do define 1.5 v for ADC12MEM0 ? any body can please elobrate it?