MSP430 Embedded Programming Tutorial TUTORIAL


Analog to Digital COnversionDC


MSP430 supports 12 Bit Analog to Digital conversion ports that can be used in a number of ways. There are original sample codes available at Texas instruments wesite. This code is slighhtly modified version of a code. I have changed the input port from A0 to A1 for analog input.

A/D conversion example


Here is the example code for the analog to digital conversion. Hook up a power supply 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
//
//  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
//
// referencedesigner.com
// Original code from Texas Instruments
//******************************************************************************

#include  <msp430x14x.h>

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
#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 embedder 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.