Good day to everyone.
I found a code in the net to work with a DSL1307 clock. In my arduino UNO works well (24/10/2012,09:01:32) but when I load the same code in the Mega the serial out put gives me something like this (165/165/20165,165:165:165).
I tried to load the clock with the hour again in the Mega but it doesn´t work. Any idea where is the problem?
Thnaks!!
Read this before posting a programming question
Your code?
What pins did you connect the clock to?
Can you give a link to the DSL1307 clock?
Here is the link to the clock. I use same Pins that you can see in the photo:
Here is the code:
#include <Wire.h>
#define DS1307_I2C_ADDRESS 0x68
byte decToBcd(byte val)
{
return ( (val/10*16) + (val%10) );
}
byte bcdToDec(byte val)
{
return ( (val/16*10) + (val%16) );
}
void setDateDs1307(byte second, // 0-59
byte minute, // 0-59
byte hour, // 1-23
byte dayOfWeek, // 1-7
byte dayOfMonth, // 1-28/29/30/31
byte month, // 1-12
byte year) // 0-99
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0);
Wire.write(decToBcd(second));
Wire.write(decToBcd(minute));
Wire.write(decToBcd(hour));
Wire.write(decToBcd(dayOfWeek));
Wire.write(decToBcd(dayOfMonth));
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));
Wire.endTransmission();
}
void getDateDs1307(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f); // Need to change this if 12 hour am/pm
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
void setup()
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
Wire.begin();
Serial.begin(115200);
second = 0;
minute = 0;
hour = 9;
dayOfWeek = 3;
dayOfMonth = 24;
month = 10;
year = 12;
}
Serial.print("<"); // Here is where I print the date, the delays are cause I send it via sms.
if (dayOfMonth < 10) Serial.print("0");
Serial.print(dayOfMonth);
Serial.print("/");
delay(1500);
if (month < 10) Serial.print("0");
Serial.print(month);
Serial.print("/");
delay(1500);
Serial.print("20");
Serial.print(year);
Serial.print(",");
delay(1500);
if (hour < 10) Serial.print("0");
Serial.print(hour);
Serial.print(":");
delay(1500);
if (minute < 10) Serial.print("0");
Serial.print(minute);
Serial.print(":");
delay(1500);
if (second < 10) Serial.print("0");
Serial.print(second);
Serial.print(">*");
delay(1500);
If you are using an Arduino Mega, SDA is pin 20 and SCL is 21, so note that shields with I2C need to be specifically for the Mega (taken from Tutorial: Arduino and the I2C bus – Part One | tronixstuff.com)