I am building a device on the AVR platform. The device will need some timing information, so I was thinking of reimplementing Arduino
millis-like functionality (though not exactly like this). However, after doing some back of the envelope calculations (partly based on this post), it started to appear to me that
millis eats up at least 5% of the CPU time on a 20Mhz processor, and proportionally more than that on a 16Mhz one:
- Each millisecond, the timer (timer0) overflows, triggers an interrupt, which increments
- An ISR should take 26 clocks for pre/post ISR routine (5
- The ISR itself should take about 21 clocks (load 32-bit value, increment it, and store it back in SRAM)
- This yields almost 50 clocks, or 50 microseconds each millisecond.
- In fact, Arduino ISR function is slower than that (as revealed by this post) because it is really careful about keeping millis as accurate as possible, which takes more CPU cycles still
I don't need
millis precision, so I am considering implementing
centis or even
decis to save processor cycles for other stuff. Is this unreasonable? Are my calculations wrong? It would seem like an odd design choice, or am I missing something?