Please Help, A/D converter

For C and ASSEMBLY users to post questions and code snippets for programming in C and ASSEMBLY. And for any other C or ASM course related questions.

Moderators: Benj, Mods

Post Reply
fajc05
Posts: 3
Joined: Fri Dec 08, 2006 5:27 am

Please Help, A/D converter

Post 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));

User avatar
Steve
Matrix Staff
Posts: 3433
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times

Post 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.

Mark
Posts: 209
Joined: Thu Oct 19, 2006 11:46 am
Location: Bakewell, UK
Has thanked: 20 times
Been thanked: 16 times

Post 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)?
Go with the Flow.

fajc05
Posts: 3
Joined: Fri Dec 08, 2006 5:27 am

Post 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?

User avatar
Steve
Matrix Staff
Posts: 3433
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times

Post 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).

Post Reply