This repository has been archived by the owner on Jan 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Argument_Complex_Multi.h
88 lines (73 loc) · 3.38 KB
/
Argument_Complex_Multi.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/****************************************************************************************************************************
Argument_Complex_Multi.h
For Arduino and Adadruit AVR 328(P) and 32u4 boards
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt
Licensed under MIT license
Now we can use these new 16 ISR-based timers, while consuming only 1 hardware Timer.
Their independently-selected, maximum interval is practically unlimited (limited only by unsigned long miliseconds)
The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers
Therefore, their executions are not blocked by bad-behaving functions / tasks.
This important feature is absolutely necessary for mission-critical tasks.
Notes:
Special design is necessary to share data between interrupt code and the rest of your program.
Variables usually need to be "volatile" types. Volatile tells the compiler to avoid optimizations that assume
variable can not spontaneously change. Because your function may change variables while your program is using them,
the compiler needs this hint. But volatile alone is often not enough.
When accessing shared variables, usually interrupts must be disabled. Even with volatile,
if the interrupt changes a multi-byte variable between a sequence of instructions, it can be read incorrectly.
If your data is multiple variables, such as an array and a count, usually interrupts need to be disabled
or the entire sequence of your code which accesses the data.
*****************************************************************************************************************************/
// To demonstrate the usage of complex multiple files to avoid `multi definition linker` error
// by using TimerInterrupt.hpp in multiple files but TimerInterrupt.h in only main file
#ifndef Argument_Complex_Multi_h
#define Argument_Complex_Multi_h
// These define's must be placed at the beginning before #include "TimerInterrupt.h"
// _TIMERINTERRUPT_LOGLEVEL_ from 0 to 4
// Don't define _TIMERINTERRUPT_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
#define TIMER_INTERRUPT_DEBUG 0
#define _TIMERINTERRUPT_LOGLEVEL_ 3
// Select just 1 TIMER to be true
#define USE_TIMER_1 true
#define USE_TIMER_2 false
// TIMER_3 Only valid for ATmega1284 and ATmega324PB (not ready in core yet)
#define USE_TIMER_3 false
// TIMER_4 Only valid for ATmega324PB, not ready in core yet
#define USE_TIMER_4 false
#if USE_TIMER_1
#define CurrentTimer ITimer1
#elif USE_TIMER_2
#define CurrentTimer ITimer2
#elif USE_TIMER_3
#define CurrentTimer ITimer3
#elif USE_TIMER_4
#define CurrentTimer ITimer4
#else
#error You must select one Timer
#endif
#if (_TIMERINTERRUPT_LOGLEVEL_ > 3)
#if (USE_TIMER_1)
#warning Using Timer1
#elif (USE_TIMER_2)
#warning Using Timer2
#elif (USE_TIMER_3)
#warning Using Timer3
#elif (USE_TIMER_4)
#warning Using Timer4
#endif
#endif
// Can be included in many files without `Multiple Definitions` Linker Error
#include "ATmega_TimerInterrupt.hpp"
#if !defined(LED_BUILTIN)
#define LED_BUILTIN 13
#endif
struct pinStruct
{
unsigned int Pin1;
unsigned int Pin2;
unsigned int Pin3;
};
#define TIMER_INTERVAL_MS 1000
void TimerHandler(unsigned int outputPinsAddress);
#endif // Argument_Complex_Multi_h