// PCINT0 on pins 53 52 51 50 10 11 12 13 ( PORT B)
// PCINT2 A8 ... A15 (PORT K)
const uint8_t portBPins[] = {53, 52, 51, 50, 10, 11, 12, 13};
const uint8_t portKPins[] = {A15, A14, A13, A12, A11, A10, A9, A8};
enum ActionType : uint8_t {NONE, ACTION_PORTB, ACTION_PORTK};
volatile ActionType action = NONE;
volatile uint8_t portState;
ActionType actionCopy;
uint8_t portStateCopy;
void activatePCINT(uint8_t aPin)
{
*digitalPinToPCMSK(aPin) |= bit (digitalPinToPCMSKbit(aPin)); // enable it
PCIFR |= bit (digitalPinToPCICRbit(aPin)); // clear interrupt flag
PCICR |= bit (digitalPinToPCICRbit(aPin)); // enable interrupt
}
ISR (PCINT0_vect) {
action = ACTION_PORTB;
portState = PINB;
}
ISR (PCINT2_vect) {
action = ACTION_PORTK;
portState = PINK;
}
void printAction() {
for (int8_t i = 7; i >= 0; i--) Serial.write(bitRead(portStateCopy, i) == 0 ? '-' : '*');
Serial.println(actionCopy == ACTION_PORTB ? F("\tPB ") : F("\tPK "));
}
void setup() {
for (uint8_t aPin : portBPins) {
pinMode( aPin, INPUT_PULLUP);
activatePCINT(aPin);
}
for (uint8_t aPin : portKPins) {
pinMode( aPin, INPUT_PULLUP);
activatePCINT(aPin);
}
Serial.begin(115200);
Serial.println(F("Ready"));
}
void loop() {
switch (action) {
case NONE:
break;
case ACTION_PORTB:
case ACTION_PORTK:
// critical section
noInterrupts();
actionCopy = action;
portStateCopy = portState;
action = NONE;
interrupts();
printAction();
break;
}
}
PCINT0
PCINT2
...
...
53
50
10
13
A8
A9
A15
...