I am trying to measure a power device's voltage using TI's MSP430. The voltage source is connected to A1 (i.e. P4) and ground (i.e. P1) on the target board.
Here is the relevant code:
ADC10CTL1 = INCH_1 + CONSEQ_0; //A1, single measurement
ADC10CTL0 = SREF_1 + ADC10SHT_3 + REFON + ADC10ON + ADC10IE + ADC10SR; //same as sample temperature sensor code
ADC10CTL1 &= ~ADC10DF; //setting binary format for ADC10MEM
ADC10CTL0 |= ENC + ADC10SC; //Sampling and conversion start
__bis_SR_register(CPUOFF + GIE); // LPM0 with interrupts enabled
//read result
I am having following problems:
I am printing the ADC10MEM contents on the AP, and I see that the leading 6 bits of ADC10MEM are all 1, instead of 0. I am unable to get the reason for the same.
If I consider only the last 10 bits of ADC10MEM, I can see that the value increases and decreases with rise and fall in voltage, but if I obtain Vin using the formula:
N = 1023 * ((Vin - VR- ) / (VR+ - VR-)), I do not get the correct value. (VR+ = 1.5V, VR- = 0V, as batteries power the target board) N: the value in ADC10MEM, in decimal
I am unable to find where I am going wrong. Do I have to enable the pin for analog input (ADC10AE0 |= 0x10), and set direction (P4DIR |= 0x01) as well?
Thanks!