Skip to content
This repository was archived by the owner on Jan 29, 2023. It is now read-only.

Commit b287fa6

Browse files
authored
v1.5.0 to add support to megaAVR
### Releases v1.5.0 1. Add support to **ATmega4809-based boards** such as **Arduino UNO WiFi Rev2, AVR_NANO_EVERY, etc.**
1 parent 69f564c commit b287fa6

File tree

16 files changed

+3620
-0
lines changed

16 files changed

+3620
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/****************************************************************************************************************************
2+
Argument_Complex.ino
3+
For Arduino megaAVR ATMEGA4809-based boards (UNO WiFi Rev2, NANO_EVERY, etc. )
4+
Written by Khoi Hoang
5+
6+
Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic
7+
Licensed under MIT license
8+
9+
Now with we can use these new 16 ISR-based timers, while consuming only 1 hwarware Timer.
10+
Their independently-selected, maximum interval is practically unlimited (limited only by unsigned long miliseconds)
11+
The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers
12+
Therefore, their executions are not blocked by bad-behaving functions / tasks.
13+
This important feature is absolutely necessary for mission-critical tasks.
14+
*****************************************************************************************************************************/
15+
16+
// These define's must be placed at the beginning before #include "TimerInterrupt_Generic.h"
17+
// _TIMERINTERRUPT_LOGLEVEL_ from 0 to 4
18+
// Don't define _TIMERINTERRUPT_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
19+
#define TIMER_INTERRUPT_DEBUG 0
20+
#define _TIMERINTERRUPT_LOGLEVEL_ 0
21+
22+
// Select USING_16MHZ == true for 16MHz to Timer TCBx => shorter timer, but better accuracy
23+
// Select USING_8MHZ == true for 8MHz to Timer TCBx => shorter timer, but better accuracy
24+
// Select USING_250KHZ == true for 250KHz to Timer TCBx => shorter timer, but better accuracy
25+
// Not select for default 250KHz to Timer TCBx => longer timer, but worse accuracy
26+
#define USING_16MHZ true
27+
#define USING_8MHZ false
28+
#define USING_250KHZ false
29+
30+
#define USE_TIMER_0 false
31+
#define USE_TIMER_1 true
32+
#define USE_TIMER_2 false
33+
#define USE_TIMER_3 false
34+
35+
#include "TimerInterrupt_Generic.h"
36+
37+
#if !defined(LED_BUILTIN)
38+
#define LED_BUILTIN 13
39+
#endif
40+
41+
struct pinStruct
42+
{
43+
unsigned int Pin1;
44+
unsigned int Pin2;
45+
unsigned int Pin3;
46+
};
47+
48+
volatile pinStruct myOutputPins = { LED_BUILTIN, A0, A1 };
49+
50+
void TimerHandler1(unsigned int outputPinsAddress)
51+
{
52+
static bool toggle1 = false;
53+
static bool started = false;
54+
55+
if (!started)
56+
{
57+
started = true;
58+
pinMode(((pinStruct *) outputPinsAddress)->Pin1, OUTPUT);
59+
pinMode(((pinStruct *) outputPinsAddress)->Pin2, INPUT_PULLUP);
60+
pinMode(((pinStruct *) outputPinsAddress)->Pin3, INPUT_PULLUP);
61+
}
62+
63+
//timer interrupt toggles pins
64+
#if (TIMER_INTERRUPT_DEBUG > 1)
65+
Serial.print("Toggle pin1 = "); Serial.println( ((pinStruct *) outputPinsAddress)->Pin1 );
66+
#endif
67+
68+
digitalWrite(((pinStruct *) outputPinsAddress)->Pin1, toggle1);
69+
70+
#if (TIMER_INTERRUPT_DEBUG > 1)
71+
Serial.print("Read pin2 A0 ("); Serial.print(((pinStruct *) outputPinsAddress)->Pin2 );
72+
Serial.print(") = ");
73+
Serial.println(digitalRead(((pinStruct *) outputPinsAddress)->Pin2) ? "HIGH" : "LOW" );
74+
75+
Serial.print("Read pin3 A1 ("); Serial.print(((pinStruct *) outputPinsAddress)->Pin3 );
76+
Serial.print(") = ");
77+
Serial.println(digitalRead(((pinStruct *) outputPinsAddress)->Pin3) ? "HIGH" : "LOW" );
78+
#endif
79+
80+
toggle1 = !toggle1;
81+
}
82+
83+
#define TIMER1_INTERVAL_MS 5000
84+
85+
void setup()
86+
{
87+
Serial.begin(115200);
88+
while (!Serial);
89+
90+
Serial.print(F("\nStarting Argument_Complex on "));
91+
Serial.println(BOARD_NAME);
92+
Serial.println(MEGA_AVR_TIMER_INTERRUPT_VERSION);
93+
94+
Serial.print(F("CPU Frequency = ")); Serial.print(F_CPU / 1000000); Serial.println(F(" MHz"));
95+
96+
Serial.print(F("TCB Clock Frequency = "));
97+
98+
#if USING_16MHZ
99+
Serial.println(F("16MHz for highest accuracy"));
100+
#elif USING_8MHZ
101+
Serial.println(F("8MHz for very high accuracy"));
102+
#else
103+
Serial.println(F("250KHz for lower accuracy but longer time"));
104+
#endif
105+
106+
// Timer0 is used for micros(), millis(), delay(), etc and can't be used
107+
// Select Timer 1-2 for UNO, 0-5 for MEGA
108+
// Timer 2 is 8-bit timer, only for higher frequency
109+
110+
ITimer1.init();
111+
112+
// Using ATmega328 used in UNO => 16MHz CPU clock ,
113+
// For 16-bit timer 1, 3, 4 and 5, set frequency from 0.2385 to some KHz
114+
// For 8-bit timer 2 (prescaler up to 1024, set frequency from 61.5Hz to some KHz
115+
116+
if (ITimer1.attachInterruptInterval(TIMER1_INTERVAL_MS, TimerHandler1, (unsigned int) &myOutputPins))
117+
{
118+
Serial.print(F("Starting ITimer1 OK, millis() = ")); Serial.println(millis());
119+
}
120+
else
121+
Serial.println(F("Can't set ITimer1. Select another freq. or timer"));
122+
}
123+
124+
void loop()
125+
{
126+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/****************************************************************************************************************************
2+
Argument_None.ino
3+
For Arduino megaAVR ATMEGA4809-based boards (UNO WiFi Rev2, NANO_EVERY, etc. )
4+
Written by Khoi Hoang
5+
6+
Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic
7+
Licensed under MIT license
8+
9+
Now with we can use these new 16 ISR-based timers, while consuming only 1 hwarware Timer.
10+
Their independently-selected, maximum interval is practically unlimited (limited only by unsigned long miliseconds)
11+
The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers
12+
Therefore, their executions are not blocked by bad-behaving functions / tasks.
13+
This important feature is absolutely necessary for mission-critical tasks.
14+
*****************************************************************************************************************************/
15+
16+
// These define's must be placed at the beginning before #include "TimerInterrupt_Generic.h"
17+
// _TIMERINTERRUPT_LOGLEVEL_ from 0 to 4
18+
// Don't define _TIMERINTERRUPT_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
19+
#define TIMER_INTERRUPT_DEBUG 0
20+
#define _TIMERINTERRUPT_LOGLEVEL_ 0
21+
22+
// Select USING_16MHZ == true for 16MHz to Timer TCBx => shorter timer, but better accuracy
23+
// Select USING_8MHZ == true for 8MHz to Timer TCBx => shorter timer, but better accuracy
24+
// Select USING_250KHZ == true for 250KHz to Timer TCBx => shorter timer, but better accuracy
25+
// Not select for default 250KHz to Timer TCBx => longer timer, but worse accuracy
26+
#define USING_16MHZ true
27+
#define USING_8MHZ false
28+
#define USING_250KHZ false
29+
30+
#define USE_TIMER_0 false
31+
#define USE_TIMER_1 true
32+
#define USE_TIMER_2 false
33+
#define USE_TIMER_3 false
34+
35+
#include "TimerInterrupt_Generic.h"
36+
37+
#define TIMER1_INTERVAL_MS 1000
38+
39+
#ifndef LED_BUILTIN
40+
#define LED_BUILTIN 13
41+
#endif
42+
43+
void TimerHandler1(void)
44+
{
45+
static bool toggle1 = false;
46+
static bool started = false;
47+
48+
if (!started)
49+
{
50+
started = true;
51+
pinMode(LED_BUILTIN, OUTPUT);
52+
}
53+
54+
//timer interrupt toggles pin LED_BUILTIN
55+
digitalWrite(LED_BUILTIN, toggle1);
56+
toggle1 = !toggle1;
57+
}
58+
59+
#if USE_TIMER_2
60+
61+
#define TIMER2_INTERVAL_MS 2000
62+
63+
void TimerHandler2(void)
64+
{
65+
static bool toggle2 = false;
66+
static bool started = false;
67+
68+
if (!started)
69+
{
70+
started = true;
71+
pinMode(A0, OUTPUT);
72+
}
73+
74+
//timer interrupt toggles outputPin
75+
digitalWrite(A0, toggle2);
76+
toggle2 = !toggle2;
77+
}
78+
#endif
79+
80+
void setup()
81+
{
82+
Serial.begin(115200);
83+
while (!Serial);
84+
85+
Serial.print(F("\nStarting Argument_None on "));
86+
Serial.println(BOARD_NAME);
87+
Serial.println(MEGA_AVR_TIMER_INTERRUPT_VERSION);
88+
Serial.println(TIMER_INTERRUPT_GENERIC_VERSION);
89+
Serial.print(F("CPU Frequency = ")); Serial.print(F_CPU / 1000000); Serial.println(F(" MHz"));
90+
91+
Serial.print(F("TCB Clock Frequency = "));
92+
93+
#if USING_16MHZ
94+
Serial.println(F("16MHz for highest accuracy"));
95+
#elif USING_8MHZ
96+
Serial.println(F("8MHz for very high accuracy"));
97+
#else
98+
Serial.println(F("250KHz for lower accuracy but longer time"));
99+
#endif
100+
101+
// Select Timer 1-2 for UNO, 0-5 for MEGA
102+
// Timer 2 is 8-bit timer, only for higher frequency
103+
ITimer1.init();
104+
105+
// Using ATmega328 used in UNO => 16MHz CPU clock ,
106+
// For 16-bit timer 1, 3, 4 and 5, set frequency from 0.2385 to some KHz
107+
// For 8-bit timer 2 (prescaler up to 1024, set frequency from 61.5Hz to some KHz
108+
109+
if (ITimer1.attachInterruptInterval(TIMER1_INTERVAL_MS, TimerHandler1))
110+
{
111+
Serial.print(F("Starting ITimer1 OK, millis() = ")); Serial.println(millis());
112+
}
113+
else
114+
Serial.println(F("Can't set ITimer1. Select another freq. or timer"));
115+
116+
#if USE_TIMER_2
117+
118+
// Select Timer 1-2 for UNO, 0-5 for MEGA
119+
// Timer 2 is 8-bit timer, only for higher frequency
120+
ITimer2.init();
121+
122+
if (ITimer2.attachInterruptInterval(TIMER2_INTERVAL_MS, TimerHandler2))
123+
{
124+
Serial.print(F("Starting ITimer2 OK, millis() = ")); Serial.println(millis());
125+
}
126+
else
127+
Serial.println(F("Can't set ITimer2. Select another freq. or timer"));
128+
129+
#endif
130+
}
131+
132+
void loop()
133+
{
134+
135+
}

0 commit comments

Comments
 (0)