Skip to main content
1 of 2
Nick Alexeev
  • 38.6k
  • 17
  • 101
  • 240

Timer ISR with variable period is fairly easy to achieve.

Variant 1. Timers usually have a countdown register, which can be updated by the code. The countdown can be updated inside the ISR itself on a case by case basis. A state machine can be used to decide which value to the counter.

Variant 2. Another option is to set up the timer for the shortest duration then count interrupts if you need longer duration. For example, you need 5ms, 10ms, 1s. Set the timer for 5ms, and increment some counter in the ISR every time it's called. Also, set the threshold. For 10ms the threshold would be 2, for 1s the threshold would be 200. Once the counter is equal to the threshold, perform the action that you need to perform at that interval. A good thing about this approach is that you can have one time and one ISR with multiple counters and multiple thresholds. Basically, the variable timing would be implemented in the ISR code, rather than in the hardware timer.

Nick Alexeev
  • 38.6k
  • 17
  • 101
  • 240