ATTiny1616 External Interrupt from DS3231 RTC

Hello! I'm working on a project where I need to wake up an ATTiny1616 using an interrupt from DS3231 RTC. The interrupt pin works while the attiny is awake, but once it is put into sleep mode it seems as though the interrupt cannot wake it up. Any help would be greatly appreciated! I have had a hard time finding resources for the ATTiny1616 interrupts and can't seem to decipher the data sheet. I'm wondering if it's not possible to use the arduino-type interrupt and if a c-type one is necessary? However, if this were the case then I don't understand why it seems to work fine while the system is awake. Here is the code I am using (the second part of the loop function isn't being used yet):

#include <Arduino.h>
#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
#include <RTClib.h>
#include <avr/io.h>

#define INT_PIN 6  


/*--------------------RTC CODE-------------------------*/
RTC_DS3231 rtc;

// the pin that is connected to SQW
//#define CLOCK_INTERRUPT_PIN 3 

int count = 0;
boolean doFirstAlarm;

void increment() { 
  //this is supposed to be the ISR
  Serial.println("Alarm triggered!");

  //LEDs used for debugging
  digitalWrite(12, HIGH);
  delay(50);
  digitalWrite(12, LOW);
  
}

void setup() {

  /*------Sleep Settings-------*/
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_enable();

  pinMode(12, OUTPUT);

  //initialize and test serial (test for successful UPDI upload)
  Serial.begin(9600);

  pinMode(1, OUTPUT);
  doFirstAlarm = true;

  // initializing the rtc
  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC!");
    Serial.flush();
  }
  
  // don't need the 32K Pin, so disable it
  rtc.disable32K();

  pinMode(INT_PIN, INPUT_PULLUP);          //interrupt pin
  
  attachInterrupt(digitalPinToInterrupt(INT_PIN), increment, FALLING); //this currently calls increment, not ISR(PCINT1_vect).

  rtc.clearAlarm(1);
  rtc.clearAlarm(2);

  // stop oscillating signals at SQW Pin
  rtc.writeSqwPinMode(DS3231_OFF);
  rtc.disableAlarm(2);  

  cli(); //disables interrupts
  sei(); //enables interrupts

}

void loop() {
  
  if (doFirstAlarm) {

    DateTime now = rtc.now();
    // future alarm time is now + timespan given.
    DateTime future (now + TimeSpan(0,0,0, 10)); //total seconds or // days,hrs,mins,secs
    
    if (!rtc.setAlarm1(future, DS3231_A1_Hour))
    {
      Serial.println("Error, alarm wasn't set!");
      
      //LEDs used for debugging
      digitalWrite(14, HIGH);
      delay(1000);
      digitalWrite(14, LOW);

    } else {
      
      Serial.println("Alarm will happen in 10 seconds!");
      
      //LEDs used for debugging
      digitalWrite(1, HIGH);
      delay(1000);
      digitalWrite(1, LOW);
      //delay(10000);  //toggle on/off to test if interrupt works while system is awake/asleep
      
      sleep_cpu();

/*****************THIS IS NEVER REACHED*********************/

      digitalWrite(14, HIGH);
      delay(1000);
      digitalWrite(14, LOW);

      rtc.clearAlarm(1); 
      
    }
  }

  doFirstAlarm = false;

  //second alarm code
  if (!rtc.setAlarm1(rtc.now() + TimeSpan(5), DS3231_A1_Second)) {
    Serial.println("Error, alarm wasn't set!");

    //LEDs used for debugging
    digitalWrite(14, HIGH);
    delay(1000);
    digitalWrite(14, LOW);

  } else {

    //LEDs used for debugging
    digitalWrite(1, HIGH);
    delay(500);
    digitalWrite(1, LOW);
            
    sleep_cpu();

    rtc.clearAlarm(1);
  }
 }

The DS3231 interrupt output is open collector, so you need a pullup resistor to see the signal. Is the INPUT_PULLUP state maintained during sleep?

Where is the interrupt enabled? Which core are you using?

Please post a wiring diagram.

I am using the megaTinyCore and uploading with UPDI.

Here is a pic of my settings:

I wasn't quite sure about enabling the interrupt for the attiny1616, all I have currently is:

cli(); //disables interrupts
sei(); //enables interrupts

Here is a picture of the circuit, (Sorry, I am working on a wiring diagram now):

I've been using the blue LED to indicate when the alarm is "counting down." So it is on until the end of the alarm, at which point the yellow LED should flash on/off at the falling edge of the square wave I believe? However, this only occurs when the alarm completes before system is put in sleep mode. Please let me know what other info I can provide, thanks.

AFAIK all interrupts have to be specifically and individually enabled. It attachInterrupt() doesn't do that for your specific case (check the core code), you need to set an appropriate bit in a register.

I suggest to get the interrupt working using a small test program, with a button for an interrupt.

In deep sleep/power down mode, only pins 2 and 6 of each port is available for full asynchronous detection

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.