Go Down

Topic: PWM timer and Arduino CPU load (Read 660 times) previous topic - next topic

merlin_jim

Hello,

I'm working on a project that requires fast analogWrites... I'm looking into using a DAC eventually, but for now I'm just using the PWM features of the arduino.

I found that to get even mediocre performance for my app I needed to play with the timers... no big surprise there... but I'm wondering if the way I'm doing it might impact anything else?  Is there an advantage to choosing, say, DIV8 over DIV1 in terms of how much processing power the PWM timer will use?  My main concern is that if I choose DIV1 over DIV8 unnecessarily, I'll be spending 1 out of every 4 clocks tending the PWM, or something silly like that...
Code: [Select]
#define TIMER_CLK_STOP            0x00      ///< Timer Stopped
#define TIMER_CLK_DIV1            0x01      ///< Timer clocked at F_CPU
#define TIMER_CLK_DIV8            0x02      ///< Timer clocked at F_CPU/8
#define TIMER_CLK_DIV64            0x03      ///< Timer clocked at F_CPU/64
#define TIMER_CLK_DIV256      0x04      ///< Timer clocked at F_CPU/256
#define TIMER_CLK_DIV1024      0x05      ///< Timer clocked at F_CPU/1024
#define TIMER_PRESCALE_MASK     0x07    //Total guess at this value...
void setup() {

 TCCR1B = (TCCR1B & ~TIMER_PRESCALE_MASK) | TIMER_CLK_DIV1;

}


acleone

The timer isn't connected to the CPU - there's zero load.  The only time the CPU has to do anything is when you set registers, eg TCCR1A.

Go Up