I cant change the duty cycle of the PWM dynamically. I found this in the datasheet that I might be misinterpreting:
CCPR1L and CCP1CON<5:4> can be written to at anytime, but the duty cycle value is not copied into CCPR1H until a match between PR2 and TMR2 occurs(i.e., the period is complete). In PWM mode, CCPR1H is a read-only register.
Using PIC18F1220, XC8 v1.34, MPLAB v5.30
PR2=99; //calculation
CCPR1L = 50; //calculation*duty cycle
T2CONbits.TMR2ON = 1; //start timer
while(1)
{
ADC_Result[0] = ADC_Read(0);
ADC_Result[1] = ADC_Read(1);
if(ADC_Result[0] > 900)
{
T2CONbits.TMR2ON = 0;
CCPR1L = 0;
T2CONbits.TMR2ON = 1;
}
else
{
T2CONbits.TMR2ON = 0;
CCPR1L = 50;
T2CONbits.TMR2ON = 1;
}
PIR1bits.CCP1IF=0;
while(PIR1bits.CCP1IF==0);
}
Shouldnt offing and oning back the timer restart the period?
Side note: I know the PWM works fine. On the simulator I can get both cases to happen, just needs a restart. I have also tried without turning off and on the timer without success.
The issue, and why the reset is needed is the last while loop. in the datasheet on page 75, is shows that the CCP1F bit is unused in PWM operation. If you wanted to wait for the period to expire to run the ADC, use tmr2if bit instead.