/* FlexiTimer2.h - Using timer2 with a configurable resolution Wim Leers Based on MsTimer2 Javier Valencia History: 6/Jun/2014 - Added Teensy 3.0 & 3.1 support 16/Dec/2011 - Added Teensy/Teensy++ support (bperrybap) note: teensy uses timer4 instead of timer2 25/April/10 - Based on MsTimer2 V0.5 (from 29/May/09) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #include unsigned long FlexiTimer2::time_units; void (*FlexiTimer2::func)(); volatile unsigned long FlexiTimer2::count; volatile char FlexiTimer2::overflowing; volatile unsigned int FlexiTimer2::tcnt2; #if defined(__arm__) && defined(TEENSYDUINO) static IntervalTimer itimer; #endif void FlexiTimer2::set(unsigned long ms, void (*f)()) { FlexiTimer2::set(ms, 0.001, f); } /** * @param resolution * 0.001 implies a 1 ms (1/1000s = 0.001s = 1ms) resolution. Therefore, * 0.0005 implies a 0.5 ms (1/2000s) resolution. And so on. */ void FlexiTimer2::set(unsigned long units, double resolution, void (*f)()) { float prescaler = 0.0; if (units == 0) time_units = 1; else time_units = units; func = f; #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__) TIMSK2 &= ~(1<= 1000000UL) && (F_CPU <= 16000000UL)) { // prescaler set to 64 TCCR2B |= (1< 16Mhz, prescaler set to 128 TCCR2B |= ((1<= 1000000UL) && (F_CPU <= 16000000UL)) { // prescaler set to 64 TCCR2 |= (1< 16Mhz, prescaler set to 128 TCCR2 |= ((1<= 1000000UL) && (F_CPU <= 16000000UL)) { // prescaler set to 64 TCCR2 |= ((1< 16Mhz, prescaler set to 256 TCCR2 |= (1<= 16000000L) { TCCR4B = (1<= 8000000L) { TCCR4B = (1<= 4000000L) { TCCR4B = (1<= 2000000L) { TCCR4B = (1<= 1000000L) { TCCR4B = (1<= 500000L) { TCCR4B = (1<= time_units && !overflowing) { overflowing = 1; count = count - time_units; // subtract time_uints to catch missed overflows // set to 0 if you don't want this. (*func)(); overflowing = 0; } } #if defined (__AVR__) #if defined (__AVR_ATmega32U4__) ISR(TIMER4_OVF_vect) { #else ISR(TIMER2_OVF_vect) { #endif #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__) TCNT2 = FlexiTimer2::tcnt2; #elif defined (__AVR_ATmega128__) TCNT2 = FlexiTimer2::tcnt2; #elif defined (__AVR_ATmega8__) TCNT2 = FlexiTimer2::tcnt2; #elif defined (__AVR_ATmega32U4__) // not necessary on 32u4's high speed timer4 #endif FlexiTimer2::_overflow(); } #endif // AVR