AVR-Template/main.c
2024-03-23 19:36:08 +01:00

38 lines
973 B
C

#include <avr/interrupt.h>
#include <avr/io.h>
#include <avr/sleep.h>
#include <util/delay.h>
#define BLINK_TIME 0.5 // 1 second
ISR(TIMER1_OVF_vect) {
PORTB ^= _BV(PORTB5); // Toggle the LED
TCNT1 = 65535 - (F_CPU / 1024 * BLINK_TIME); // Reset timer value
}
int main(void) {
// Set PORTB5 as output
DDRB |= _BV(DDB5);
// Initialize TIMER1 value
TCNT1 = 65535 - (F_CPU / 1024 * BLINK_TIME);
// Set the prescaler to 1024
TCCR1B |= _BV(CS10) | _BV(CS12);
// Enable TIMER1 overflow interrupt
TIMSK1 |= _BV(TOIE1);
// Enable global interrupts
// sei();
// Enable global interrupts with style
__asm__ __volatile__ ("sei");
while (1) {
// Keep in mind that the sleep mode depth will affect the wake up time
sleep_cpu(); // Deep sleep
// set_sleep_mode(SLEEP_MODE_IDLE); // Idle sleep
// sleep_mode(); // Sets the CPU to whatever mode was set
}
}