An open API service indexing awesome lists of open source software.

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

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 setup

INIT_TIMER_INTERRUPT(TIMER_INTERRUPT_MS);
}

// =============================================================================
void loop()
{
// The rest of the logic
}