adc in pic16f877a written in C - help needed

Status
Not open for further replies.

shivachellam

Newbie level 6
Joined
Jan 28, 2009
Messages
11
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,400
pic16 adc acquistion time

hi i tried to wrote a c code for ADC in pic16f877a at mplab but i cant succeeds. plz if any one send c code for ADC it will very helpful to me.thank u.
 

htc.h tmr1if

#include<htc.h>

#define ADC_DIR TRISA
#define DISPLAY_DIR TRISB
#define INPUT PORTA
#define OUTPUT PORTB
#define S0_dir TRISC6
#define S1_dir TRISC7
#define S0 RC6
#define S1 RC7
#define IN 0xFF
#define OUT 0x00
#define in 0b1;
#define out 0b0;
unsigned char aaaa;
void delay40us(void)
{
TMR1H=255; //TH
TMR1L=55; //TR
T1CKPS1=0; //PRESCALE BIT
T1CKPS0=0; //PRESCALE BIT
TMR1ON=1; //TRO=1
while(!TMR1IF){} //TFO
TMR1ON=0;
TMR1IF=0;
}


void display(void)
{
aaaa=ADRESH;
S0=1;
S1=0;
OUTPUT = aaaa;
delay40us();
S0 = 0;
S1 = 1;
OUTPUT = ADRESL;
delay40us();
S0 =0;
S1=0;
CCPR1L=aaaa;
CCPR2L=255-aaaa;
}


void initialise(void)
{
GIE = 0;
ADC_DIR = IN;
DISPLAY_DIR = OUT;
S0_dir = out;
S1_dir = out;
ADRESL = 0x00;
ADRESH = 0x00;
display();
pwm();
}

void main(void)
{
initialise();
while(1){
//selecting a/d result format & configuring a/d port
ADCON1 = 0b00001110; // |ADFM = 1(6 msb of adresh=0| ADFCS2|0|0 |ad port conf bits 1111 RA0 analog RA3=vref+ RA2 =vref-|
//selecting A/d input channel
CHS2 =0;
CHS1 =0;
CHS0 =0;
//selecting A/d conversion clock TAD=1.6us required since fosc= 11.0592mhz TAD=32TOSC
ADGO =0;

ADCS2 = 0;
ADCS1 =1;
ADCS0 =0;

//turn on ad module

ADON=1;
while(1){
//wait for required acquistion time
delay40us();

ADGO =1;

while(ADGO){}
ADGO = 0;
delay40us();
delay40us();
display();
delay40us();
delay40us();
}
}
}

this is code written fot hitech c compiler
 

Re: adc in pic16f877a

thank u . but in my mplab i cant open htc.h header file.wat to do . i am using mplab version 7
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…