This commit is contained in:
Lea 2023-11-17 09:36:44 +01:00
parent 7e5a1470aa
commit ec293656e2
Signed by: Lea
GPG key ID: 1BAFFE8347019C42

53
main.c
View file

@ -6,32 +6,43 @@
#include <stdbool.h>
#include <avr/interrupt.h>
int tick_timer_start = 0xffff - (F_CPU / 1024); // 1 second
volatile int debounce_int2 = 10;
volatile int debounce_int3 = 10;
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(INT2_vect) {
if (debounce_int2) return;
debounce_int2 = 100;
OCR0A++;
}
// 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;
ISR(INT3_vect) {
if (debounce_int3) return;
debounce_int3 = 100;
OCR0A--;
}
ISR(TIMER0_COMPA_vect) {
PORTF ^= 0b00000001;
}
void main() {
DDRB = 0b10000000;
DDRF = 0b00000001;
DDRD = 0b00000000;
PORTD |= 0b00001100;
start_timer();
while(1);
// EIMSK controls which ports can trigger interrupts
EIMSK |= _BV(INT2) | _BV(INT3);
// Trigger on falling edge on INT2 and INT3
EICRA |= _BV(ISC21) | _BV(ISC31);
TCCR0B |= _BV(CS00) | _BV(CS02); // clk/1024
TIMSK0 |= _BV(OCIE0A);
OCR0A = 0;
sei();
while(1) {
_delay_ms(10);
OCR0A++;
}
}