I am attempting to use the ADC on my mspf5438 to trigger an LED at this point and later on a initiate UART communication. For now though I am trying to just light the LED. I am using the following code but the input voltage seems to be different than what I'm actually getting from conversions. My input is a sine wave with a changing peak to peak value. It has an average value of 2.1V. I want to use an externally supplied Vref+ of 3.3V and Vref- of 0V, which I have supplied to pins 9 and 10 of the MSP430. My input to the ADC is denoted as P6.5/A5, which is pin 2 on the msp. My LED is connected to P6.1/A1, pin 98. I have a few specific questions in addition to asking someone to review my code.
1. Do I need to enable pins 9 and 10 for the reference voltage as inputs in addition to supplying the voltage?
2. If I set a state for a specific port, such as P6OUT |= 0X02, does it only affect the pins which I have configured as outputs?
/////////////////////ADC Setup//////////////////////////////
ADC12CTL0 = ADC12SHT02 + ADC12ON + ADC12REFON; // Sampling time, ADC12 on
ADC12CTL1 = ADC12SHP; // Use sampling timer
ADC12IE = 0x01; // Enable interrupt
ADC12CTL0 |= ADC12ENC;
P6SEL |= 0x20; // P6.5 ADC option select
P6DIR |= 0x02; // P6.1 output for LED X1
P6OUT = 0x00; //Start LED in off state
while(1) //Continuous conversions?
{
ADC12CTL0 |= ADC12SC; // Start sampling/conversion
__bis_SR_register(LPM0_bits + GIE); // LPM0, ADC12_ISR will force exit
__no_operation(); // For debugger
}
} //End of main()
#pragma vector = ADC12_VECTOR
__interrupt void ADC12_ISR(void)
{
switch(__even_in_range(ADC12IV,34))
{
case 0: break; // Vector 0: No interrupt
case 2: break; // Vector 2: ADC overflow
case 4: break; // Vector 4: ADC timing overflow
case 6: // Vector 6: ADC12IFG0
last_state = ADC12MEM0; //This little bit of code lets me
if(last_state > new_state) //store my highest value converted
{ //for later review
new_state = last_state;
}
if(ADC12MEM0>= 0X9BB) // If voltage is larger than this number, light LED
P6OUT |= 0x02; // P6.1 = 1
else
P6OUT &= ~0x02; // NAND P6 Port with 0x02, should turn
//off LED ON P6.1
__bic_SR_register_on_exit(LPM0_bits); // Exit active CPU
default: break;
}
}