Ok
I am using an Atmel 1284P with the Mighty Optiboot bootloader if that information is needed.
So I can get each of these things working individually but the moment I put them together I get a very weird result.
I am trying to use an RTC DS3231 with the Arduino sleep function and woken up with an interrupt from the RTC. (Real time clock) with the SQW pin of the RTC attached to my micros interrupt pin.
Each of these things work fine when done alone but once put together the code freezes and does not continue.
Here is my code as of now. I’m trying to wake the micro every minute, the global var T being the interval for the waiting time.
#include <avr/sleep.h>
#include <avr/power.h>
#include <Wire.h>
#include "ds3231.h"
struct ts t; // The Time structure
int T = 1; // The sleeping time in minutes
void setup()
{
pinMode(2,INPUT); // my interuppt is on pin 2
Serial.begin(9600);
Wire.begin();
DS3231_init(DS3231_INTCN); // Start the RTC interface
}
void loop()
{
Serial.println("Running"); // something to see that the micro is wake for a bit and not frozen
delay(5000); // a delay in my main code I will be loggin data
set_next_alarm(); /// set the next RTC alarm
DS3231_clear_a2f(); // clear a2 alarm flag and let INT go into high ,,, disabling the interrupt
sleep_enable(); // get sleep ready
attachInterrupt(2,wakeUp, LOW); // set the interrupt things
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is power down for most power savings
cli();
sei();
sleep_cpu(); // Sleeps here
sleep_disable(); // Wakes up here
// I have ccopied this bit of the sleeping code from the arduino fourums
}
void wakeUp() // run when interrupt is called
{
detachInterrupt(2); // detach the interrupt
sleep_disable();
}
void set_next_alarm(void) // Set the next alarm here
{
unsigned char wakeup_min;
DS3231_get(&t); // get the current time
// calculate the minute when the next alarm will be triggered
wakeup_min = (t.min / T + 1) * T;
if (wakeup_min > 59) {
wakeup_min -= 60;
}
boolean flags[4] = { 0, 1, 1, 1 };
DS3231_set_a2(wakeup_min, 0, 0, flags); // set the alarm 2
// activate Alarm2
DS3231_set_creg(DS3231_INTCN | DS3231_A2IE);
}
But I get a frozen program when the code runs it sleeps and the alarm is triggered on the RTC. I know the RTC is triggering because I have put an LED on the SQW pin and see it go LOW but the RTC keeps the pin LOW and nothing else happens.
Any ideas or help would be appreciated I have been at this for a good 3 weeks but don’t have a clue here.
Ok So today I tired a different way to sleep and wake my micro by just using the Watchdog timer and sleeping for 8 seconds each time then waking and checking the alarm if it had triggered and going back to sleep or logging my data.
I know this is not what I needed exactly but it does work really well if not perfect for power saving.
I have since found that there is a bug in the pin mapping of the boot loader of the mighty1284P code that has to do with the one pin that I need and its interuppt.. at least that is what I read so I abandoned that attempt so I could continue with my project as I am not good enough to mess with the boot loader.
So that my fix for now until I find another way then I shall update this post again.