Log in

View Full Version : Problems with ADC



mcbeasleyjr
- 29th January 2009, 16:57
Hell again all... seems like I get one thing fixed and something else goes wrong. I am having a problem with my ADC on the PIC16F877A. Here is the code that I am using:



DEFINE OSC 20
DEFINE ADC_BITS 10
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 1
PAUSE 1000

LIGHT VAR WORD

ADCON1=%10000010
TRISA=%11111111
TRISB=%00000000
TRISD=%00000000

MAIN:
ADCIN 1, LIGHT
LIGHT.0=PORTD.6
LIGHT.1=PORTD.7
LIGHT.2=PORTB.0
LIGHT.3=PORTB.1
LIGHT.4=PORTB.2
LIGHT.5=PORTB.3
LIGHT.6=PORTB.4
LIGHT.7=PORTB.5
LIGHT.8=PORTB.6
LIGHT.9=PORTB.7
GOTO MAIN
END


This measures the voltage coming from a CDS Photocell circuit. I have Vcc to photocell to 10k resistor to ground. ADC is connected between the photocell and 10k resistor, so as the resistance of the photocell changes, as does the voltage. I can measure this with a meter but when I try to output to light up the corresponding LEDs it doesn't work right. Any help with this will be greatly appreciated.

mister_e
- 29th January 2009, 17:03
2 things
CDS usually have high impedance, but the PIC require a max of 10K. You'll need to use a buffer in between (op-amp, transistor, FET, ...)
The way you have it now, you read PORTB and D and place it in your LIGHT variable

try


PORTB = (LIGHT >>2)
PORTD.6 = LIGHT.0
PORTD.7 = LIGHT.1

mcbeasleyjr
- 29th January 2009, 17:16
PORTB = (LIGHT >>2)
PORTD.6 = LIGHT.0
PORTD.7 = LIGHT.1

Doesn't this line just shift everything two bits to the right:


portb = (light >>2)


By doing this I'm actually losing two bits right?

mister_e
- 29th January 2009, 17:28
LIGHT is a Word, PORT is a BYTE... this is always hard to fit 16bits in a 8 bit register huh? ;)

LIGHT>>2 - yes it shift two position to the right... yes you miss bit0 and bit1, but the next lines
PORTD.6 = LIGHT.0
PORTD.7 = LIGHT.1

place them on PORTD.

no lost at all.

None of your bit are lost LIGHT>>2 as long as you dont use something like
LIGHT=LIGHT>>2

mcbeasleyjr
- 29th January 2009, 17:34
ok i was thinking that bit 0 and bit 1 would just be moved to where bits 2 and bits 3 are... thus showing bits 0 and 1 twice and losing bits 8 and 9 with the code you provided me with... i'll give it a shot and see what happens...