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 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++);
}
}