skip navigational linksPJRC
Shopping Cart Checkout Shipping Cost Download Website
Home MP3 Player 8051 Tools All Projects PJRC Store Site Map
You are here: Teensy Teensyduino Libraries MsTimer2

PJRC Store
Teensy 3.1, $19.80
Teensy 2.0, $16.00
Teensy++ 2.0, $24.00
USB Cable, $4.00
Teensy
Main Page
Teensy 3.1
Getting Started
How-To Tips
Code Library
Projects
Teensyduino
Reference

MsTimer2 and FlexiTimer2 Libraries

MsTimer2, by Javier Valencia, lets you run a function every millisecond. FlexiTimer2 is version of MsTimer2 by Wim Leers, which makes the interval configurable.

Download: MsTimer2.zip
Download: FlexiTimer2.zip

Hardware Requirements

TODO: table showing pins unusable as PWM: Teensy 2.0: 10 and 12. Teensy++ 2.0: 1 and 24.

Basic Usage

TODO: write this

Example Program

TODO: add this

Interrupt 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:

MsTimer2 page.

FlexiTimer2 page.