This made me remember something, The compare match clears timer1 and so the above code is wrong. The lines setting CCP1 shouldn't be += just =. I.E CCP1 = servoPos[n] etc. The Special Events Trigger mode has (unfortunately) been removed from the newer chips.
Some of the newer chips have PWM "pulse steering" which works quite nicely for servos. Unlike the "soft" servo methods, it's a hardware solution (no jitter) and since the PWM module is buffered you have plenty of time to service the PWM driver interrupt (you can turn off interrupts for short periods of time, if necessary, without affecting the servos). It's really a pretty nice hi-rez low-overhead servo solution...
As per! I was wrong, thinking PWM.... Servos use PCM … It make sense to me now... I remember a couple of years back working on JPanhault's aero endeavours… We had all sorts of servo driving software... Believe it or not, this was the one we went with..
PWM was in my head because recently we've been messing with Arduino PWM outputs....
With PICs, to use PWM for servos, the "secret" so to speak, is to have an additional prescaler allowing to output a frequency, low enough, as required by them.
That is common (not sure if with all PICs) with ECCP modules. In some of the manuals (if not all) the example on how to reach 50 Hz is detailed with numerical values.
We seem to have lost the Thread starter... kawauso
I have made several "non" interrupt servo drivers, but it's difficult to maintain perfect timing (especially in C)
Interrupts are really the only way...
You need to just grab Mikes code and load it into a pic16f877a... It should work fine..
With PICs, to use PWM for servos, the "secret" so to speak, is to have an additional prescaler allowing to output a frequency, low enough, as required by them.
That is common (not sure if with all PICs) with ECCP modules. In some of the manuals (if not all) the example on how to reach 50 Hz is detailed with numerical values.
As I said in a very early post, you cannot get the prescalers to slow to 50Hz pwm. You also need to slow the CPU clock speed from 4MHz to 500kHz. That allows hardware to control the update.
Alternatively, a servo sets with a couple 50Hz pulses and holds x
For some time if you want to set other servos and then loop to check and update each servo
As I said in a very early post, you cannot get the prescalers to slow to 50Hz pwm. You also need to slow the CPU clock speed from 4MHz to 500kHz. That allows hardware to control the update.
Alternatively, a servo sets with a couple 50Hz pulses and holds x
For some time if you want to set other servos and then loop to check and update each servo
As I said in a very early post, you cannot get the prescalers to slow to 50Hz pwm. You also need to slow the CPU clock speed from 4MHz to 500kHz. That allows hardware to control the update.
Alternatively, a servo sets with a couple 50Hz pulses and holds x
For some time if you want to set other servos and then loop to check and update each servo
As I understand hobby servos, the duty cycle doesn't matter as much as the on time so any frequency from 25 to 100Hz has worked ok for me (with some trimming).
Anyhow, 1 mSec is generally one extreme and 2 mSec is the other extreme with 1.5 mSec being neutral (midpoint).
To do this with PWM on a pic, specifically an 18F877, set parameters as follows to get 62Hz updates and 12% duty cycle (2 mSec) at 1MHz F(osc).
This technique yields about 31 steps of resolution which is good enough for most flying/driving RC applications or the little gadgets people have asked me to make. Your milage may vary.
NOTE: you may have to adjust (trim) a bit to tune for your specific servo (add/subtract a few bits on the duty cycle). Also, remember that the two least significant bits for duty cycle are bits 5&4 of CCP1CON.
Using PWM allows the microcontrol hardware to maintain position and code can be used for monitoring sensors and other overhead.
If you are a purist and want 50Hz update frequency, you'll have to drop clock F(osc) to 500kHz and you'll have to settle for 16 steps of resolution.
If you have a non-hobby servo, check the datasheet for range of high/low times and adjust PWM as needed of this level of resolution fits your needs.
Microcontroller: PIC 16F1829 Language: MPASM I am using the CCP2 module to create a servo pulse. The interrupt is called at 20-ms intervals by TMR2. ;****************************************************************************** ; INTERRUPT SERVCE ROUTINE...
Do you constantly need some kind of validation? If you think everything in life is a competition, professional help is available and happiness is possible. Keep your local suicide prevention hotline number available - the holidays are difficult for people with your symptoms.