Speeduino
Loading...
Searching...
No Matches
timers.h
Go to the documentation of this file.
1
2
3/*
4NOTE - This file and it's associated functions need a CLEARER NAME
5
6//Purpose
7We'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.
8
9
10//Technical
11Timer2 is only 8bit so we are setting the prescaler to 128 to get the most out of it. This means that the counter increments every 0.008ms and the overflow at 256 will be after 2.048ms
12Max Period = (Prescale)*(1/Frequency)*(2^8)
13(See arduinomega.blogspot.com.au/2011/05/timer2-and-overflow-interrupt-lets-get.html)
14
15We're after a 1ms interval so we'll need 131 intervals to reach this ( 1ms / 0.008ms per tick = 125).
16Hence we will preload the timer with 131 cycles to leave 125 until overflow (1ms).
17
18*/
19#ifndef TIMERS_H
20#define TIMERS_H
21
22#include "globals.h"
23
24#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.
25
26#if(defined(CORE_TEENSY) || defined(CORE_STM32))
27 #define TACHO_PULSE_LOW() (digitalWrite(pinTachOut, LOW))
28 #define TACHO_PULSE_HIGH() (digitalWrite(pinTachOut, HIGH))
29#else
30 #define TACHO_PULSE_HIGH() (*tach_pin_port |= (tach_pin_mask))
31 #define TACHO_PULSE_LOW() (*tach_pin_port &= ~(tach_pin_mask))
32#endif
33enum 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
34
36extern volatile bool tachoSweepEnabled;
37extern volatile uint16_t tachoSweepIncr;
38
39#define TACHO_SWEEP_TIME_MS 1500
40#define TACHO_SWEEP_RAMP_MS (TACHO_SWEEP_TIME_MS * 2 / 3)
41#define MS_PER_SEC 1000
42
43extern volatile unsigned int dwellLimit_uS;
44
45#if defined (CORE_TEENSY)
47 void oneMSInterval(void);
48#elif defined (ARDUINO_ARCH_STM32)
49 void oneMSInterval(void);
50#endif
51void initialiseTimers(void);
52
53#endif // TIMERS_H
static uint32_t rshift(uint32_t a)
Bitwise right shift - generic, unoptimized, case.
Definition bit_shifts.h:349
void oneMSInterval(void)
Definition timers.cpp:76
volatile TachoOutputStatus tachoOutputFlag
Definition timers.cpp:40
volatile uint16_t tachoSweepIncr
Definition timers.cpp:41
volatile bool tachoSweepEnabled
void initialiseTimers(void)
Definition timers.cpp:50
TachoOutputStatus
Definition timers.h:33
@ ACTIVE
Definition timers.h:33
@ READY
Definition timers.h:33
@ TACHO_INACTIVE
Definition timers.h:33
volatile unsigned int dwellLimit_uS
Definition timers.cpp:37