Files
42_EXT_03_42chips/module04/ex00 copy/main.c
2025-03-10 17:27:05 +01:00

44 lines
1.2 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "header.h"
// 14.3.3 : alternate functions on port D
// PD2 : - INT0 (External Interrupt 0 Input)
// - PCINT18 (Pin Change Interrupt 18)
// 12.4 : Interrupt Vectors in ATmega328 and ATmega328P
// - INT0 : External Interrupt Request 0
typedef enum {
DOWN,
UP
} State;
volatile uint8_t button_state = UP;
// Table 13-2 : interrupt types
#define INT0_LOW ((0 << ISC01) | (0 << ISC01))
#define INT0_LOGICAL ((0 << ISC01) | (1 << ISC01))
#define INT0_FALLING ((1 << ISC01) | (0 << ISC01))
#define INT0_RAISING ((1 << ISC01) | (1 << ISC01))
// use interruption to change led1 state when button1 is pressed
int main() {
MODE_OUTPUT(LED1);
CLEAR_ELEM(LED1);
MODE_INPUT(BUTTON1);
PULLUP_ON(BUTTON1);
SREG |= ENABLE_GLOBAL_INTERRUPT;
EIMSK = (1 << INT0); // 13.2.2 : Enable INT0 interrupt (EIMSK External Interrupt Mask Register)
EICRA = INT0_LOGICAL; // Table 13-2 : trigger type (EICRA External Interrupt Control Register A)
while (1);
}
ISR(INT0_vect) {
_delay_ms(50);
button_state = (button_state == UP) ? DOWN : UP;
if (button_state == UP) {
TOGGLE_ELEM(LED1);
}
// EIFR = (1 << INTF0); // 13.2.3 : clear flag, why ? i think it only indicates that a trigger event has occured
}