HOW TO USE ADC IN PIC18F

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

Re: HOW TO USE ADC IN PIC18F

Postby mohdismiaswaly » Thu Aug 22, 2013 4:07 pm

Hello,

How to write program for this:
"After that, start ADC conversion by set the bit GO on ADCON0<1>. Wait until conversion done and read ADRESH, ADRESL as the 10 bit result"

Mind to give the program code?

I just want to read the ADRESH and ADRESL register result.

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

Re: HOW TO USE ADC IN PIC18F

Postby ober » Mon Aug 26, 2013 9:39 am

mohdismiaswaly WROTE:How to write program for this:
"After that, start ADC conversion by set the bit GO on ADCON0<1>. Wait until conversion done and read ADRESH, ADRESL as the 10 bit result"


ADCON0bits.GO = 1; //setting GO to start ADC conversion
while(ADCONbits.GO == 1) continue; //wait for ADC conversion to complete
// Read the ADRESH and ADRESL
Ober Choo
Cytron Technologies Sdn Bhd
www.cytron.com.my
User avatar
ober
Moderator
 
Posts: 1486
Joined: Wed Apr 15, 2009 1:03 pm

Re: HOW TO USE ADC IN PIC18F

Postby mohdismiaswaly » Thu Sep 12, 2013 11:53 pm

Hello everyone,

Below is the picture of my circuit connection. Humidity sensor is connected to pin RA0 (analog input) and all pin PORTD connected to 7 segment display (common anode) as output. What I want to do is to display ONLY the content of ADRESL registers to 7 segment display.

Note:
white wire=output from sensor connected to pin RA0
yellow wire=ground from sensor connected to ground to PIC
blue wire=output from PIC of PORTD pins (RD0-RD7)
red wire=Vdd (5V) from PIC to 7segment display

The problem is I write this code:

#include <htc.h>

main(void)
{
TRISC=0 ;
TRISD=0 ;
TRISAbits.TRISA0=0;
ADCON0 = 0x81 ;
ADCON1 = 0xcE ;
PORTA=0;
PORTC=0;
PORTD=0;

while(1)
{
delay_ms(1) ;
ADCON0bits.GO = 1 ;
while(ADCON0bits.DONE == 1) ;

PORTD=ADRESL;
PORTC=ADRESH;
delay_ms(3000);

}
}

delay_ms(unsigned int x)
{
for(;x>0;x--);
{
T0CON=0x80; //Fosc 4MHz, Timer 0,16-bit mode, prescaler 1:2, use internal clock, increment on positive-edge
TMR0H=0x9E;
TMR0L=0X57;
T0CONbits.TMR0ON=1;
while(INTCONbits.TMR0IF==0);
T0CONbits.TMR0ON=0;
INTCONbits.TMR0IF=0;
}
}

Anything goes wrong? I am not sure the setting for ADCON0 and ADCON1. I am also not sure about the code I highlight in red colour above. Are they all correct?
Attachments
IMG_00000140.jpg
circuit connection
mohdismiaswaly
Apprentice
 
Posts: 39
Joined: Mon Nov 05, 2012 2:51 pm

Re: HOW TO USE ADC IN PIC18F

Postby shahrul » Fri Sep 13, 2013 9:51 am

If you read adc on RA0, TRISA0 should set to 1 to become an input.
Then, I'm not checking the remaining code.
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 » Fri Sep 13, 2013 2:54 pm

So, I set TRISA0 to 1.

How about the code in red colour? Correct?
mohdismiaswaly
Apprentice
 
Posts: 39
Joined: Mon Nov 05, 2012 2:51 pm

Re: HOW TO USE ADC IN PIC18F

Postby mohdismiaswaly » Tue Sep 17, 2013 4:34 pm

Hello,

Anyone, please respond to the previous message as above.

Help needed.
:cry:
mohdismiaswaly
Apprentice
 
Posts: 39
Joined: Mon Nov 05, 2012 2:51 pm

Re: HOW TO USE ADC IN PIC18F

Postby sich » Tue Sep 17, 2013 6:29 pm

Actually what's the problem you encounter? And what have you achieved so far? What's the output of code? Please be more specific so we can assist you more effectively.
~> How to ask QUESTIONS the SMART way in FORUM? <~
User avatar
sich
Moderator
 
Posts: 603
Joined: Tue Apr 21, 2009 2:15 pm

Previous

Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 31 guests