On Fri, Sep 9, 2011 at 12:07 PM, Bob Blick wrote: > Let's say you want to drive 8 servos and you can use all of PORTB to do > it, and that CCP1 output is on RC2. And let's also use a two-input AND > gate for each servo. One input for each AND gate goes to RC2, and the > other inputs each get one of PORTB's pins. Start with all PORTB low. > > Timer interrupt happens, you clear PORTB. Pause TMR1. Add your servo > pulse value for servo #0 and TMR1 value together and put in CCPR1. Do > what it takes to set RC2 high and enable CCP1 to clear it without > glitching. Set RB0 to enable servo #0 and unpause TMR1. Return from > interrupt. > > The next time the interrupt happens, do the same thing for servo #1 and > RB1. Cycle through until all servos have been serviced. > > So you get exact cycle times regardless of servo positions, the servos > are updated round-robin so your power supply is happiest, you get the > precision of .5 uS for your servo pulses. > > I have done it in the past with only three PORTB pins and the CCP > output. Instead of eight AND gates you use a 3 line to 8 line decoder > like the 74HC138. Unfortunately the output is inverted so you need a > stack of inverters. > > Instead of using AND gates you can use a resistor and diode per pin, but > it's uncool unless you are desperate, and it loads the CCP output really > hard. Ah, an excellent explanation, thank you Bob. One question though. Once you've done the servo #0, what triggers the start of servo #1? My first though would be to have the CCP interrupt as well as having it clear the pin. Then the pin would get cleared with no jitter, and you'd end up in the ISR soon after to service the next servo. Today I wrote some code and got the servo working using two timers, one generating the 20mS period and the other turning off the output at the appointed time. Worked ok, but I got a bit of jitter. So I decided to try using the CCP, still clocked off a separate timer. I did realize that on my current test board I don't have access to the CCP pin, so I'll have to wait for my boards to come in to try actually using the automatic pin clearing. I figured I'd try using the CCP as a trigger to clear the pin in the ISR though. Didn't work. After quite a while of going over and over what I'd done, I had a look at the errata for the chip I'm using (18F1320). Turns out that the way the datasheet says to set T3CON to indicate that TMR3 clocks the CCP module is a reserved state and doesn't work. Nice. Fixed that and it works. Still some jitter, which is understandable. Next up I will try to move it all to one timer, just like Bob describes. Thanks! Josh --=20 A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools. =A0 =A0 =A0 =A0 -Douglas Adams --=20 http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .