I have a project in which I take my Atmega1284 chip to sleep and wake up at certain times to do some work. During microcontroller's sleep, I want to take also the RTC 3231 to low power mode to save energy.
I don't know if it is normal, but when I cut the power to my DS3231 RTC by making the connected pin on my Atmega1284 LOW, and thereby forcing it to battery power, the SQW button also goes LOW and automatically wakes the microcontroller, because of the interrupt connected to it.
Is this normal, shouldn't SQW stay HIGH even when on battery? Otherwise how can the RTC be taken to low power mode?
#include <Wire.h>
#include <RTClibExtended.h>
#include <LowPower.h>
int rtcpin = 21;
RTC_DS3231 RTC; //we are using the DS3231 RTC
void telltime()
{
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
DateTime now = RTC.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
}
//-------------------------------------------------
void wakeUp() // here the interrupt is handled after wakeup
{
Serial.println("wakeup worked");
}
void alarmset() {
RTC.alarmInterrupt(1, true);
attachInterrupt(digitalPinToInterrupt(10), wakeUp, LOW);
digitalWrite(rtcpin, LOW);
byte savedPCICR = PCICR;
PCICR = 0; // Disable all pin change interrupts
void hal_sleep (void);
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF); //arduino enters sleep mode here
detachInterrupt(digitalPinToInterrupt(10)); //execution resumes from here after wake-up
digitalWrite(rtcpin, HIGH);
telltime();
RTC.armAlarm(1, false);
RTC.clearAlarm(1);
RTC.alarmInterrupt(1, false);
PCICR = savedPCICR; // Restore any pin change interrupts that were disabled
delay(100);
}
void setup() {
Serial.begin(115200); //hızlandırdık
//Initialize communication with the clock
Wire.begin();
pinMode(rtcpin, OUTPUT);
digitalWrite(rtcpin, HIGH);
RTC.begin();
RTC.adjust(DateTime(__DATE__, __TIME__)); //set RTC date and time to COMPILE time
//clear any pending alarms
RTC.armAlarm(1, false);
RTC.clearAlarm(1);
RTC.alarmInterrupt(1, false);
RTC.armAlarm(2, false);
RTC.clearAlarm(2);
RTC.alarmInterrupt(2, false);
RTC.writeSqwPinMode(DS3231_OFF);
}
//------------------------------------------------------------
void loop() {
RTC.setAlarm(ALM1_MATCH_HOURS, 50, 5, 0); //set your wake-up time here
telltime ()
Serial.println("1. alarm cycle");
alarmset ();
Serial.println("1. alarm triggered");
delay(1000);
RTC.setAlarm(ALM1_MATCH_HOURS, 55, 5, 0); //set your wake-up time here
telltime ();
Serial.println("2.. alarm cycle");
alarmset ();
Serial.println("2. alarm triggered");
delay(1000);
}