Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

help required Pic 16f877a adc

Status
Not open for further replies.

krishnaraj916

Newbie level 6
Joined
Oct 13, 2011
Messages
12
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,384
HI I AM USING PIC16F877A TO JUST MEASURE THE VOLTAGE AT THE A1 PIN ( ANALOGUE) AND DISPLAY THE CORRESPONDING 8 BITS AS THE OUTPUT OF PORT B

but wn trying to simulate with the code below ..nothing happens...
pls help me!!

#include<htc.h>

#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //high freq osc > 200 khz
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOBROWNOUT //No brownout reset

#FUSES NOCPD //No EE protection
#FUSES NOPUT //No Power Up Timer

#use delay(clock=4000000)

void init_a2d(void)
{
ADCON0=0; // select Fosc/2
ADCON1=0x01; // select right justify result. A/D port configuration 0
ADON=1; // turn on the A2D conversion module
}

/* Return an 8 bit result */
unsigned char read_a2d(unsigned char channel)
{
channel&=0x07; // truncate channel to 3 bits
ADCON0=0xC5; // clear current channel select
ADCON0|=(channel<<3); // apply the new channel select
ADGO=1; // initiate conversion on the selected channel
while(ADGO)continue;
return(ADRESH); // return 8 MSB of the result
}

void main(void){
unsigned char x;

init_a2d(); // initialise the A2D module
GIE=0; // don't want interrupts
TRISB=0x00; // the 8 bits of POTRB will be used in output mode

while(1){
x=read_a2d(1); // sample the analog value on RA0
PORTB = x; //to give corresponding bcd
}
}
 

Hi;
You write ADCON1 as 0x01, means you configure AN0-AN7 as analog input except AN3, which is Vref+. Do you connect your reference voltage?
OR write ADCON1 as 0x00 simply to select VDD as Vref+.
 

#include<htc.h>

#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //high freq osc > 200 khz
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOBROWNOUT //No brownout reset

#FUSES NOCPD //No EE protection
#FUSES NOPUT //No Power Up Timer

#use delay(clock=4000000)

void init_a2d(void)
{
CMCON = 0X07; //DISABLE THE COMPARATORS
ADCON0=0X40; // select Fosc/8 FOR XTAL<5MHz
ADCON1=0x84; // select right justify result. A/D port configuration 4
ADON=1; // turn on the A2D conversion module
}

/* Return an 8 bit result */
unsigned char read_a2d(unsigned char channel)
{
channel&=0x07; // truncate channel to 3 bits
ADCON0&=0xC5; // clear current channel select WITHOUT EFFECTING THE OTHER BITS
ADCON0|=(channel<<3); // apply the new channel select
INSERT A 30 MICROSEC DELAY HERE
ADGO=1; // initiate conversion on the selected channel
while(ADGO)continue;
return(ADRESH); // return 8 MSB of the result
}

void main(void){
unsigned char x;

init_a2d(); // initialise the A2D module
GIE=0; // don't want interrupts
TRISB=0x00; // the 8 bits of POTRB will be used in output mode

while(1){
x=read_a2d(1); // sample the analog value on RA0
PORTB = x; //to give corresponding bcd
}
}
I have made a few changes to your code. Try it, If it still does not work then i will send you my own project.
 

@emresel : thnx ...i dinn know abt setting Vref


@waseem:THNX A LOT.. THE CODE WORKED PERFECTLY...AFTER INSERTING THE 30us delay...!! and making the changes u specified!!
bt y is it 30us and not ny othr value...?
THNX ANYWAYS
 

The reason is mentioned in the datasheet of the controller. Actually this time is called acquisition time.
There is a capacitor inside the adc module of the controller which must charge to the input voltage before the adc can start converting the voltage to a digital value. That capacitor needs a finite time to charge. Every time you switch a channel, time should be given for the capacitor to charge.
The exact time can be calculated using the formula in the data sheet. 30usec is just an approximate time which works well in most of the situations.
Regards
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top