reading voltage using TI's MSP430 FF22x4
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; //Sam开发者_运维知识库pling 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!
TI provides nice example code for those MSP430 here:
http://www.ti.com/litv/zip/slac123d
One of them also reads the voltage and you should use it to compare to what you're doing. I don't remember about the ADC10DF, but it would seem to be a suspicious thing to do (since your error is related to the data out).
Also, you say that you read the result. If you don't wait enough, the result isn't valid. You didn't show any interrupt routine so perhaps there something there going on.
精彩评论