Hello all,
I've got a problem I can't understand so would appreciate any pointers people could give me!
I have an RTC (DS1307) connected via I2C to a mega 2560. Everything was working yesterday. This morning the board wouldn't run it's program and after some checking I found it had hung at the point of receiving the I2C data (via Wire library).
I copied the minimal code out to a simpler sketch where it works fine (see below). I have marked with comments where the same code hangs in the larger more complex sketch.
I can fix the hang by inserting a serial.print( "anything" ) just before the wire.available().
Any ideas why this would hang on the large sketch (and was working yesterday) but today has a problem, and works in a smaller sketch??
Thanks for any pointers!
#include <Wire.h>
#define DS1307_I2C_ADDRESS 0x68 // I2C bus address of DS1307 RTC
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
pinMode( 13, OUTPUT );
Serial.begin(115200); // start serial for output
Serial.println( "started" );
delay( 100 );
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0);
byte status = Wire.endTransmission();
digitalWrite( 13, HIGH );
Serial.println( "request 7 bytes" );
Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
digitalWrite( 13, LOW );
// HANGS between here
while( Wire.available() < 7 )
{
// AND here
digitalWrite( 13, HIGH );
delay( 10 );
digitalWrite( 13, LOW );
delay( 10 );
}
int sec = bcdToDec(Wire.receive()& 0x7f); // 28/4: I've declared rtc variables globally so that log to eeprom could use them
int minute = bcdToDec(Wire.receive());
int hour = bcdToDec(Wire.receive()& 0x3f);
int day = bcdToDec(Wire.receive());
int date = bcdToDec(Wire.receive());
int month = bcdToDec(Wire.receive());
int year = bcdToDec(Wire.receive());
// log_debug( "got date bytes" );
Serial.print( year );
}
byte bcdToDec(byte val)
{
return ( (val/16*10) + (val%16) );
}
void loop()
{
}