Reputation: 15
I am writing code for generating continuously generated PWM with a potentiometer.The code is given below. The code is working fine, but in some scenarios, it stops working.
int rd_adc()
{
int x;
int y;
TRISB = 0;
TRISD = 0;
//trise.b1 = 1;
ADCON0 = 0x81; // Fosc/64, Channel 0, A/D is on
ADCON1 = 0xCE; // Right justified, Fosc/64, AN0 =Analog
ADCON0.GO = 1; //Start Converting
while (ADCON0.GO == 1) //wait for completion of conversion
;
PORTB = ADRESH; //Display low byte on PORT C
PORTD = ADRESL; //Display high byte on PORT D
x = ADRESH;
x = (ADRESH << 8) | (ADRESL);
y = x * 0.2492; //for 0.00488x51(for making 10 bit x value within range of 0-255
//x = x * 51;
return y;
}
void main()
{
TRISA.F0 = 1;
TRISC.F2 = 0;
PR2 = 156;
CCP1CON = 0x0C;
T2CON = 0x02;
TMR2 = 0; //clr timer reg
T2CON.TMR2ON = 1;
while(1)
{
CCPR1L = rd_adc();
PIR1.TMR2IF = 0; //clr timer flag
while (PIR1.TMR2IF == 0) // wait for end of period
;
}
}
Upvotes: 1
Views: 89