MsTimer2 and FlexiTimer2 Libraries
MsTimer2, by Javier Valencia, lets you periodically run a function, by a configurable number of milliseconds.FlexiTimer2 is version of MsTimer2 by Wim Leers, which makes the interval resolution configurable, rather than being fixed at 1 millisecond steps
Download: |
Included with the Teensyduino Installer Latest MsTimer2 on Github Latest FlexiTimer2 on Github |
Hardware Requirements
TODO: table showing pins unusable as PWM: Teensy 2.0: 10 and 12. Teensy++ 2.0: 1 and 24.MsTimer2 & FlexiTimer2 work on Teensy LC & 3.0 - 3.6. Internally, IntervalTimer is used these 32 bit boards. For new projects, direct use of IntervalTimer is recommended.
Basic Usage
TODO: write thisExample Program
TODO: add thisInterrupt Context Issues
MsTimer2 and FlexiTimer2 will call your function from interrupt context. Because it will interrupt your program at any moment, special design is necessary to share data with the rest of your program.Variables usually need to be "volatile" types. Volatile tells the compiler to avoid optimizations that assume variable can not spontaneously change. Because your function may change variables while your program is using them, the compiler needs this hint. But volatile alone is often not enough.
When accessing shared variables, usually interrupts must be disabled. Even with volatile, if the interrupt changes a multi-byte variable between a sequence of instructions, it can be read incorrectly. If your data is multiple variables, such as an array and a count, usually interrupts need to be disabled for the entire sequence of your code which accesses the data.
TODO: an example.
Details
The original version of these libraries are at:New development at: https://github.com/PaulStoffregen/MsTimer2 and https://github.com/PaulStoffregen/FlexiTimer2.