CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Possible PWM Code??

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
weg22



Joined: 08 Jul 2005
Posts: 91

View user's profile Send private message

Possible PWM Code??
PostPosted: Sat Jan 20, 2007 1:04 pm     Reply with quote

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
    }
  }
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group