PDA

View Full Version : 16F819 ADC 8bit=127, Not 255?Help!



Accelerator
- 18th June 2006, 06:57
Hi ladies and gentlemen,

I'm very new to this forum, as well as the PIC world, and I have a simple code below to test my PIC16F819 's ADC function. I am trying to set the ADC as a 8-bit converter, however, it turned out that the value only goes up to 127, not 255 (if you refer to the code below, the value of "counter" only displayed up to 4, instead of 9, and my servo only turned half way...). By the way, my analog test input for PORTA.0 is from 0v to 5v.

I'm stuck and I have no clue of what I have done wrong, would anyone please help me out?!

Thanks in advance!

regards,
Accelerator

================================================== ===
@ DEVICE PIC16F819, INTRC_OSC_NOCLKOUT, WDT_OFF, LVP_OFF, PWRT_ON, PROTECT_OFF, BOD_OFF

include "modedefs.bas"

DEFINE OSC 8 'set the clock to 8 Mhz
OSCCON = $70 'set the clock to 8 Mhz, 60 for 4MHz
'set up the analog to digital converters
DEFINE ADC_BITS 8 'set # of bits
DEFINE ADC_CLOCK 1 'set clock source(1=internal 8MHz, 3=rc)
DEFINE ADC_SAMPLEUS 50 'set sampling time in ms
ADCON1 = 0 'set PORTA pins to analog
TRISA = %00000001
TRISB = %00000000

COUNTER var PORTA
LED var PORTB.7
POTENTIAL var byte

high led

SENSOR01:

ADCIN 0,potential

If potential>0 and potential<26 then counter = 0
If potential=>26 and potential<51 then counter = 1
If potential=>51 and potential<76 then counter = 2
If potential=>76 and potential<101 then counter = 3
If potential=>101 and potential<126 then counter = 4
If potential=>126 and potential<151 then counter = 5
If potential=>151 and potential<176 then counter = 6
If potential=>176 and potential<201 then counter = 7
If potential=>201 and potential<226 then counter = 8
If potential=>226 and potential<255 then counter = 9

pulsout PORTB.0, potential

goto sensor01
================================================== ===

Acetronics2
- 18th June 2006, 09:33
a little ADC input circuitry scheme ... please.

Thanks

Alain

Bruce
- 18th June 2006, 16:12
A couple things to watch out for.

1.
Place ADCON1 = %01001110 in the init section of your code. Now only RA0
is configured as an analog input, and the rest can be used for digital I/O.

Some pins you're trying to use as digital outputs are still configired as analog
inputs. ADCON1 = 0 sets them "all" to analog inputs.

2.
You need a different A/D conversion clock if you're running at 8MHz. With
ADCON1.6 = 1 this sets the conversion clock to 16 which is what you need
for any osc over 5MHz up to 10MHz MAX. See the data sheet table Tad vs.
max device operating frequency.

3.
If you're using RA0 for the A/D input, and RA5 is input only, you're never
going to see from 0 to 255 output on your LED's on porta. You could use all
of portb for the LED's and output your pulse on one of the porta pins like RA1,
2,3,4,6 or 7.

4.
PULSOUT toggles the pin twice, so the initial state of the pin used determines
the polarity of your pulse. If you want a high-going pulse, then clear the pin
you're using first. Just above TRISB = %00000000 place PORTB = %00000000.

PULSOUT resolution at 8MHz is 5uS. Assuming your A/D reading is from 0-255,
your MAX servo pulse can only be 5uS x 255 = 1.275mS.

You could use pulsout PORTB.0, potential*2 to compensate. That increases
your pulse out to ((255*2)*5uS) = 2.55mS MAX. Your servo should move full
range in both directions. You may want to add a short delay just after your
PULSOUT to get somewhere between 50-60Hz update rates.

BigWumpus
- 19th June 2006, 11:00
1. mistake:
You defined the variable COUNTER on PARTA. Why ?
If you read this 8 bits back, they represent the digital attached signals from the pins.
You should define it as a simple variable (Var byte).

2....:
you write to Counter, but never read it back ?

3....:
maybe you can throw away the if-thens by using counter=Potenzial*10/256 with some other steps

Accelerator
- 21st June 2006, 01:13
Thank you very much for all the comments!!!! I do appreciate them! You guys are masters!!

I have not tried you guys advises yet, I have been busy these couple days. But I will surely try them out once I free up myself!

To reply the questions #2 from BigWumpus, I connected my PORTA.1~4 to a single digit display through a decoder; and since its just a display, I don't need to read it back. I do like your #3 suggestion though! Thanks a lot!



1. mistake:
You defined the variable COUNTER on PARTA. Why ?
If you read this 8 bits back, they represent the digital attached signals from the pins.
You should define it as a simple variable (Var byte).

2....:
you write to Counter, but never read it back ?

3....:
maybe you can throw away the if-thens by using counter=Potenzial*10/256 with some other steps

Accelerator
- 24th June 2006, 17:34
I finally have time to work on my code, and YES! I got it work!!

There are few things I changed:
1. For the single digit display, I changed my output from PORTA, to PORTB.0~3. Since then, it displayed numbers from 0-9!!!

2. For the servo, I changed the PULSOUT signal from potential to potential*2, because, the full motion range 0-255 is for 4MHz, but I'm using 8MHz indeed.

Thank God!!!