├── FlexiTimer2.cpp ├── FlexiTimer2.h ├── README.md ├── examples └── FlashLed │ └── FlashLed.ino ├── keywords.txt ├── library.json └── library.properties /FlexiTimer2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | FlexiTimer2.h - Using timer2 with a configurable resolution 3 | Wim Leers 4 | 5 | Based on MsTimer2 6 | Javier Valencia 7 | 8 | History: 9 | 6/Jun/2014 - Added Teensy 3.0 & 3.1 support 10 | 16/Dec/2011 - Added Teensy/Teensy++ support (bperrybap) 11 | note: teensy uses timer4 instead of timer2 12 | 25/April/10 - Based on MsTimer2 V0.5 (from 29/May/09) 13 | 14 | This library is free software; you can redistribute it and/or 15 | modify it under the terms of the GNU Lesser General Public 16 | License as published by the Free Software Foundation; either 17 | version 2.1 of the License, or (at your option) any later version. 18 | 19 | This library is distributed in the hope that it will be useful, 20 | but WITHOUT ANY WARRANTY; without even the implied warranty of 21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 22 | Lesser General Public License for more details. 23 | 24 | You should have received a copy of the GNU Lesser General Public 25 | License along with this library; if not, write to the Free Software 26 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 27 | */ 28 | 29 | #include 30 | 31 | unsigned long FlexiTimer2::time_units; 32 | void (*FlexiTimer2::func)(); 33 | volatile unsigned long FlexiTimer2::count; 34 | volatile char FlexiTimer2::overflowing; 35 | volatile unsigned int FlexiTimer2::tcnt2; 36 | #if defined(__arm__) && defined(TEENSYDUINO) 37 | static IntervalTimer itimer; 38 | #endif 39 | 40 | void FlexiTimer2::set(unsigned long ms, void (*f)()) { 41 | FlexiTimer2::set(ms, 0.001, f); 42 | } 43 | 44 | 45 | /** 46 | * @param resolution 47 | * 0.001 implies a 1 ms (1/1000s = 0.001s = 1ms) resolution. Therefore, 48 | * 0.0005 implies a 0.5 ms (1/2000s) resolution. And so on. 49 | */ 50 | void FlexiTimer2::set(unsigned long units, double resolution, void (*f)()) { 51 | float prescaler = 0.0; 52 | 53 | if (units == 0) 54 | time_units = 1; 55 | else 56 | time_units = units; 57 | 58 | func = f; 59 | 60 | #if defined (__AVR_ATmega168__) || defined (__AVR_ATmega48__) || defined (__AVR_ATmega88__) || defined (__AVR_ATmega328P__) || defined (__AVR_ATmega1280__) || defined (__AVR_ATmega2560__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__) 61 | TIMSK2 &= ~(1<= 1000000UL) && (F_CPU <= 16000000UL)) { // prescaler set to 64 68 | TCCR2B |= (1< 16Mhz, prescaler set to 128 76 | TCCR2B |= ((1<= 1000000UL) && (F_CPU <= 16000000UL)) { // prescaler set to 64 87 | TCCR2 |= (1< 16Mhz, prescaler set to 128 95 | TCCR2 |= ((1<= 1000000UL) && (F_CPU <= 16000000UL)) { // prescaler set to 64 105 | TCCR2 |= ((1< 16Mhz, prescaler set to 256 113 | TCCR2 |= (1<= 16000000L) { 124 | TCCR4B = (1<= 8000000L) { 127 | TCCR4B = (1<= 4000000L) { 130 | TCCR4B = (1<= 2000000L) { 133 | TCCR4B = (1<= 1000000L) { 136 | TCCR4B = (1<= 500000L) { 139 | TCCR4B = (1<= time_units && !overflowing) { 199 | overflowing = 1; 200 | count = count - time_units; // subtract time_uints to catch missed overflows 201 | // set to 0 if you don't want this. 202 | (*func)(); 203 | overflowing = 0; 204 | } 205 | } 206 | 207 | #if defined (__AVR__) 208 | #if defined (__AVR_ATmega32U4__) 209 | ISR(TIMER4_OVF_vect) { 210 | #else 211 | ISR(TIMER2_OVF_vect) { 212 | #endif 213 | #if defined (__AVR_ATmega168__) || defined (__AVR_ATmega48__) || defined (__AVR_ATmega88__) || defined (__AVR_ATmega328P__) || defined (__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__) 214 | TCNT2 = FlexiTimer2::tcnt2; 215 | #elif defined (__AVR_ATmega128__) 216 | TCNT2 = FlexiTimer2::tcnt2; 217 | #elif defined (__AVR_ATmega8__) 218 | TCNT2 = FlexiTimer2::tcnt2; 219 | #elif defined (__AVR_ATmega32U4__) 220 | // not necessary on 32u4's high speed timer4 221 | #endif 222 | FlexiTimer2::_overflow(); 223 | } 224 | #endif // AVR 225 | -------------------------------------------------------------------------------- /FlexiTimer2.h: -------------------------------------------------------------------------------- 1 | #ifndef FlexiTimer2_h 2 | #define FlexiTimer2_h 3 | 4 | #ifdef __AVR__ 5 | #include 6 | #elif defined(__arm__) && defined(TEENSYDUINO) 7 | #include 8 | #else 9 | #error FlexiTimer2 library only works on AVR architecture 10 | #endif 11 | 12 | 13 | namespace FlexiTimer2 { 14 | extern unsigned long time_units; 15 | extern void (*func)(); 16 | extern volatile unsigned long count; 17 | extern volatile char overflowing; 18 | extern volatile unsigned int tcnt2; 19 | 20 | void set(unsigned long ms, void (*f)()); 21 | void set(unsigned long units, double resolution, void (*f)()); 22 | void start(); 23 | void stop(); 24 | void _overflow(); 25 | } 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #FlexiTimer2 Library# 2 | 3 | Run a function on a configurable interval. 4 | 5 | http://playground.arduino.cc/Main/FlexiTimer2 6 | 7 | https://github.com/wimleers/flexitimer2 8 | 9 | http://www.pjrc.com/teensy/td_libs_MsTimer2.html 10 | 11 | Based on MsTimer2 originally written by Javier Valencia. 12 | 13 | Wim Leers added code which makes interval resolution configurable, 14 | -------------------------------------------------------------------------------- /examples/FlashLed/FlashLed.ino: -------------------------------------------------------------------------------- 1 | /* 2 | FlexiTimer2: 3 | Arduino library to use timer 2 with a configurable resolution. 4 | Based on MsTimer2 by Javier Valencia. It is called FlexiTimer2 because it 5 | is based on MsTimer2, but offers more flexibility, 6 | since it has a configurable timer resolution. 7 | MsTimer2 library: http://www.arduino.cc/playground/Main/MsTimer2 8 | 9 | For more details on FlexiTimer2 see: 10 | http://www.arduino.cc/playground/Main/FlexiTimer2 11 | https://github.com/wimleers/flexitimer2 12 | 13 | */ 14 | 15 | #include 16 | 17 | // Switch on LED on and off each half second 18 | 19 | #if ARDUINO >= 100 20 | const int led_pin = LED_BUILTIN; // 1.0 built in LED pin var 21 | #else 22 | const int led_pin = 13; // default to pin 13 23 | #endif 24 | 25 | void flash() 26 | { 27 | static boolean output = HIGH; 28 | 29 | digitalWrite(led_pin, output); 30 | output = !output; 31 | } 32 | 33 | void setup() 34 | { 35 | pinMode(led_pin, OUTPUT); 36 | 37 | FlexiTimer2::set(500, 1.0/1000, flash); // call every 500 1ms "ticks" 38 | // FlexiTimer2::set(500, flash); // MsTimer2 style is also supported 39 | FlexiTimer2::start(); 40 | } 41 | 42 | void loop() 43 | { 44 | } 45 | -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | FlexiTimer2 KEYWORD1 2 | set KEYWORD2 3 | start KEYWORD2 4 | stop KEYWORD2 5 | -------------------------------------------------------------------------------- /library.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "FlexiTimer2", 3 | "keywords": "timer, callback", 4 | "description": "Arduino library to use timer 2 with a configurable resolution. Based on MsTimer2 by Javier Valencia. Written for the project associated with the \"Mobile & Pervasive Computing\" course at Hasselt University in Belgium.", 5 | "authors": 6 | [ 7 | { 8 | "name": "Paul Stoffregen", 9 | "email": "paul@pjrc.com", 10 | "url": "http://www.pjrc.com", 11 | "maintainer": true 12 | }, 13 | { 14 | "name": "Javier Valencia", 15 | "email": "javiervalencia80@gmail.com" 16 | }, 17 | { 18 | "name": "Wim Leers", 19 | "email": "work@wimleers.com", 20 | "url": "http://wimleers.com/" 21 | } 22 | ], 23 | "repository": { 24 | "type": "git", 25 | "url": "https://github.com/PaulStoffregen/FlexiTimer2.git" 26 | }, 27 | "version": "1.1.0", 28 | "homepage": "https://www.pjrc.com/teensy/td_libs_MsTimer2.html", 29 | "frameworks": [ 30 | "arduino" 31 | ], 32 | "platforms": [ 33 | "atmelavr", 34 | "teensy" 35 | ], 36 | "examples": [ 37 | "examples/*/*.pde" 38 | ] 39 | } 40 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=FlexiTimer2 2 | version=1.1.0 3 | author= Wim Leers , Javier Valencia 4 | maintainer=Paul Stoffregen 5 | sentence=Arduino library to use timer 2 with a configurable resolution. 6 | paragraph=Based on MsTimer2 by Javier Valencia, but with a configurable timer resolution. Written for a project associated with the "Mobile & Pervasive Computing" course at Hasselt University in Belgium. 7 | category=Timing 8 | url=https://github.com/PaulStoffregen/FlexiTimer2 9 | architectures=avr,arm 10 | --------------------------------------------------------------------------------