Hello!
I recently got this great adafruit shield which has an RTC on board. The RTC communicates using I2C (analog 4 and 5 on my arduino uno), and it uses this library.
I am trying to make the RTC work. If i try to access it (using RTC.now()) from loop() it works as expected. However, I would like to be able to get a DateTime object in the ISR, to know if I should wake up the processor just yet. If I try to use RTC.now() from the ISR the program hangs permanently.
I have shortened my code so you can see the problem. If the "DateTime dt = RTC.now()" line in the ISR is commented, there is no hang.
#include <Wire.h>
#include <RTClib.h>
RTC_DS1307 RTC;
void timerOneInit(){
cli();
TCCR1A = 0;// set entire TCCR1A register to 0
TCCR1B = 0;// same for TCCR1B
TCNT1 = 0;//initialize counter value to 0
OCR1A = 65535;// = (16*10^6) / (1*1024) - 1 (must be <65536)
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS10 and CS12 bits for 1024 prescaler
TCCR1B |= (1 << CS12) | (1 << CS10);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
sei();
}
//called when timer one overflows
ISR(TIMER1_COMPA_vect){
Serial.println("Timer one");
Serial.flush();
DateTime dt = RTC.now();
}
void setup(){
init();
Serial.begin(9600);
while(!Serial) ;
Serial.println("Serial Initialized!");
Wire.begin();
RTC.begin();
if(!RTC.isrunning()){
Serial.println("RTC is trying to set up...");
RTC.adjust(DateTime(__DATE__, __TIME__));
}
Serial.flush();
timerOneInit();
}
void loop(){
}
Why is this happening, and how can i fix it?
Thanks!