MSP430 Embedded Programming Tutorial TUTORIAL


Sending ADC data on Serial Port


In this example we will be looking at how to gather the ADC data from the MSP430 and throw the data on the Serial Port.

A/D conversion and Sending to Serial Port


Go through the following code.

 //******************************************************************************
//  MSP-FET430F149 Demo - ADC12, Sample A1, Send the level at Serial Port
//
//  Description: A single sample is made on A1 with reference to AVcc.
//  Software  start sample and conversion - Send out the
//  Analog voltage level to the serial port as two bytes.
//
//Additionally if the voltage is over 0.5*Vcc it makes LED at P6.2 glow
//
//                MSP430F149
//             -----------------
//         /|\|              XIN|-
//          | |                 |
//          --|RST          XOUT|-
//            |                 |
//      Vin-->|P6.1/A1      P6.2|-->
//            |                 |
//            |             P3.4|----------->
//            |                 | 9600 - 8N1
//            |             P3.5|<-----------
//
// referencedesigner.com
// Some of the Original codes are from Texas Instruments
//******************************************************************************

#include  <msp430x14x.h>

unsigned int delay ( unsigned int x)
{
unsigned int i,j;
for (i = 0; i<= x; i++)
  {
  for(j=0;j<=1000; j++) ;
  }
return 0;
}


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

  // Serial Port Settings

  P3SEL |= 0x30;                            // P3.4,5 = USART0 TXD/RXD
  ME1 |= UTXE0 + URXE0;                     // Enable USART0 TXD/RXD
  UCTL0 |= CHAR;                            // 8-bit character
  UTCTL0 |= SSEL0;                          // UCLK = ACLK
  UBR00 = 0x03;                             // 32k/9600 - 3.41
  UBR10 = 0x00;                             //
  UMCTL0 = 0x4A;                            // Modulation
  UCTL0 &= ~SWRST;                          // Initialize USART state machine
  IE1 |= URXIE0;                            // Enable USART0 RX interrupt

  for (;;)
  {
    // This should actually happen in a timer interrupt where
    // we may like to sample only once in, say 1 second
    delay(500);
    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)
{
 unsigned short lResultRSSI ;
 unsigned char part1, part2;

    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)

   lResultRSSI = ADC12MEM0 ;                          // RXBUF0 to TXBUF0

  part1 = (lResultRSSI & 0x00FF); // lsb
  part2 = (lResultRSSI & 0xFF00) >> 8; // msb

  while (!(IFG1 & UTXIFG0));
  TXBUF0 = part2; // We send MSB first

  while (!(IFG1 & UTXIFG0));
  TXBUF0 = part1;

}


Copy paste this code in your IAR embedder system and run the code. You should set the Serial Port connecting your MSP430 to the computer through a RS232 level shifter. Set the buad rate at 9600, 8 Data bits, no pary and 1 stop bit. Notice that you should not be looking at the ascii text, which is a norm at Hyperterminal. Instead, use a serial port terminal that will display the data in hex. If the voltage at your A1 port is about 1.5V and your board is running at 3.0V, this will typcally give out data close to 07 FF ( the data is in hex) . If you increase the cvoltage input close to 3.0V it will give 0F FF. If it is 0V, it will give out 00 00.

Notice that the delay loop is written as a for loop. This is not the best practice, but it works. A timer will be a better way to implement the delay. In fact, within the timer, we may call the ADC conversion steps.