PWM Jitter on ATmega32

tmkupo wrote on Saturday, December 15, 2007:


I’m trying to run some servos using the PWM features of ATmega32 hardware timers, producing a 1-2ms pulse with a 1/128ms resolution.  My capture/compare interrupt code works as a stand-alone, but when I integrate it with freeRTOS, the signal jitters by about 30us which causes the servos to shake.

I’ve tried modifying the tick code to use timer2 instead of timer1, I’ve tried using naked interrupts, using enter/exit critical and also running the code as a plain hardware interrupt rather than using the interrupt to resume the task but nothing seems to work.

FreeRTOS is running the ATmega32’s 8MHz internal clock with a tick rate of 1000.  Is the interrupt latency of the ATmega32 on freeRTOS too much for use with servos, or am I doing something wrong here?



davedoors wrote on Monday, December 17, 2007:

Set the tick interrupt to use the lowest priority and the PWM interrupt to use a higher priority.  This is ok provided you dont call FreeRTOS API functions from the PWM interrupt. Some ports have a configKERNEL_INTERRUPT_PRIORITY for this purpose.

jmr1972 wrote on Monday, December 17, 2007:

Well the first thing I would do is to lower the tick rate. Are you certain that you need to swap tasks every 1ms?
Also 30us is about 240 clock cycles at 8MHz that’s, in an average bit of code, around 200 instructions (which isn’t bad at all).
If you really need to update your control loop at a 1ms rate then I would suggest that you go for a higher clock frequency (this will reduce your jitter but it won’t eliminate it).

I would put the servo speed and position update loops into ISRs and leave the RTOS tick at a much lower rate say 50ms+)