/*****************************************************************
* K8LH Crazy-8 'Soft' 8-channel Servo Controller variables *
*****************************************************************/
static unsigned char n = 0;
static unsigned int Pulse [] = { 1500, 1500, 1500, 1500,
1500, 1500, 1500, 1500,
20000 };
static unsigned char Servo = 1;
void isr_hi ()
{
/****************************************************************
* K8LH Crazy-8 Hi-Rez Soft 8-channel (PORTB) Servo Algorithm *
****************************************************************/
if (PIR1bits.CCP1IF == 1) // if "special event trigger"
{ PIR1bits.CCP1IF = 0; // clear CCP1 interrupt flag
LATB = Servo; // output new Servo pulse and
CCPR1 = Pulse[n++]; // setup next CCP1 "match" value
Pulse[8] -= CCPR1; // adjust end-of-cycle off time
Servo <<= 1; // and prep for next channel
if (n == 9) // if end of a 20-msec cycle
{ n = 0; // reset array index
Pulse[8] = 20000; // reset the 20.0-msec period
Servo++; // reset Servo shadow to Servo 1
}
}
}
/*****************************************************************
* MAIN *
*****************************************************************/
void main (void)
{
// setup INTOSC for 8-MHz operation
OSCCON = 0b01110000;
LATB = 0; // all PORT B servo outputs '0'
TRISB = 0; // set PORT B all outputs
ADCON1 = 15; // all digital I/O (no analog)
// setup TMR1 and CCP1 module "special event trigger" interrupts
CCPR1 = 1000; // first "compare" interrupt
T1CON = 0b00010000; // pre 2, post 1, 1 usec 'ticks'
CCP1CON = 0b00001011; // "special event trigger" mode
PIR1 = 0; // clear peripheral int flags
PIR2 = 0; //
PIE1 = 0; // clear peripheral int enables
PIE2 = 0; //
PIE1bits.CCP1IE = 1; // enable CCP1 interrupts
INTCONbits.PEIE = 1; // enable peripheral interrupts
INTCONbits.GIE = 1; // enable global interrupts
T1CONbits.TMR1ON = 1; // turn on Timer 1
while(1)
{
}
}