microcontrolling/main.c

38 lines
782 B
C
Raw Permalink Normal View History

2023-08-25 07:33:25 +00:00
#define __AVR_ATmega2560__
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
2023-08-26 17:08:09 +00:00
#include <stdbool.h>
2023-08-29 08:58:36 +00:00
#include <avr/interrupt.h>
2023-08-25 07:33:25 +00:00
2023-10-20 08:58:29 +00:00
int tick_timer_start = 0xffff - (F_CPU / 1024); // 1 second
2023-08-29 08:58:36 +00:00
void start_timer() {
2023-08-29 16:52:13 +00:00
TCCR1B |= _BV(CS10) | _BV(CS12); // clk/1024
2023-08-29 08:58:36 +00:00
// 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;
2023-08-29 08:58:36 +00:00
// 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;
2023-11-03 08:37:39 +00:00
PORTB ^= 0b10000000;
2023-08-29 18:18:30 +00:00
}
2023-08-29 18:49:46 +00:00
void main() {
2023-10-20 08:58:29 +00:00
DDRB = 0b10000000;
2023-08-27 19:01:23 +00:00
2023-08-29 18:49:46 +00:00
start_timer();
2023-10-20 08:58:29 +00:00
while(1);
2023-08-27 19:01:23 +00:00
}