2014-01-30 20:02:32 -08:00
/*
NOTE - This file and it ' s associated functions need a CLEARER NAME
//Purpose
2017-03-22 23:19:18 -07:00
We ' re implementing a lower frequency interrupt loop to perform calculations that are needed less often , some of which depend on time having passed ( delta / time ) to be meaningful .
2014-01-30 20:02:32 -08:00
//Technical
2015-02-09 05:00:16 -08:00
Timer2 is only 8 bit so we are setting the prescaler to 128 to get the most out of it . This means that the counter increments every 0.008 ms and the overflow at 256 will be after 2.048 ms
2014-01-30 20:02:32 -08:00
Max Period = ( Prescale ) * ( 1 / Frequency ) * ( 2 ^ 8 )
( See http : //arduinomega.blogspot.com.au/2011/05/timer2-and-overflow-interrupt-lets-get.html)
2017-03-22 23:19:18 -07:00
We ' re after a 1 ms interval so we ' ll need 131 intervals to reach this ( 1 ms / 0.008 ms per tick = 125 ) .
2015-02-09 05:00:16 -08:00
Hence we will preload the timer with 131 cycles to leave 125 until overflow ( 1 ms ) .
2014-01-30 20:02:32 -08:00
*/
2015-02-14 05:11:43 -08:00
# ifndef TIMERS_H
# define TIMERS_H
2014-01-30 20:02:32 -08:00
2019-03-12 20:24:05 -07:00
volatile bool tachoAlt = false ;
2019-03-11 02:41:50 -07:00
# define TACHO_PULSE_HIGH() *tach_pin_port |= (tach_pin_mask)
2019-03-12 20:24:05 -07:00
# define TACHO_PULSE_LOW() *tach_pin_port &= ~(tach_pin_mask)
2019-03-11 02:41:50 -07:00
enum TachoOutputStatus { DEACTIVE , READY , ACTIVE } ; //The 3 statuses that the tacho output pulse can have
volatile uint8_t tachoEndTime ; //The time (in ms) that the tacho pulse needs to end at
volatile TachoOutputStatus tachoOutputFlag ;
2017-08-27 21:35:49 -07:00
volatile byte loop33ms ;
volatile byte loop66ms ;
volatile byte loop100ms ;
volatile byte loop250ms ;
2014-01-30 20:02:32 -08:00
volatile int loopSec ;
2016-11-20 21:46:13 -08:00
volatile unsigned int dwellLimit_uS ;
2017-01-24 16:10:12 -08:00
volatile uint16_t lastRPM_100ms ; //Need to record this for rpmDOT calculation
2017-08-16 18:24:26 -07:00
volatile uint16_t last250msLoopCount = 1000 ; //Set to effectively random number on startup. Just need this to be different to what mainLoopCount equals initially (Probably 0)
2014-01-30 20:02:32 -08:00
2016-10-10 04:38:40 -07:00
# if defined (CORE_TEENSY)
IntervalTimer lowResTimer ;
2017-01-18 16:31:05 -08:00
void oneMSInterval ( ) ;
2019-03-07 03:47:22 -08:00
# elif defined(CORE_STM32_OFFICIAL) || defined(CORE_STM32_GENERIC)
2017-03-22 23:19:18 -07:00
void oneMSInterval ( ) ;
2016-10-10 04:38:40 -07:00
# endif
2014-01-30 20:02:32 -08:00
void initialiseTimers ( ) ;
2015-02-14 10:03:40 -08:00
# endif // TIMERS_H