weg22
Joined: 08 Jul 2005 Posts: 91
|
Possible PWM Code?? |
Posted: Sat Jan 20, 2007 1:04 pm |
|
|
I found this post on the Microchip web site for generating PWM signals. Can anyone tell me how this could be written to compile with CCS?
Thanks in advance,
-weg
-----------------------------------------------------------------
Here's an untested "software only" example driver for 8 channels using PORTB. As stated earlier in the thread, this solution may exhibit a one or two instruction cycle pulse width jitter.
Your Main program simply sets values in the Pulse array, Pulse[0] through Pulse[7]. Pulse[0] is output on RB0, Pulse[1] is output on RB1, and so on through Pulse[7] which is output on RB7. Pulse[8] is the end-of-period off time (20000 usecs minus the Pulse[0] through Pulse[7] on time values) where the Servo variable (and PORTB) = '00000000'.
I'd love to hear back from anyone who might be able to test this example driver. Please remember Timer 1 prescaler should be setup for 1.0-usec 'ticks' with whatever oscillator frequency you're using.
Code: |
static unsigned char n = 0;
static unsigned int Pulse [] = { 1500, 1500, 1500, 1500,
1500, 1500, 1500, 1500,
20000 };
static unsigned char Servo = 1;
/*****************************************************************
* setup interrupt vectors *
*****************************************************************/
#pragma code high_interrupt = 0x08
void high_interrupt (void)
{ _asm goto isr_hi _endasm
}
#pragma code
/*****************************************************************
* ISR (high) *
*****************************************************************/
#pragma interrupt isr_hi
/* *
* K8LH Crazy-8 'Soft' 8-channel (Port B) Servo Controller *
* */
void isr_hi ()
{ if (PIR1bits.CCP1IF == 1) // if CCP "compare" interrupt
{ LATB = Servo; // output new Servo pulse
CCPR1H += (Pulse[n]/256); // update the "match" value
CCPR1L += (Pulse[n]%256); // for this Servo pulse
Pulse[8] -= Pulse[n]; // adjust end-of-period off time
Servo <<= 1; // and prep for next channel
PIR1bits.CCP1IF = 0; // clear CCP1 interrupt flag
if (!(n = ++n % 9)) // if end-of-period (n=0)
{ Pulse[8] = 20000; // reset the 20.0-msec period
Servo++; // and reset Servo = 1
}
}
}
|
|
|