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 <stdbool.h>
#include <avr/interrupt.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() { ISR(INT2_vect) {
TCCR1B |= _BV(CS10) | _BV(CS12); // clk/1024 if (debounce_int2) return;
debounce_int2 = 100;
// Set the TOIE (Timer Overflow Interrupt Enable) bit OCR0A++;
// 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 ISR(INT3_vect) {
// is triggered whenever timer 1 (16 bit) overflows. if (debounce_int3) return;
ISR(TIMER1_OVF_vect) { debounce_int3 = 100;
TCNT1 = tick_timer_start; OCR0A--;
PORTB ^= 0b10000000; }
ISR(TIMER0_COMPA_vect) {
PORTF ^= 0b00000001;
} }
void main() { void main() {
DDRB = 0b10000000; DDRF = 0b00000001;
DDRD = 0b00000000;
PORTD |= 0b00001100;
start_timer(); // EIMSK controls which ports can trigger interrupts
while(1); 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++;
}
} }