HOW TO USE ADC IN PIC18F

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

HOW TO USE ADC IN PIC18F

Postby mohdismiaswaly » Tue Jul 23, 2013 4:20 pm

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..
mohdismiaswaly
Apprentice
 
Posts: 39
Joined: Mon Nov 05, 2012 2:51 pm

Re: HOW TO USE ADC IN PIC18F

Postby shahrul » Tue Jul 23, 2013 9:19 pm

All ADCON0, ADCON1 and ADCON2 have respective function. Refer address in the datasheet.
ex.
ADCON1=0b00000111;
ADCON0=0b00000001;
ADCON2=0b10000110;
adc.jpg
User avatar
shahrul
Professional
 
Posts: 812
Joined: Sat May 16, 2009 9:54 pm
Location: Selangor

Re: HOW TO USE ADC IN PIC18F

Postby mohdismiaswaly » Wed Jul 24, 2013 11:18 am

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?
Attachments
IMG_00000025.jpg
connection of input/output
mohdismiaswaly
Apprentice
 
Posts: 39
Joined: Mon Nov 05, 2012 2:51 pm

Re: HOW TO USE ADC IN PIC18F

Postby robosang » Thu Jul 25, 2013 9:53 pm

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.
robosang
Expert
 
Posts: 1239
Joined: Wed Jun 10, 2009 5:37 pm

Re: HOW TO USE ADC IN PIC18F

Postby mohdismiaswaly » Fri Jul 26, 2013 4:59 pm

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
mohdismiaswaly
Apprentice
 
Posts: 39
Joined: Mon Nov 05, 2012 2:51 pm

Re: HOW TO USE ADC IN PIC18F

Postby shahrul » Fri Jul 26, 2013 5:12 pm

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.
User avatar
shahrul
Professional
 
Posts: 812
Joined: Sat May 16, 2009 9:54 pm
Location: Selangor

Re: HOW TO USE ADC IN PIC18F

Postby mohdismiaswaly » Sat Jul 27, 2013 10:07 am

In register ADCON0, bit <1> is for GO. Are u sure bit <2> ?

Thanks
mohdismiaswaly
Apprentice
 
Posts: 39
Joined: Mon Nov 05, 2012 2:51 pm

Re: HOW TO USE ADC IN PIC18F

Postby yonghui » Sat Jul 27, 2013 10:27 am

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.
thanks&regards,
yh
yonghui
Moderator
 
Posts: 732
Joined: Mon Sep 28, 2009 3:27 pm

Re: HOW TO USE ADC IN PIC18F

Postby yonghui » Sat Jul 27, 2013 10:28 am

In register ADCON0, bit <1> is for GO. Are u sure bit <2> ?


depending on which pic18f u use, every pic mayb different
thanks&regards,
yh
yonghui
Moderator
 
Posts: 732
Joined: Mon Sep 28, 2009 3:27 pm

Re: HOW TO USE ADC IN PIC18F

Postby shahrul » Sat Jul 27, 2013 1:05 pm

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.. :)
User avatar
shahrul
Professional
 
Posts: 812
Joined: Sat May 16, 2009 9:54 pm
Location: Selangor

Next

Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 25 guests