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 )
2019-10-21 18:49:07 -07:00
( See arduinomega . blogspot . com . au / 2011 / 05 / timer2 - and - overflow - interrupt - lets - get . html )
2014-01-30 20:02:32 -08:00
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
2023-10-06 00:10:20 -07:00
# include "globals.h"
2021-11-11 20:11:42 -08:00
# define SET_COMPARE(compare, value) compare = (COMPARE_TYPE)(value) // It is important that we cast this to the actual overflow limit of the timer. The compare variables type can be bigger than the timer overflow.
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)
2022-06-26 18:48:12 -07:00
enum TachoOutputStatus { TACHO_INACTIVE , READY , ACTIVE } ; //The 3 statuses that the tacho output pulse can have. NOTE: Cannot just use 'INACTIVE' as this is already defined within the Teensy Libs
2021-10-22 15:04:31 -07:00
2023-10-06 00:10:20 -07:00
extern volatile TachoOutputStatus tachoOutputFlag ;
extern volatile bool tachoSweepEnabled ;
extern volatile uint16_t tachoSweepIncr ;
2023-10-06 14:25:26 -07:00
2023-01-10 21:27:16 -08:00
# define TACHO_SWEEP_TIME_MS 1500
# define TACHO_SWEEP_RAMP_MS (TACHO_SWEEP_TIME_MS * 2 / 3)
# define MS_PER_SEC 1000
2019-03-11 02:41:50 -07:00
2023-10-06 00:10:20 -07:00
extern volatile unsigned int dwellLimit_uS ;
2014-01-30 20:02:32 -08:00
2016-10-10 04:38:40 -07:00
# if defined (CORE_TEENSY)
2023-10-06 00:10:20 -07:00
extern IntervalTimer lowResTimer ;
2022-11-05 15:43:29 -07:00
void oneMSInterval ( void ) ;
2020-12-07 05:43:01 -08:00
# elif defined (ARDUINO_ARCH_STM32)
2022-11-05 15:43:29 -07:00
void oneMSInterval ( void ) ;
2016-10-10 04:38:40 -07:00
# endif
2022-11-05 15:43:29 -07:00
void initialiseTimers ( void ) ;
2014-01-30 20:02:32 -08:00
2015-02-14 10:03:40 -08:00
# endif // TIMERS_H