Hi Mike,
I couldn't figure out how to load a picture like this to the forum so I followed your www link. A photo is on the way.
5v comes from the Arduino to the breadboard and is jumpered across the board to the other side.
Ground is handled the same
Analog pin 4 is the green wire. It goes to the board and connects to the yellow wire which goes to SCA on the clock
Analog pin 5 is the blue wire. It goes to the board and connectes to the blue wire which goes to the SCL on the clock.
+5v on the clock module goes to +5 on the breadboard
GND on the clock module goes to GND on the breadboard
I found the code below on the Arduino playground. It came from Combustory.com. I ran it exactly as it came from there and the clock returned all zeros. In the course of understanding it a bit better, I cleaned it up some so the data returned is a bit tighter.
I also ran Matt's code. All Zeroes
I ran several other examples. Zeroes
The Combustory code was the only one that had the routine for setting some memory to 0xff to (as I read it) get the clock out of backup mode. It's completely command driven. The first command is to get the time. Second command writes 0xff to registers 8-35. Third command dumps all registers.
thanks,
Roger
1: 0:0:0 0/0/0
Command: 49
0:0:0 0/0/0: RTC1307 Initialized Memory
Command: 50
0:0:0 0/0/0: RTC 1307 Dump Begin
0: 0 1: 0 2: 0 3: 0 4: 0 5: 0 6: 0 7: 0
8: 0 9: 0 10: 0 11: 0 12: 0 13: 0 14: 0 15: 0
16: 0 17: 0 18: 0 19: 0 20: 0 21: 0 22: 0 23: 0
24: 0 25: 0 26: 0 27: 0 28: 0 29: 0 30: 0 31: 0
32: 0 33: 0 34: 0 35: 0 36: 0 37: 0 38: 0 39: 0
40: 0 41: 0 42: 0 43: 0 44: 0 45: 0 46: 0 47: 0
48: 0 49: 0 50: 0 51: 0 52: 0 53: 0 54: 0 55: 0
56: 0 57: 0 58: 0 59: 0 60: 0 61: 0 62: 0 63: 0
RTC1307 Dump end
/*
- RTC Control v.01
- by http://www.combustory.com John Vaughters
- Credit to:
- Maurice Ribble - http://www.glacialwanderer.com/hobbyrobotics for RTC DS1307 code
- With this code you can set the date/time, retreive the date/time and use the extra memory of an RTC DS1307 chip.
- The program also sets all the extra memory space to 0xff.
- Serial Communication method with the Arduino that utilizes a leading CHAR for each command described below.
- Commands:
- T(00-59)(00-59)(00-23)(1-7)(01-31)(01-12)(00-99) - T(sec)(min)(hour)(dayOfWeek)(dayOfMonth)(month)(year) - T Sets the date of the RTC DS1307 Chip.
- Example to set the time for 02-Feb-09 @ 19:57:11 for the 3 day of the week, use this command - T1157193040209
- Q(1-2) - (Q1) Memory initialization (Q2) RTC - Memory Dump
*/
#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68 // This is the I2C address
// Global Variables
int command = 0; // This is the command char, in ascii form, sent from the serial port
int i;
long previousMillis = 0; // will store last time Temp was updated
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
byte test;
long nIteration = 0;
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return ( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return ( (val/16*10) + (val%16) );
}
// 1) Sets the date and time on the ds1307
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock
// Assumes you're passing in valid numbers, Probably need to put in checks for valid numbers.
void setDateDs1307()
{
second = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48)); // Use of (byte) type casting and ascii math to achieve result.
minute = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
hour = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
dayOfWeek = (byte) (Serial.read() - 48);
dayOfMonth = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
month = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
year= (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0x00);
Wire.send(decToBcd(second)); // 0 to bit 7 starts the clock
Wire.send(decToBcd(minute));
Wire.send(decToBcd(hour)); // If you want 12 hour am/pm you need to set
// bit 6 (also need to change readDateDs1307)
Wire.send(decToBcd(dayOfWeek));
Wire.send(decToBcd(dayOfMonth));
Wire.send(decToBcd(month));
Wire.send(decToBcd(year));
Wire.endTransmission();
}
// Gets the date and time from the ds1307 and prints result
void getDateDs1307()
{
// Reset the register pointer
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0x00);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
// A few of these need masks because certain bits are control bits
second = bcdToDec(Wire.receive() & 0x7f);
minute = bcdToDec(Wire.receive());
hour = bcdToDec(Wire.receive() & 0x3f); // Need to change this if 12 hour am/pm
dayOfWeek = bcdToDec(Wire.receive());
dayOfMonth = bcdToDec(Wire.receive());
month = bcdToDec(Wire.receive());
year = bcdToDec(Wire.receive());
Serial.print(hour, DEC);
Serial.print(":");
Serial.print(minute, DEC);
Serial.print(":");
Serial.print(second, DEC);
Serial.print(" ");
Serial.print(month, DEC);
Serial.print("/");
Serial.print(dayOfMonth, DEC);
Serial.print("/");
Serial.print(year, DEC);
}
void vInitializeMemory()
{
Wire.beginTransmission(DS1307_I2C_ADDRESS); // 255 will be the init value and 0 will be cosidered an error that occurs when the RTC is in Battery mode.
Wire.send(0x08); // Set the register pointer to be just past the date/time registers.
for (i = 1; i <= 27; i++)
{
Wire.send(0xff);
delay(100);
}
Wire.endTransmission();
getDateDs1307();
Serial.println(": RTC1307 Initialized Memory");
}
void vDumpMemory()
{
int nRow;
int nCol;
int nByte;
getDateDs1307();
Serial.println(": RTC 1307 Dump Begin");
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0x00);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 64);
for (nRow = 0; nRow < 8; nRow++)
{
for(nCol = 0; nCol < 8; nCol ++)
{
nByte = nRow * 8 + nCol;
test = Wire.receive();
if(nByte < 10)
Serial.print(" ");
Serial.print(nByte, DEC);
Serial.print(":");
if(test < 10)
Serial.print(" ");
if(test < 100)
Serial.print(" ");
Serial.print(test, DEC);
Serial.print(" ");
}
Serial.println();
}
Serial.println(" RTC1307 Dump end");
}
void nGetDate()
{
nIteration++;
Serial.print(nIteration);
Serial.print(": ");
getDateDs1307();
Serial.println();
}
void vHandleCommand()
{ // Look for char in serial que and process if found
command = Serial.read();
switch(command)
{
case 32: // space = get Date
nGetDate();
break;
case 84: // T = Set date
setDateDs1307();
getDateDs1307();
Serial.println(" ");
break;
case 81: // Q RTC1307 Memory Functions
delay(100);
if (Serial.available())
{
command = Serial.read();
Serial.print("Command: ");
Serial.println(command); // Echo command CHAR in ascii that was sent
switch(command)
{
case 49: //If command = "1" RTC1307 Initialize Memory - All Data will be set to 255 (0xff). Therefore 255 or 0 will be an invalid value.
vInitializeMemory();
break;
case 50: //If command = "2" RTC1307 Memory Dump
vDumpMemory();
break;
}
}
}
Serial.println("------------------------------------");
}
void setup() {
Wire.begin();
Serial.begin(9600);
}
void loop()
{
if (Serial.available())
vHandleCommand();
command = 0; // reset command
delay(1000);
}
//******************************The End