Page 1 of 1

Please Help, A/D converter

Posted: Fri Dec 08, 2006 5:33 am
by fajc05
I am using the pic 16f877a and for some reason the a/d is giving us random values when there is not an input. Here is the code that I am using. Maybe someone can help me with this problem.
option_reg = 0x06;
TRISA = 0xff;
clear_wdt();

adcon1 = 0x02;
adcon0 = 0x80;
adcon0 = 0x81; // bsf ADON

delay_ms(20);

adcon0 |= 0x04;
delay_ms(20);
while(adcon0 & 0x04)
;
PORTD = 0xff;
PORTE = 0xff;
int TEMPERATURE = 0x00;

// holds the 10bit result in a 16bit variable
TEMPERATURE = (adresh|(adresl<<8));

Posted: Fri Dec 08, 2006 9:18 am
by Steve
Your result is left-justified (top bit of ADCON1 is 0), so the 10-bit result is contained in all 8 bits of adresh and the top 2 bits of adresl. This wight work better:

Code: Select all

TEMPERATURE = (adresh * 4) + (adresl >> 6);
If not, try inspecting the individual values of adresh and adresl. Also, it could be that your hardware is giving you fluctuating values.

Posted: Mon Dec 11, 2006 11:05 am
by Mark
When there is a 'not an input', i.e. the pin is floating then stray capacitance can well give rise to strange results. Perhaps unused inputs can be tied to ground (I gather this can also save power)?

Posted: Mon Dec 11, 2006 10:38 pm
by fajc05
I will try putting the unused inputs to ground. Would it be better to just specify the input i want instead of the whole port? also, how to i inspect my adresh and adresl to see what is there?

Posted: Tue Dec 12, 2006 9:41 am
by Steve
Instead of displaying the calculated TEMPERATURE variable, just display the "adresh" value instead. And then write another program to just display the "adresl" value (although this will be less useful as it is showing the least significant 2 bits only).