nexusone1984 32 Posted April 10, 2011 Share Posted April 10, 2011 I trying to read from Pin 8 a analog voltage with vcc as reference. Setup the ADC 8 as I so far tried: ADC10CTL1 = INCH_8; ADC10CTL0 = ADC10SHT_2 + MSC + ADC10ON + ADC10IE; ADC10AE0 |= 0x80; // P1.7 ADC10 option select Read ADC 8: ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start __bis_SR_register(CPUOFF + GIE); // LPM0 with interrupts enabled k = (ADC10MEM / 1024) * 8; Convert to 8 bit bar graph. 0 = no leds lit, 8 = all lit. Also is there a good programing reference manual. Quote Link to post Share on other sites
NatureTM 100 Posted April 10, 2011 Share Posted April 10, 2011 I'm assuming you don't have an interrupt service routine coded. Since you have the ADC interrupt enabled, when the ADC finishes, the cpu will exit LPM0 and the program will jump to where the ADC10 ISR should be. If there's nothing there, you'll get unexpected behavior. You could either put something in the interrupt vector, or don't enable the interrupt. Also, for "k = (ADC10MEM / 1024) * 8;" the ADC1MEM / 1024 is integer division, and the result is rounded down to the nearest whole number. The result of ADC1MEM / 1024 will always be between 0 and 1, but rounded down, so always 0. You can use algebra to change the code so it will work: "k = (ADC10MEM * 8) / 1024;" That answer will still be rounded down, but at least give a result from 0 to 8. "k = (char)((ADC10MEM / (float)1024) * 8 + 0.5);" will round appropriately, but will be slower. I've got some example code for the ADC10 I'll just copy over from my blog: #include "msp430g2231.h" #define ANALOG_PIN 1 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){ unsigned int analogValue; DCOCTL = CALDCO_1MHZ; BCSCTL1 = CALBC1_1MHZ; WDTCTL = WDTPW + WDTHOLD; // read adc repeatedly analogPinSelect(ANALOG_PIN); while(1){ analogValue = analogRead(); } } You should be able to work off that example and get what you need. jsolarski and GeekDoc 2 Quote Link to post Share on other sites
RobG 1,892 Posted April 10, 2011 Share Posted April 10, 2011 How about this: k = ADC10MEM >> 7; You will get range 0-7, but no math, besides you would never get 8th LED to light up anyway because ADC10MEM goes up to 1023 only. Quote Link to post Share on other sites
nexusone1984 32 Posted April 10, 2011 Author Share Posted April 10, 2011 Thanks for the help... NatureTM, you code work great once I made a couple of corrects to my goofs..... pin 8 should have said bit8.. or ADC07 once I changed 8 to 7 that help that. And the math change also corrected that problem. Works like I wanted it to now. Quote Link to post Share on other sites
GeekDoc 226 Posted April 12, 2011 Share Posted April 12, 2011 nexusone1984: Don't forget that little "thumbs-up" symbol at the top right of a post. Use it to give "thanks" to the author. The number of "thanks" an author has received (and given) is shown in their info on the right of their posts. This helps others gauge the author's overall helpfulness and credibility. zeke 1 Quote Link to post Share on other sites
zeke 693 Posted April 12, 2011 Share Posted April 12, 2011 @GeekDoc: Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.