Jump to content

Reading the ADC from any pin

Recommended Posts

I trying to read from Pin 8 a analog voltage with vcc as reference.


Setup the ADC 8 as I so far tried:




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.

Link to post
Share on other sites

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;


 // read adc repeatedly
   analogValue = analogRead();


You should be able to work off that example and get what you need.

Link to post
Share on other sites



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.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...