modifing millis() to run on timer2 and free timer0

Hi,

on preparing my first arduino-project (previous avr experience - live seems to be much easier with arduinos now :slight_smile: ), I'm currently stucked at the timers. Using the search functions of the forum, google, etc. I wasn't able find answers to the following questions:

  1. Is the timer definition limited to the wiring.c, or are the registers programmed at any additional location?
  2. Is it possible to redirect the millis(), micros() and delay() function to run on timer2 by changing the wiring.c and free timer0 for own usage? Or better writing a library version e.g. named millis2() using timer2. Both are 8bit, registers are comparable.
  3. Do I mess up anything else if I do this and use timer0 for my own project?

Background:
I want to use a couple of sensors logging to a sd-card with approx. 80Hz. This is due to the latency not possible in a direct approach. Therefore I want to place the sensor operations in an interupt appending the new data to a buffer and the main routine is writing and clearing the buffer to the sd as fast as possible.
To prevent mess up with the also used servo lib (timer1) it is required to place the sensor operations in a lower priority interupt, which leaves only timer0 to be used for this job. But millis() is also used so ... millis() needs to be on timer2, servo on timer1 and the sensors on timer0.

I hope you can give me some hints on the questions above.

Matthias

no one had an answer for me :frowning: well anyway I'm just trying it.

In case someone else has the same problem please find attached an updated version of the wiring.c placed in \Arduino\hardware\arduino\cores\arduino
by defining #define useTimer2onMillis the modified wiring.c is redirecting millis, micros, etc. to timer 2 and sets timer 2 to fast pwm-mode for correct speed. On the tests this seams to work well. However I'm still not sure wether anything else is messed up besides tone() on the arduino.
Also at the moment the #define needs to be in the wiring.c it self. If I place it in the sketch itself it is ignored. Anyone has an idea?

wiring.c (10.4 KB)

There is probably some way to conditionalise by defining either a new board or new variant,
but the most backwards compatible structure is to provide a timer2 library with an
option to turn off timer0 overflow interrupts (disabling millis/delay/micros), but provide
millis2, delay2, micros2 using timer2. If you want a saner life I'd set timer2 to overflow
on 249 so its period is exactly 1ms,