Hi,
Here is the code I use... (Note I'm using software polling)
/* Channels */
#define ADCCH0 0x00
#define ADCTEMP 0x1A
#define ADCBG 0x1B
#define ADCDISABLE 0x1F
byte PTAstate;
byte PTBstate;
word volts;
void main(void) {
/* Init ADC into single conversion mode, low power hence the stop mode */
ADCSC1 = 0x1F; // Disables all channels
while(1) {
EnterStop();
ADCChannel(ADCTEMP);
volts = ADCCheck();
ADCChannel(ADCCH0);
volts = ADCCheck();
}
}
/* Helper Functions */
void ADCChannel(byte Channel) {
ADCSC1 &= Channel;
}
void EnterStop(void) {
SPMSC2 = 0x00; /* Make sure stop mode3 is entered */
PTAstate = PTADD;
PTBstate = PTBDD;
PTADD = 0xFF;
PTBDD = 0xFF; /* Set all as output to lower power consumption */
asm STOP;
PTADD = PTAstate;
PTBDD = PTBstate;
}
word ADCcheck(void) {
volatile word DataOut;
volatile byte DataH;
DataOut = 0;
DataH = 0;
while (ADCSC1_COCO != 1);
DataOut = ADCRL;
DataH = ADCRH;
DataOut += DataH * 256;
return DataOut;
}
Out of this I get the correct temperature the first time. The second loop time I get the same value I got for channel 0. This is the same for the Bandgap voltage. But... if I check the bandgap, then the temperature, there isn't an issue. It's only when I check channel 0. I've also tried channel 1 and it works like channel 0, only outputting the same value as the channel I select.
I ommited the setup code I used for the ADC. It uses single conversion, low power, slow, div 8 clock, no compare function and no interrupt.
I'm trying to get this to work in a low power fashion hence the stop modes and whatnot.
Thanks for your help. Let me know if I need to clarify more.