read from a Analog port
example of how to read a Analog signal from one of the port pins:
In this example I use the MSP430 Launchpad board, 74hc595 for displaying the Analog input, CDS photo cell.
// MSP430G2231
// Read from a analog signal from a port, and display it in bargraph format
#include <msp430x20x2.h>
#include <io.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
char bar[9] = {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x07, 0xff };
static void __inline__delay(register unsigned int n);
#define ANALOG_PIN 8
unsigned int analogRead(){
ADC10CTL0 |= ADC10SC;
while(ADC10CTL1 & ADC10BUSY);
return ADC10MEM;
}
void analogPinSelect(unsigned int pin){
if(pin < 8){
ADC10CTL0 &= ~ENC;
ADC10CTL1 = pin << 12;
ADC10CTL0 = ADC10ON + ENC + ADC10SHT_0;
}
}
void main( void )
{
int i, j, pin = 7;
char k;
WDTCTL = WDTPW + WDTHOLD;
BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALDCO_1MHZ;
P1DIR |= 0x07;
P1SEL = 0x00;
analogPinSelect( pin );
while( 1 )
{
ADC10CTL0 |= ADC10SC; // Sampling and conversion start
k = ( analogRead() * 9) / 1024;
__inline__delay(0x8ff0);
j = bar[k];
P1OUT &= ~BIT2;
for(i=0; i < 8; i++)
{
P1OUT &= ~BIT1;
if (j & 0x01)
{
P1OUT |= BIT0;
}else{
P1OUT &= ~BIT0;
}
P1OUT |= BIT1;
j = j>>1;
}
P1OUT |= BIT2;
}
}
// Delay Routine from mspgcc help file
static void __inline__delay(register unsigned int n)
{
__asm__ __volatile__ (
"1: \n"
" dec %[n] \n"
" jne 1b \n"
: [n] "+r"(n));
}