PDA

View Full Version : 12F675 ADC 'Issues'



harrisondp
- 30th March 2005, 08:22
Hello everyone,

I have read that the 10bit reading from the onboard ADC on the 12F675 should range from 0 to 1023 (1024 steps). I can never achieve this. I have tried two different ways of reading the adc value (one with the built in adc command and one with messing with the registers directly). I have also tried different test boards (one is a homemade pcb etched with the toner transfer method and another was built on a standard breadboard).

The ADC's vref is set to vcc. I can get 0 whenever I set the AN0 pin to GND. I get around 1012 - 1013 when I set the AN0 pin to VCC (+5v). My vcc is a stable 5v dc (checked with two meters).

My current code for reading the adc is:



reading var word
avgloop var byte
avgtotal var word

CMCON = 7 ' Comparators OFF
ANSEL = %00100001 ' AN0 enabled (last 4 bits are enable bits {AN3:AN0}
ADCON0 = %10000001 ' VCC Vref / Turn On ADC Module

readadc:
' Take 20 readings and average them for accuracy
avgtotal = 0
for avgloop = 1 to 20
ADCON0.1 = 1 ' Start ADC
loopadc:
if ADCON0.1 = 1 then goto loopadc ' Check to make sure ADC conversion finished..
pause 5 ' Give ADC Settling Time
reading.highbyte = ADRESH
reading.lowbyte = ADRESL
avgtotal = avgtotal + reading
next avgloop
reading = avgtotal / 20
return


I am using a 12F675 with pbp 2.44.

Harrison

Melanie
- 30th March 2005, 10:33
What's the Pause 5 in aid of? You want to give the ADC time to settle after you first switch it on (ADCON0 = %10000001 statement)... not AFTER it's taken the result (kinda after the event in my opinion!).

The rest of your code looks good, but that messy 'If...goto' in the middle of your For...Next loop could be replaced with a simple...

While ADCON0.1=1:Wend

harrisondp
- 31st March 2005, 01:55
I don't think that would be a problem since the readadc sub isn't called until the 12F675 has been up for 30 seconds.

Thanks for the helpful hint on the while:wend usage. I would have never thought of that.

Harrison