56 lines
1.5 KiB
C
56 lines
1.5 KiB
C
#include <avr/interrupt.h>
|
|
#include <avr/io.h>
|
|
#include <avr/sleep.h>
|
|
#include <avr/wdt.h>
|
|
|
|
#define LED_PIN PB5 // Define the pin connected to the LED
|
|
|
|
ISR(TIMER1_COMPA_vect) {
|
|
// Static variable to keep track of the number of interrupts
|
|
static uint16_t counter = 0;
|
|
|
|
PORTB ^= _BV(LED_PIN); // Toggle the LED
|
|
|
|
// Modulo magic, see below for explanation
|
|
if (++counter % 2 == 0) {
|
|
OCR1A = 15624 / 2;
|
|
} else {
|
|
OCR1A = 15624 / 16;
|
|
}
|
|
|
|
// Clear the interrupt flag
|
|
TIFR1 |= (1 << TOV1);
|
|
}
|
|
|
|
// We will use Timer1 for the interrupt
|
|
void configure_interrupt() {
|
|
TCNT1 = 0; // Initialize the counter value to 0
|
|
TCCR1A = 0; // Set the timer to normal mode
|
|
TCCR1B = 0; // Set the timer to normal mode
|
|
OCR1A = 15624 / 16; // 31250 for a 2s delay, 15624 for a 1s delay
|
|
TCCR1B |= (1 << WGM12); // Set the timer to CTC mode
|
|
TCCR1B |= (1 << CS02) | (1 << CS00); // Set the prescaler to 1024
|
|
TIMSK1 |= (1 << OCIE1A); // Enable overflow interrupt
|
|
sei(); // Enable global interrupts (cli() to disable)
|
|
}
|
|
|
|
int main(void) {
|
|
// Configure the LED pin as an output
|
|
DDRB |= _BV(LED_PIN);
|
|
|
|
configure_interrupt();
|
|
|
|
// Set the watchdog timer to 8 seconds
|
|
wdt_enable(WDTO_8S);
|
|
|
|
// Set the sleep mode to idle
|
|
set_sleep_mode(SLEEP_MODE_IDLE);
|
|
|
|
// Pat the dog and sleep until the next interrupt
|
|
while (1) {
|
|
wdt_reset();
|
|
sleep_mode();
|
|
}
|
|
|
|
return 0;
|
|
}
|