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));
Please Help, A/D converter
- Steve
- Matrix Staff
- Posts: 3433
- Joined: Tue Jan 03, 2006 3:59 pm
- Has thanked: 114 times
- Been thanked: 422 times
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:
If not, try inspecting the individual values of adresh and adresl. Also, it could be that your hardware is giving you fluctuating values.
Code: Select all
TEMPERATURE = (adresh * 4) + (adresl >> 6);