#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.