PDA

View Full Version : Analog input problem



burak450
- 27th February 2009, 15:20
Hi everybody,

When I use this code i get correct results



DEFINE LCD_DREG PORTB
DEFINE LCD_DBIT 0
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 4
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 5
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
DEFINE OSC 4
DEFINE ADC_BITS 8
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 100
bilgi var byte
red var byte
green var byte
blue var byte
flag1 var byte
flag2 var byte
flag3 var byte
SYMBOL VERI=PORTA.0
ADCON1=2
TRISA=%00000001
TRISB=0
PORTB=0
TRISC=0
LCDOUT $FE,1




yap:

flag1=0
flag2=0
flag3=0

high PORTA.1
PAUSE 750
CALL ADC_OKUMA
red=bilgi
pause 5
low PORTA.1


high PORTA.2
PAUSE 750
CALL ADC_OKUMA
blue=bilgi
pause 5
low PORTA.2


high PORTA.3
PAUSE 750
CALL ADC_OKUMA
green=bilgi
pause 5
low PORTA.3



pause 7000

goto yap



ADC_OKUMA:
ADCIN VERI,bilgi
RETURN



But I cannot use porta.5 and get wrong results
Where am I wrong adcon1 settings or something else???




DEFINE LCD_DREG PORTB
DEFINE LCD_DBIT 0
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 4
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 5
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
DEFINE OSC 4
DEFINE ADC_BITS 8 'A/D çevirim sonucu kaç bit olacak
DEFINE ADC_CLOCK 3 'Clock kaynağı (3=rc)
DEFINE ADC_SAMPLEUS 100
bilgi var byte
red var byte
green var byte
blue var byte
ADCON1=2
input PORTA.5
SYMBOL VERI=PORTA.5
TRISB=0
PORTB=0
TRISC=0
LCDOUT $FE,1



yap:

high PORTD.7
PAUSE 750
ADCIN VERI,bilgi
red=bilgi
pause 5
low PORTD.7


high PORTD.6
PAUSE 750
ADCIN VERI,bilgi
blue=bilgi
pause 5
low PORTD.6


high PORTD.5
PAUSE 750
ADCIN VERI,bilgi
green=bilgi
pause 5
low PORTD.5

CALL GOSTER















pause 7000
goto yap

ADC_OKUMA:
ADCIN VERI,bilgi
RETURN

GOSTER:
LCDOUT $fe,2,#red," ",#blue," ",#green," "

RETURN

burak450
- 27th February 2009, 15:34
By the way I just tried adcon1=0 it didnt work again...How can I use porta.5 as analog input ????

mister_e
- 27th February 2009, 17:08
Knowing your PIC model would help us.

burak450
- 27th February 2009, 17:10
my pic is 16f877a

burak450
- 27th February 2009, 17:13
ı wrote this code.


What i see on lcd is the values getting bigger .Started 90,100,108,120,145

How can i use first analog five input pins in 16f877a...



DEFINE LCD_DREG PORTB
DEFINE LCD_DBIT 0
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 4
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 5
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
DEFINE OSC 4
DEFINE ADC_BITS 8
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 100
SYMBOL VERI=PORTA.5
deneme var byte
ADCON1=10000010
TRISA=%11111111
basla:
LCDOUT $FE,1
adcin veri,deneme
LCDOUT $fe,2,#deneme," "
pause 3000
goto basla

end


which setting is wrong ???

mister_e
- 27th February 2009, 17:20
1. you have to set trisa.5 to 1, make this pin as input
2. choose an ADCON1 setting that set AN4 as analog, =0 or =2 have to work
3. make sure analog comparator is disable. CMCON=7
4. use ADCIN with analog channel number, not his pin name
ADCIN 4, YourVariable

HTH

burak450
- 27th February 2009, 17:31
I wrote this but same problem continues.the value starts 111 gets bigger and bigger up to about 249-254 :(




DEFINE LCD_DREG PORTB
DEFINE LCD_DBIT 0
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 4
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 5
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
DEFINE OSC 4
DEFINE ADC_BITS 8
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 100
deneme var byte
ADCON1=2
trisa.4=1
CMCON=7
basla:
LCDOUT $FE,1
adcin 4,deneme
LCDOUT $fe,2,#deneme," "
pause 3000
goto basla

mister_e
- 27th February 2009, 17:43
try ADCON1=%11100010

burak450
- 27th February 2009, 17:57
i tried this it worked !

deneme var byte
ADCON1=0
trisa.5=1
CMCON=7
basla:
LCDOUT $FE,1
adcin 4,deneme
LCDOUT $fe,2,#deneme," "
pause 3000
goto basla


after that i tried this it didnt work
values is getting smaller now..
its so weird

bilgi var byte
red var byte
green var byte
blue var byte
ADCON1=0
trisa.5=1
trisa.1=1
trisa.2=1
trisa.3=1
trisa.0=1
CMCON=7

TRISB=0
PORTB=0
TRISC=0
TRISD=0
LCDOUT $FE,1



yap:

high PORTD.7
PAUSE 750
ADCIN 5,bilgi
red=bilgi
pause 5
low PORTD.7


high PORTD.6
PAUSE 750
ADCIN 5,bilgi
blue=bilgi
pause 5
low PORTD.6


high PORTD.5
PAUSE 750
ADCIN 5,bilgi
green=bilgi
pause 5
low PORTD.5

CALL GOSTER


pause 7000
goto yap


GOSTER:
LCDOUT $fe,2,#red," ",#blue," ",#green," "


Where am i wrong this final code???

mister_e
- 27th February 2009, 18:02
In the first you have ADCIN 4, the second you have ADCIN 5.

What is your analog source? Any available schematic ?

You want to use GOSUB instead of CALL. don't forget the RETURN as well.

burak450
- 27th February 2009, 18:11
sorry. i put code there imcomplete.there is a "return" at the end.i saw this picture .there is RA5 after RA3.thats why i used porta.5.do you have any idea why did it work with first code and didnt with second???


http://img18.imageshack.us/img18/8128/adszbby.jpg

HYETİK
- 27th February 2009, 20:16
I saw and I experience that my friend's a pin of pic16f877 was not working. Only one pin was not working:) others are works well may be u should change your pic:)

so weird:)

HYETİK
- 27th February 2009, 20:19
when my friend changed his MCU hic circuit worked well.
I was shocked when I saw it