Reason of wake up from pin change (PCINT) on few pins

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

 

Spending few weeks with Attiny 85 and lots of readings of docs, github, this forum to build 'power manager' for small project.

The ATtiny should always sleep, being awaken from one of 2 inputs (PCINT0 or PCINT4).

I want to know which pin caused wake up 0 or 4.

 

Tried to use regular 'lang' like:

PCMSK  |= bit (PCINT0);  // awake on D0
PCMSK  |= bit (PCINT4);  // awake on D4
GIFR   |= bit (PCIF);    // clear any outstanding interrupts
GIMSK  |= bit (PCIE);    // enable pin change interrupts 

However, it's hard for me debug without serial output. So, I am forced to use following code for the same functionality:

#include <avr/sleep.h>
#include <avr/power.h>
#include <TinyPinChange.h>
#include <SoftSerial.h>     /* Allows Pin Change Interrupt Vector Sharing */
#define DEBUG_TX_RX_PIN     3
SoftSerial sSerial(DEBUG_TX_RX_PIN, DEBUG_TX_RX_PIN, false);

volatile uint16_t FirstInputChangeCount = 0;  /* Volatile since the variable will be updated in interruption */
volatile uint16_t SecondInputChangeCount = 0; /* Volatile since the variable will be updated in interruption */
uint8_t VirtualPortNb;
uint8_t VirtualPortNb_;

void setup() {
  TinyPinChange_Init();
  VirtualPortNb  = TinyPinChange_RegisterIsr(0, InterruptFunctionToCall);
  VirtualPortNb_ = TinyPinChange_RegisterIsr(4, InterruptFunctionToCall);
  TinyPinChange_RegisterIsr(0, InterruptFunc);
  TinyPinChange_RegisterIsr(4, InterruptFunc);

  /* Enable Pin Change for each pin */
  TinyPinChange_EnablePin(ESP_14);
  TinyPinChange_EnablePin(WTR_IN);

  sSerial.begin(115200);/
  sSerial.txMode();
}

void loop () {
  // Excluded sleep function for simplicity of code block
  sSerial.print(F("FirstInputChangeCount="));sSerial.println(FirstInputChangeCount);
  sSerial.print(F("SecondInputChangeCount="));sSerial.println(SecondInputChangeCount);
}

void InterruptFunctionToCall(void)
{
    if(TinyPinChange_RisingEdge(VirtualPortNb, 0)) // count rising only
    {
        FirstInputChangeCount++; 
    }
    
    if(TinyPinChange(VirtualPortNb_, 4))  // count any change
    {
        SecondInputChangeCount++; 
    }
}

Both counters 'First' and 'Second' count on any change of any pin.

How can i distinguish which pin changed its state?

 

Thank you.

This topic has a solution.
Last Edited: Fri. Jul 19, 2019 - 10:29 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

grhhm wrote:
How can i distinguish which pin changed its state?
You keep a copy of the PIN register then read it again at wake up and (probably using Xor: ^) you detemrine which bit has changed to cause the PICINT ISR.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you!