#define EICRA_BASEADDR 0x69
#define EIMSK_BASEADDR 0x3D
#define EIFR_BASEADDR 0x3C
#define SREG_BASEADDR 0x5F
#define ISC00 0
#define ISC01 1
#define I 7
#define INT0 0
#define _EICRA (*(volatile uint8_t *)EICRA_BASEADDR)
#define _EIMSK (*(volatile uint8_t *)EIMSK_BASEADDR)
#define _EIFR (*(volatile uint8_t *)EIFR_BASEADDR)
#define _SREG (*(volatile uint8_t *)SREG_BASEADDR)
void EXTI_Init(void) {
// Disable global interrupts while configuring
_SREG &= ~(1 << I); // Clear I bit in SREG to disable global interrupts
// Configure INT0 for rising edge trigger
_EICRA |= (1 << ISC01) | (1 << ISC00); // Set ISC01 and ISC00 for rising edge
_EIMSK |= (1 << INT0); // Enable INT0 external interrupt
// Set PD2 (INT0) as input and enable internal pull-up if needed
DDRD &= ~(1 << PD2); // Set PD2 as input
PORTD |= (1 << PD2); // Enable internal pull-up (if button connects to GND)
// Re-enable global interrupts
_SREG |= (1 << I); // Set I bit in SREG to enable global interrupts
}
void setup() {
Serial.begin(9600);
EXTI_Init(); // Initialize EXTI once during setup
}
void loop() {
// Main code can run here
}
// ISR for INT0 interrupt
ISR(INT0_vect) {
Serial.println("Interrupt occurred on INT0");
// Clear any pending interrupt flags for INT0 (if necessary)
_EIFR |= (1 << INTF0); // Write 1 to clear INT0 flag in EIFR
}