microcontrolling/main.c
2023-11-03 09:37:39 +01:00

38 lines
782 B
C

#define __AVR_ATmega2560__
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <stdbool.h>
#include <avr/interrupt.h>
int tick_timer_start = 0xffff - (F_CPU / 1024); // 1 second
void start_timer() {
TCCR1B |= _BV(CS10) | _BV(CS12); // clk/1024
// Set the TOIE (Timer Overflow Interrupt Enable) bit
// on TIMSK1 (Timer 1 Interrupt Mask Register).
TIMSK1 |= _BV(TOIE1);
// Sets the current timer value
TCNT1 = tick_timer_start;
// Enable interrupts
sei();
}
// ISR is used to handle interrupts. TIMER1_OVF_vect
// is triggered whenever timer 1 (16 bit) overflows.
ISR(TIMER1_OVF_vect) {
TCNT1 = tick_timer_start;
PORTB ^= 0b10000000;
}
void main() {
DDRB = 0b10000000;
start_timer();
while(1);
}