I enable in sketch PCINT for interrupts and/or for wakeup MC from sleep.
interrupts are working but I want to determine which pin trigger this interrupt
and I facing with two issues on Micro
only PB5 and PB6 are return correct value. PB7 always return zero. why?
when interrupt fire when MC in sleep mode (need uncomment the following string ) then all pins PB5..PB7 return zero. why?
#include "deepSleep.h"
#ifdef USBCON
#define LED (1 << PC7)
#define led_hi() PORTC |= LED
#define led_lo() PORTC &= ~LED
#else
#define LED (1 << PB5)
#define led_hi() PORTB |= LED
#define led_lo() PORTB &= ~LED
#endif
#define PIR (1 << INT0)
int flag = 0;
int KEYVAL = 0;
int PBLAST = 0;
int PBNOW = 0;
void setup() {
DDRB = 0; PORTB = 0;
cli();
PCICR |= (1 << PCIE0); // Enables Ports B as Pin Change Interrupts
PCMSK0 |= (1 << PCINT7) | (1 << PCINT6) | (1 << PCINT5); // PCINT0
sei();
DDRD |= LED; PORTD &= ~LED;
Serial.begin(19200);
}
void loop() {
if (flag) {
flag = 0;
Serial.println(millis());
led_hi(); // turn LED ON
delay(4000);
led_lo(); // turn LED OFF if we have no motion
Serial.println(KEYVAL);
KEYVAL = 0;
PBLAST = PINB;
PCMSK0 |= (1 << PCINT7) | (1 << PCINT6) | (1 << PCINT5);
}
//gotoSleep();
delay(2000);
Serial.println(F("Up"));
}
ISR(PCINT0_vect)
{
PCMSK0 = 0;
++flag;
PBNOW = PINB ^ PBLAST;
switch (PBNOW) {
case (1 << PB7):
KEYVAL = 11;
break;
case (1 << PB6):
KEYVAL = 10;
break;
case (1 << PB5):
KEYVAL = 9;
break;
default:
break;
}
}
inline void gotoSleep() {
Serial.println(F("goto"));
delay(2000);
Serial.println(F("s"));
delay(200);
Serial.println(F("l"));
delay(200);
Serial.println(F("e"));
delay(200);
Serial.println(F("e"));
delay(200);
Serial.println(F("p"));
delay(200);
deepSleep();
}
inline void wakeupMode() {
PORTD &= ~PIR;
EIFR = bit(INTF0);
attachInterrupt(0, wakeup, RISING );
}
void wakeup() {
sleep_disable();
detachInterrupt(0);
flag++;
}
No. It looks like you were trying to set the LED pin as an output and set it to LOW but you are using PORTD instead of PORTC for Micro or PORTB for UNO. You are setting Pin 6 on Micro and Pin 5 on UNO.
I think that is a digitalWrite(0, LOW);. Why would you set Pin 0 to LOW?
to be correct, here digitalWrite(3, LOW) seence INT0 == PD0 == pin3 (micro), BUT
you open my eyes because I use this code also for Nano , is need for me to correct this logic. thank you!
I have your latest example tried....
on micro is not working because you have not used USBDevice.attach detach , plus need add time for upload USBDevice attach... I tried to add this my self (MCU is goes to sleep and wakeup) but not get anything on serial... need to brainstorm more
on Nano is working but PB1 is triggering only once after each restart, cannt understand why