External Interrupts on atmega2560 #447
-
I am struggling on figuring out how to enable external interrupts on a Mega 2560. I am doing the following to set up pins 18 and 19 as external interrupts: let dp: Peripherals = Peripherals::take().unwrap();
dp.EXINT.eicrb.modify(|_, w| w.isc4().bits(0x03));
dp.EXINT.eicrb.modify(|_, w| w.isc5().bits(0x03));
dp.EXINT.eimsk.modify(|r, w| {
let cur_bits: u8 = r.bits();
let new_bits = cur_bits|(1 << arduino_hal::pac::Interrupt::INT4 as u8)|(1 << arduino_hal::pac::Interrupt::INT5 as u8);
w.bits(new_bits)
}); I also have the ISRs implemented: #[interrupt(atmega2560)]
fn INT4() {
// code here
}
#[interrupt(atmega2560)]
fn INT5() {
// code here
} But when I call |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
OK, this was one of those problems where I needed to walk away from it for a while, then read the data sheet over again, and review the framework code again, and then the solution (and your problems) reveals itself. First problem is that pins 18 and 19 on the Mega 2560 are INT3 and INT2 (respectively). Once I fixed that, I got my code working: // setting up the interrupts
let dp: Peripherals = Peripherals::take().unwrap();
dp.EXINT.eicra.modify(|_, w| w.isc2().val_0x03());
dp.EXINT.eicra.modify(|_, w| w.isc3().val_0x03());
dp.EXINT.eimsk.modify(|r, w| {
let cur_bits: u8 = r.bits();
let new_bits = cur_bits|0b00001100; // INT2 and INT3
w.bits(new_bits)
}); And then my handlers: // INT3 is d18 pin
#[interrupt(atmega2560)]
fn INT3() {
interrupt::free(|cs| {
// code here
});
}
// INT2 is d19 pin
#[interrupt(atmega2560)]
fn INT2() {
interrupt::free(|cs| {
// code here
});
} Now things work as I expect them too. :-) |
Beta Was this translation helpful? Give feedback.
OK, this was one of those problems where I needed to walk away from it for a while, then read the data sheet over again, and review the framework code again, and then the solution (and your problems) reveals itself. First problem is that pins 18 and 19 on the Mega 2560 are INT3 and INT2 (respectively). Once I fixed that, I got my code working: