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);
}
}