Hi,
I have a strange problem using the ADC.
I have no problem sampling 10-bit ADC values but when the value is stable around 768 (0b1100000000) I get "fake" values!!? Both 512 and 1023. This only occurs around 768.
I have narrowed down the code only to read the ADC value to exclude that the code is wrong:
The code is now simply in a infinitive loop:
iLoad = ADCR; (iLoad is an integer)
I have tried longer sample time, digital buffer disable etc.. I suppose that there is something that I'm missing here!?
I'm using the DEMO9S08SG8.
/Jonas
Moving on with the problem and finally I have found a solution! The problem is that I cannot explain why it works now!?
I changed the code to read from two different channels and now the values are stable!?
Can anyone explain why?
if (ADCSC1_ADCH == 2)
ADCSC1_ADCH = 3;
else
ADCSC1_ADCH = 2;
...
if(ADCSC1_COCO){ // AD conversion ready?
if(ADCSC1_ADCH == 2) // Channel 2?
iVoltage = ADCR; // Read AD value channel 2
else
iLoad = ADCR; // Read AD value channel 3
}
In myfinal design I will read from two channels so I'm glad that it works now but also a bit scary since I still don't know what caused the problem before...
Any Ideas?
Hello,
I cannot explain the reason for the reading instability, however I might possibly have expected the following code structure, assuming both ADCO and AIEN bits are zero (single conversion and no interrupt) -
if (ADCSC1_ADCH == 2)
ADCSC1 = 3;
else
ADCSC1 = 2;
...
while (ADCSC1_COCO == 0); // Wait until conversion ready
if (ADCSC1_ADCH == 2) // Channel 2?
iVoltage = ADCR; // Read AD value channel 2
else
iLoad = ADCR; // Read AD value channel 3
The code now waits until the conversion is complete.
Regards,
Mac
Hi Mac,
Sorry there was a mistake from my side copying the code. This is how I use it:
Continuous conversion and no interrupt.
if(ADCSC1_COCO){ // Conversion ready?
if (ADCSC1_ADCH == 2) // Channel 2?
iVoltage = ADCR; // Read AD value channel 2
else
iLoad = ADCR; // Read AD value channel 3
}
Good point though! Thanks!
/Jonas
Hello Jonas,
It makes no sense to use continuous conversion mode if you are monitoring more than one ADC channel. Since you need to alternate between two channels, each conversion must be explicitly started using the required channel.
Regards,
Mac