You might also consider using the PWM module along with a small interrupt driven "helper" to link together several smaller PWM period "frames" into the much longer 120-Hz PWM period. This method produces a relatively low overhead high resolution jitter free PWM output.
I don't have PBP but perhaps you and other Forum members can muddle through this pseudo C code example.
This example uses a 12-MHz clock (Tcy = 0.3334-usecs) with PWM module settings that produce a PWM period of 333.3334-usecs (250 x 1.3334-usec 'steps'). The interrupt "helper" links together 25 of these short 333.3334-usec PWM period "frames" to produce a precise 120-Hz (8333.3334-usec) period with an output pulse width of 0 to 6250 x 1.3334-usec "steps".
Regards, Mike
Code:
;******************************************************************
;
; // setup PWM for a 333.3 usec period (12-MHz xtal, Timer 2
; // prescale 4:1, PR2 = 249) and use 25 of these 333.3 usec
; // PWM "frames" for a precise 120-Hz (8.3334 msec) period.
;
; char frame = 1; // initialize "frame" var'
; int width = 100; // 0..6250 (1.333 usec steps)
; int pulse; // isr work variable, 0..6250
;
; void interrupt() // 120-Hz example
; { pir1.TMR2IF = 0; // clear timer 2 interrupt flag
; frame--; // decrement 'frame' number
; if(frame == 0) // if end-of-period
; { frame = 25; // reset for new period
; pulse = width; // refresh work variable
; }
; if(pulse > 250) //
; { ccpr1l = 250; // do a 100% duty cycle frame
; pulse -= 250; // update 'pulse'
; }
; else //
; { ccpr1l = pulse; // do variable duty cycle
; pulse = 0; // force remaining frames to 0%
; }
;
;
;
; void Main() //
; { cmcon0 = 7; // comparators off
; ansel = 0; // a2d convertor off
; pir1 = 0; // clear all interrupt flags
; pie1.TMR2IE = 1; // enable TMR2 interrupts
; ccpr1l = 0; // set 0% duty cycle (output off)
; t2con = 0b00000101; // '0-------' unimplemented bit
; // '-0000---' TOUTPS<3:0>, postscale 1
; // '-----1--' TMR2ON, turn Timer 2 on
; // '------01' T2CKPS<1:0>, prescale 4
; pr2 = 250-1; // 250 x 1333-nsec 'ticks' = 333 usecs
; intcon = 0b11000000; // '1-------' GIE, enable global ints
; // '-1------' PEIE, enable peripheral ints
; // '--0-----' T0IE, TMR0 ints disabled
; // '----0---' GPIE, IOC disabled
; // '-----000' T0IF/INTF/GPIF flags
; while(1) //
; { //
; } //
; }
;
Bookmarks