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.

ADC CONVERSION problem in pic 16f877a

Status
Not open for further replies.

amirtha

Newbie level 3
Joined
May 20, 2013
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,314
hi....
i have problem in reading ADC ....i m using pic 16f877a and compiler HITECH ANSI C compiler..i m reading 10 bit adc and dividing it by a value so that it can be loaded into the CCPR1L register and corresponding pulses are generated for converter...any one who knw about it plz tell me what is wrong in my code..

this is my coding..PWM frequency is 10kHz, oscillator freq = 8MHZ and assigned left justification


#include<htc.h>
#include<stdio.h>
#include<math.h>
__CONFIG(0x3f72);
void PWM(unsigned int duty);
unsigned int ADCread(unsigned char);
unsigned int i;
void main()
{
unsigned int r,d;
TRISA =0x0F;
ADCON0=0X41;
//ADCON1=0X44;
r=ADCread(1);
d=r/0b1010101;
PWM(d);
}
unsigned int ADCread(unsigned char channel)
{
unsigned int lb,hb,adr;
CMCON=7;
ADCON0=0X41;
ADCON1=0X44;
for(i=0;i<200;i++);
GODONE=1;
while(GODONE==1);
hb=ADRESH;
lb=ADRESL;
adr=(hb<<2)|lb;
PORTB=ADRESL;
return adr;
}
void PWM(unsigned int duty)
{
TRISC1=0;
TRISC2=0;
PORTC=0X00;
CCP1CON=0X0C;
CCP2CON=0X0C;
PR2=12;
T2CON=0X03;
TMR2=0x00;
TMR2ON=1;
while(1)
{
for (i=1;i<200;i++);
CCPR1L=duty;
CCPR2L=duty;
for (i=1;i<200;i++);
}
}
 

i tried that also...ADC is not working......
 

How did you check ADC is not working ?
by leds on PORTB ?

don't forget to put PORTB as Output
TRISB=0x00;
 

ADCON1=0X44;
must be 0x04 for left justified
or 0x84 for right justified


you don't use variable chanel for adc reading
so it means channel 0 by default
analog input on RA0 ?
 

i tried what all u mentioned ..... getting same problem..i m getting same output at the ccp1 pin even if i didnt give analog signal ....can anyone plz tell what is basic and simple adc code that working for 16f877a in HITECh ansi c complier???
 

You are not so clear ,
you wrote : ADC is not working ...
but now, it seems probleme is on PWM output.. is it ?


Try to replace the driving (ADC result) value for PWM by a fixed value, instead of ADC measurement
to only test the PWM ..

Code:
void Tempo( unsigned long L)
{
 while(L>0)
 {
   L--;
  }
}

void main()
{
unsigned int r,d;
TRISA =0x0F;
TRISB=0x00;   
//ADCON0=0X41;
//ADCON1=0X44;
//r=ADCread(1);
//d=r/0b1010101;

d=0;
PWM(d);
Tempo(200000);
d=64;
PWM(d);
Tempo(200000);
d=128;
PWM(d);
Tempo(200000);
d=192;
PWM(d);
Tempo(200000);
d=255
PWM(d);
}
 

s.... i also tried by replacing the r = ADCRead(0) with the r = 0b11111111...in that program i m getting pulses..hope there is no problem in PWM...Is there anything that have to corrected in basics..
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top