I have some demo code for an RTC interrupt which I'm running on an ATmega1284p MCU. The interrupt pin I'm using is PB2 which is mapped to D10 which is INT2 (I think). If I update the code to use attachInterrupt(2, INT_ISR, FALLING); rather than attachInterrupt(0, INT_ISR, FALLING); all works as expected. But I don't understand why I haven't had to change the Initialize code below which I think points to PORT D to set a pin ready rather than PORT B? May I ask someone to explain what this initialize is doing and how I might need to change it to be correct?
Simply, these commands set pin D2 (INT0) on the 328p to HIGH and OUTPUT. PORTD sets Pin 2 HIGH which is bit 3 in the register: B00000100 or 0x04. DDRD sets Pin 2 to OUTPUT which is also bit 3 in the register: B00000100 or 0x04.
I could change this to fit my hardware which has INT2 on PB2 by using the code below, as its just the PORT and not the PIN number that changes so the 0x04 value remains the same. Alternatively, a more readable version of the code would be at the bottom, athough I don't understand why setting the pin HIGH comes before setting the pin as an OUTPUT. I think this is correct but please correct me if I've got this wrong? Ta
PS I think setting HIGH before OUTPUT sets the PIN to INPUT with PULLUP enabled?
You're close. The first one sets the pin HIGH. The second one sets bit 4 in the DDRD register to 0. So it's making the pin an INPUT. So this is the equivalent of setting INPUT_PULLUP in pinMode.
Since pins default to input, you don't have to explicitly make them inputs. So your code was working before without that. You may or may not actually need the pull-up.
Without those lines the only difference was that the pin didn't have the pull-up enabled.
Thanks for confirming/explaining, much appreciated - good to have learnt something..... as always. One more question, would the MCU consume more power with or without the pullup enabled? Ta