/* *
* array1 and array2 elements hold Servo pulse width values *
* ranging from 3000..9000 in 250-nsec 'ticks' representing an *
* extended pulse width range of 750-usecs to 2250-usecs *
* */
static unsigned int array1 [] = { 6000, 6000, 6000, 6000,
6000, 6000, 6000, 6000 };
static unsigned int array2 [] = { 6000, 6000, 6000, 6000,
6000, 6000, 6000, 6000 };
/*****************************************************************
* K8LH 16-Channel 74HC238 Hi-Rez 'PWM' Servo Algorithm Driver *
*****************************************************************/
#pragma interrupt isr_hi
void isr_hi ()
{ if(PIR1bits.TMR2IF)
{ PIR1bits.TMR2IF = 0; // clear TMR2 interrupt flag
/**************************************************************
* *
* setup 74HC238 address lines and our work variables for *
* the next servo channel interval during the final (10th) *
* frame of the current servo channel interval (the final *
* frame in each interval will always have a 0% duty cycle *
* and all 74HC238 outputs are 'off' so it will be safe to *
* change the address lines) *
* */
if(frame == 10) // 10 PWM frames/Servo interval
{ frame = 0; // reset PWM frame counter
select %= 8; // 8 Servo intervals/20-msec cycle
pulse1 = array1[select];
pulse2 = array2[select];
PORTB = (PORTB & 0b11111000) | select;
select++; // bump for next interval
}
frame++; // bump for next interrupt
/**************************************************************
* setup CCP1 PWM duty cycle for next 250-usec PWM 'frame' *
* */
if(pulse1 > 1000) // if pulse1 > 250-usecs
{ CCPR1L = 250; // do a 100% duty cycle frame
pulse1 -= 1000; // subtract 250-usecs
}
else // do a variable or 0% frame
{ CCP1CONbits.CCP1Y = (pulse1 & 1);
CCP1CONbits.CCP1X = (pulse1 & 2);
CCPR1L = (pulse1 >>= 2);
pulse1 = 0; // remaining frames are %0
}
/**************************************************************
* setup CCP2 PWM duty cycle for next 250-usec PWM 'frame' *
* */
if(pulse2 > 1000) // if pulse2 > 250-usecs
{ CCPR2L = 250; // do a 100% duty cycle frame
pulse2 -= 1000; // subtract 250-usecs
}
else // do a variable or 0% frame
{ CCP2CONbits.CCP2Y = (pulse1 & 1);
CCP2CONbits.CCP2X = (pulse1 & 2);
CCPR2L = (pulse2 >>= 2);
pulse2 = 0; // remaining frames are %0
}
}
}