Page 1 of 2

HOW TO USE ADC IN PIC18F

PostPosted: Tue Jul 23, 2013 4:20 pm
by mohdismiaswaly
Hello,

I am going to use a sensor which will produce analog output ranging from 0V to 5V. So I need to connect the output from the sensor to pin RA0 (AN0).
What should I do in the program code? Just configure ADCON1? What about ADCON0 and ADCON2? So far I just see people use ADCON1.

I use this instruction for setting ADCON1:

MOVLW 0x0E ; since I just only use pin RA0
MOVLW ADCON1

what I want is just the result in ADRESH and ADRESL register. I don't care the value, I just want to use the value for my if/else statement later in the code.

Please, someone..

Re: HOW TO USE ADC IN PIC18F

PostPosted: Tue Jul 23, 2013 9:19 pm
by shahrul
All ADCON0, ADCON1 and ADCON2 have respective function. Refer address in the datasheet.
ex.
ADCON1=0b00000111;
ADCON0=0b00000001;
ADCON2=0b10000110;
adc.jpg

Re: HOW TO USE ADC IN PIC18F

PostPosted: Wed Jul 24, 2013 11:18 am
by mohdismiaswaly
this is how the connection of input/output

white jumper wire is connected to pin RA0 as an input
led is just to show when sensor get immersed into water (in the cup), output will give 4.5V, and I want to use that output from pin RA0 to process if/else statement.

If output sensor 4V and above, led will light up, otherwise led will turn off.

So I want to use the value of ADRESH and ADRESL register to do this.

Any help?

Re: HOW TO USE ADC IN PIC18F

PostPosted: Thu Jul 25, 2013 9:53 pm
by robosang
mohdismiaswaly WROTE:this is how the connection of input/output

white jumper wire is connected to pin RA0 as an input
led is just to show when sensor get immersed into water (in the cup), output will give 4.5V, and I want to use that output from pin RA0 to process if/else statement.

If output sensor 4V and above, led will light up, otherwise led will turn off.

So I want to use the value of ADRESH and ADRESL register to do this.

Any help?


Where is the gnd from sensor oh? How can the PIC know is 4V or 5V or any voltage with GND as reference ??

Help, you try first, then show us what you get.

Re: HOW TO USE ADC IN PIC18F

PostPosted: Fri Jul 26, 2013 4:59 pm
by mohdismiaswaly
the gnd of the sensor is black jumper there on the uv board..it is connected to power supply (the regulator chip)..

the sensor just have 3 pin (Vcc, Gnd, Output)..so i just connect the output (white jumper) to the PIC (pin RA0 I am using AN0 channel in ADC)

ADCON0 set to 00000001
ADCON1 set to 00000110
ADCON2 set to 10000110

Is that correct?
Thanks

Re: HOW TO USE ADC IN PIC18F

PostPosted: Fri Jul 26, 2013 5:12 pm
by shahrul
mohdismiaswaly WROTE:
ADCON0 set to 00000001
ADCON1 set to 00000110
ADCON2 set to 10000110

Is that correct?
Thanks

After that, start ADC conversion by set the bit GO on ADCON0<2>. Wait until conversion done and read ADRESH, ADRESL as the 10 bit result.

Re: HOW TO USE ADC IN PIC18F

PostPosted: Sat Jul 27, 2013 10:07 am
by mohdismiaswaly
In register ADCON0, bit <1> is for GO. Are u sure bit <2> ?

Thanks

Re: HOW TO USE ADC IN PIC18F

PostPosted: Sat Jul 27, 2013 10:27 am
by yonghui
u will need to connect the gnd of the sensor back to the PIC as mentioned by robosang. or else ur sensor value read by PIC will be random value since there is common gnd on sensor and PIC.

Re: HOW TO USE ADC IN PIC18F

PostPosted: Sat Jul 27, 2013 10:28 am
by yonghui
In register ADCON0, bit <1> is for GO. Are u sure bit <2> ?


depending on which pic18f u use, every pic mayb different

Re: HOW TO USE ADC IN PIC18F

PostPosted: Sat Jul 27, 2013 1:05 pm
by shahrul
mohdismiaswaly WROTE:In register ADCON0, bit <1> is for GO. Are u sure bit <2> ?

Thanks

Yes, bit <1>. I answer in hurry, not much time to spend here.. :)