https://github.com/stefanov-sm/arduino-timer-interrupt
Simple Arduino UNO and NANO timer interrupt support
https://github.com/stefanov-sm/arduino-timer-interrupt
Last synced: about 2 months ago
JSON representation
Simple Arduino UNO and NANO timer interrupt support
- Host: GitHub
- URL: https://github.com/stefanov-sm/arduino-timer-interrupt
- Owner: stefanov-sm
- License: mit
- Created: 2025-01-28T20:27:13.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2025-02-08T19:01:37.000Z (3 months ago)
- Last Synced: 2025-02-08T19:32:41.312Z (3 months ago)
- Language: C++
- Homepage:
- Size: 15.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Arduino timer interrupt
Frugal **`Timer1`** interrupt support for Arduino R3 NANO and R3 UNO
> [!IMPORTANT]
> Maximum interval between interrupts is **4193 ms**
### Info (PrincOps)
_____
- Set **TIMER1_COMPA_vect** interrupt vector to the correponding ISR (_Interrupt Service Routine_)
_____
- Set **WGM12** bit of register **TCCR1B** for **CTC** (_Clear Timer on Compare match_) mode
`TCCR1B |= (1 << WGM12);`
_____
- Set **CS10** and **CS12** bits of register **TCCR1B** for **prescaler = 1024**
`TCCR1B |= (1 << CS12) | (1 << CS10);`
Use `(1< [!TIP]
> Note the use of `static` variables for the slow job
```C++
#include "timer_interrupt.h"#define TIMER_INTERRUPT_MS 10
#define SLOW_GATE 11
#define A_LONG_TIME (60L*1000/TIMER_INTERRUPT_MS)
// One minute here, may be many days long// =============================================================================
// The timer interrupt service routine (ISR)
SET_TIMER_ISR({
// Do the fast job(s) here, once every 10 ms// A slow job
// Toggle SLOW_GATE pin once per minute
static unsigned long slow_counter = 0;
if (++slow_counter >= A_LONG_TIME)
{
slow_counter = 0;
static bool slow_status = false;
slow_status = !slow_status;
digitalWrite(SLOW_GATE, slow_status ? HIGH: LOW);
}
});// =============================================================================
void setup()
{
pinMode(SLOW_GATE, OUTPUT);
// The rest of the setupINIT_TIMER_INTERRUPT(TIMER_INTERRUPT_MS);
}// =============================================================================
void loop()
{
// The rest of the logic
}