Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
I think I would go with mikes idea use interrupts it wouldn't be much deference then pwm It would run in back ground.You could do it with interrupts so it appears to require no software support.
Mike.
// setup a 1000-usec PWM period using PR2 = 249 and prescaler
// 4 (4 MHz) or 16 (16 MHz) for 4 usec pulse width resolution
// and combine 40 of these 1000-usec "frames" for an overall
// 40-msec (25-Hz) PWM period
int dcy = 5000; // 0..10000 (4-usec ticks)
int pulse = 0; // ISR work variable
int frame = 1; // ISR frame number, 1..40
void DutyCycle(char percent)
{ dcy = percent * 100; // 0..100% -> 0..10000
}
void interrupt()
{ if(pir1.TMR2IF) // if Timer 2 interrupt
{ pir1.TMR2IF = 0; // clear Timer 2 interrupt flag
frame--; //
if(frame == 0) // if end-of-period
{ frame = 40; // reset for new 40 msec period
pulse = dcy; // reset 'pulse' work variable
}
if(pulse > 250) // if pulse > 1000-usecs
{ CCPR1L = 250; // do a 100% duty cycle frame
pulse -= 250; // subtract 1000-usecs
}
else // do a variable or 0% frame
{ CCPR1L = pulse; // 0..1000 usecs
pulse = 0; // force remaining frames to %0
}
}
}