AVR-Template/main.c

57 lines
1.5 KiB
C
Raw Normal View History

2024-03-23 19:36:08 +01:00
#include <avr/interrupt.h>
#include <avr/io.h>
#include <avr/sleep.h>
2024-04-01 20:38:35 +02:00
#include <avr/wdt.h>
2024-03-23 19:36:08 +01:00
2024-04-01 20:38:35 +02:00
#define LED_PIN PB5 // Define the pin connected to the LED
2024-03-23 19:36:08 +01:00
2024-04-01 20:38:35 +02:00
ISR(TIMER1_COMPA_vect) {
// Static variable to keep track of the number of interrupts
static uint16_t counter = 0;
2024-03-23 19:36:08 +01:00
2024-04-01 20:38:35 +02:00
PORTB ^= _BV(LED_PIN); // Toggle the LED
2024-03-23 19:36:08 +01:00
2024-04-01 20:38:35 +02:00
// Modulo magic, see below for explanation
if (++counter % 2 == 0) {
OCR1A = 15624 / 2;
} else {
OCR1A = 15624 / 16;
}
2024-03-23 19:36:08 +01:00
2024-04-01 20:38:35 +02:00
// Clear the interrupt flag
TIFR1 |= (1 << TOV1);
}
2024-03-23 19:36:08 +01:00
2024-04-01 20:38:35 +02:00
// 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)
}
2024-03-23 19:36:08 +01:00
2024-04-01 20:38:35 +02:00
int main(void) {
// Configure the LED pin as an output
DDRB |= _BV(LED_PIN);
configure_interrupt();
2024-03-23 19:36:08 +01:00
2024-04-01 20:38:35 +02:00
// Set the watchdog timer to 8 seconds
wdt_enable(WDTO_8S);
2024-03-23 19:36:08 +01:00
2024-04-01 20:38:35 +02:00
// Set the sleep mode to idle
set_sleep_mode(SLEEP_MODE_IDLE);
// Pat the dog and sleep until the next interrupt
2024-03-23 19:36:08 +01:00
while (1) {
2024-04-01 20:38:35 +02:00
wdt_reset();
sleep_mode();
2024-03-23 19:36:08 +01:00
}
2024-04-01 20:38:35 +02:00
return 0;
2024-03-23 19:36:08 +01:00
}