PDA

View Full Version : AD conversion



CipiCips
- 18th May 2011, 22:56
Hello

Does anyone know what is the maximum and minimum value of variable at 8 bit AD conversion (PIC 16f877A)

For example:

pw var byte

adcin 0, pw "read AD value on pin 0 and store it at variable pw"

now I am wondering what is the max and min value of that AD
is it 0 and 255 since variable is Byte or there is some formula to count that :)

mister_e
- 18th May 2011, 23:02
Bingo! 0-255 it is.

CipiCips
- 18th May 2011, 23:49
then why this doesnt work:

I have a potentiometer on pin 0 (AD), and when I turn that pot to one end (left or right) my values of variable should be 0 or 255. And when I wrote IF function nothing happens :)

for example:

start:
ADCIN 0, pw
if pw = 0 then
Pulsout portd.2, 220
else
goto start
endif
goto start

when ever I wrote pw = 255 or pw = 0 it doesnt do anything, but when I wrote If pw > 220 then it works ????

mister_e
- 19th May 2011, 00:19
For debugging purpose I always suggest people use a LCD, ICD or use PC Terminal to see what's happen INSIDE the PIC.

Also, post your PIC model, whole code so we see your ADC DEFINES. Is the ADC result left or right justified, stuff like that.

CipiCips
- 19th May 2011, 00:28
I am using PIC 16f877A

this is the code


DEFINE ADC_BITS 8 ' Set number of bits in result
DEFINE ADC_CLOCK 3 ' Set clock source (rc = 3)
DEFINE ADC_SAMPLEUS 50
ADCON1 = 0 ' PORTA is analog
pw VAR BYTE
TRISA = 255
TRISD = 0

start:

ADCIN 0,pw

if (pw > 220) and (pw<252) then
pulsout portd.3,220
else
goto start
endif
goto start

This program works, when I move Joypad to the right motor move to the coresponding postion, I wanna know what is the right value of variable pw, when my joypad is moved al the way to the right and al the way to the left??

mister_e
- 19th May 2011, 00:37
OK, so you have the results left justified, it should work. BUT maybe you have some nifty noise on the line or something like that. Allow a certain error margin.



test 0
If pw<5 then

test 255
if pw>250 then


still stucked?!? well let's try a variant of it

Test 0
if ADRESH<5 then

test 255
if ADRESH>250 then

about now?

CipiCips
- 19th May 2011, 00:58
So this ADRESH is the adress where AD save it's value ?

what if I have two, three or four AD, is it all stored in same adress ?

mister_e
- 19th May 2011, 01:05
Yup they all have the same register.

ADCIN select the channel, perform the conversion, then read it from ADRESH/ADRESL register, then dump it to your variable.

CipiCips
- 19th May 2011, 01:09
thx Steve

I will probably have few questions, now I will contion on my work

thx