Author Topic: Use CCP1 PWM functionality to drive servo's  (Read 4658 times)

0 Members and 1 Guest are viewing this topic.

Offline RebelgiumTopic starter

  • Supreme Robot
  • *****
  • Posts: 637
  • Helpful? 0
  • It's called the future ... We like it here
    • orgcrime.net
Use CCP1 PWM functionality to drive servo's
« on: July 13, 2008, 03:31:35 AM »
I want to drive a servo using my the CCP1 or 2 peripheral in my PIC18F4431.
The PWM functionality of the CCP peripherals use the Timer2.

But the slowest period I can get (I think) is 70Hz. (Postscale=16, Prescale = 16, PR2 = 11111111) and a servo expects pulses every 20ms (50Hz).

- Is this a problem? or will the servo be able to coop with the 70Hz?
- Is there a way to reduce the period of Timer2 even further?

- I want to use the CCP peripheral so that I don't need to use delay functions in my program and waste processortime... Is this a good idea?

Thanks



« Last Edit: July 13, 2008, 01:43:10 PM by Rebelgium »
To relax after some hard work on robotics: A very fun free online text based MMORPG
orgcrime.net

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: Use CCP1 PWM functionality to drive servo's
« Reply #1 on: July 20, 2008, 09:01:38 AM »
Quote
But the slowest period I can get (I think) is 70Hz. (Postscale=16, Prescale = 16, PR2 = 11111111) and a servo expects pulses every 20ms (50Hz).
It doesn't have to be 20ms, thats just the Admin recommended wait. I sometimes go as low as 10ms, but I've found some servos start to overheat at that rate . . .

Quote
I want to use the CCP peripheral so that I don't need to use delay functions in my program and waste processortime... Is this a good idea?
Well, using the delay method, each servo will cause a delay on average 1.5ms. So 5 servos would cause a delay of about 7.5ms. If you send a command to a servo 10 times per second, thats 75ms, or about 7.5% of total processing time. Unless you are doing some crazy processing, thats fairly insignificant.

Your PIC probably has only 2-4 PWM channels, meaning it won't really save you any processing.

The one advantage however is that with PWM it becomes a 'set and forget about it' command. You wouldn't need to worry about sending servo commands all the time. You could also use timer interrupts to run servos as a set and forget function, too.

Offline GHF

  • Jr. Member
  • **
  • Posts: 29
  • Helpful? 0
    • GeeksHaveFeelings
Re: Use CCP1 PWM functionality to drive servo's
« Reply #2 on: July 20, 2008, 02:47:18 PM »
The CCP module should not be set to PWM mode if you want servo control. CCP's PWM mode is designed for high frequency PWM, and you won get good resolution with low-frequency servo pulses.

However, the compare mode (the second C) is more useful for servos. In compare mode, you give it a value and it will compare that value against a timer until the timer's register(s) is equal to the value you gave it. Then it raises an interrupt. See the following pseudo code:

Code: [Select]
setup {
  compare_interrupt_enable = 1;
  disable_timer();
  clear_timer_value();
  set_compare_value(0xF000); // set the desired time to be 0xF000 timer ticks
  servo_pin = 1; // turn on servo pin
  enable_timer(); // start the clock
}

interrupt_handler {
  servo_pin = 0; // the desired time has been reached
  // in your application, you would probably have another interrupt to do the setup every 20ms
}