;
; // setup a 1000 usec PWM period with PR2 = 249 and prescaler
; // 16:1 (16 MHz clock) for 20 PWM 'frames' per 20 msec Servo
; // period and 1 usec pulse width resolution
;
; int servo = 1500; // 0..20000 in 1 usec steps
; int width = 0; // ISR work variable
; int frame = 1; // frame number 1..20
;
; void ISR()
; { PIR1bits.TMR2IF = 0; // clear Timer 2 interrupt flag
; frame--; //
; if (frame == 0) // if end of the 20 msec period
; { frame = 20; // reset for new 20 msec period
; width = servo; // reset 'width' work variable
; }
; if ((width > 1000) // if width > 1000 usecs
; { CCPR1L = 250; // do a 100% duty cycle frame
; CCP1CONbits.CCP1Y = 0; //
; CCP1CONbits.CCP1X = 0; //
; width -= 1000; // subtract 1000 usecs
; }
; else
; { CCP1CONbits.CCP1Y = (width & 1);
; CCP1CONbits.CCP1X = (width & 2);
; CCPR1L = (width >> 2); // 0..249 (0..999 usecs)
; width = 0; // force remaining frames to 0%
; }
; }
;