How to test DS1307 rtc module

Hello to all,

I have purchased a DS1307 RTC module and I’m wondering if there’s any way to test it.

My project consists in hooking up a LCD shield to the RTC module and an Arduino Duemilanove. I just want it to do simple things : read date and print it on the LCD.

I have tested several codings so far but none of them seem to work : it prints odd characters.

I’ve come to think that the RTC module might be down. Or maybe I’m just using a wrong code. Lately I’ve used and modified the code that is available from sparkfun’s rtc module’s page :

#include <Wire.h>

#include <LCD4Bit_mod.h>

//create object to control an LCD.

//number of lines in display=1

LCD4Bit_mod lcd = LCD4Bit_mod(2);

int clockAddress = 0x68; // This is the I2C address

int command = 0; // This is the command char, in ascii form, sent from the serial port

long previousMillis = 0; // will store last time Temp was updated

byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;

byte test;

// 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()

{

// Use of (byte) type casting and ascii math to achieve result.

second = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48));

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(clockAddress);

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(clockAddress);

Wire.send(0x00);

Wire.endTransmission();

Wire.requestFrom(clockAddress, 7);

// A few of these need masks because certain bits are control bits

second = bcdToDec(Wire.receive() & 0x7f);

minute = bcdToDec(Wire.receive());

// Need to change this if 12 hour am/pm

hour = bcdToDec(Wire.receive() & 0x3f);

dayOfWeek = bcdToDec(Wire.receive());

dayOfMonth = bcdToDec(Wire.receive());

month = bcdToDec(Wire.receive());

year = bcdToDec(Wire.receive());

lcd.print(hour);

lcd.printIn(“:”);

lcd.print(minute);

lcd.printIn(“:”);

lcd.print(second);

lcd.printIn(" ");

lcd.print(month);

lcd.printIn(“/”);

lcd.print(dayOfMonth);

lcd.printIn(“/”);

lcd.print(year);

}

void setup() {

pinMode(13, OUTPUT); //we’ll use the debug LED to output a heartbeat

lcd.init();

//optionally, now set up our application-specific display settings, overriding whatever the lcd did in lcd.init()

//lcd.commandWrite(0x0F);//cursor on, display on, blink on. (nasty!)

lcd.clear();

Wire.begin();

Serial.begin(57600);

}

void loop() {

setDateDs1307();

getDateDs1307();

lcd.printIn(" ");

delay(100);

}

Here’s a pic of what the LCD is showing :

[<LINK_TEXT text=“http://images3.hiboox.com/images/5309/3 … 3e9850.jpg”>http://images3.hiboox.com/images/5309/3c48a19c7746daaf9944683e0a3e9850.jpg</LINK_TEXT>

Could you tell me what’s wrong ?

Thank you guys for your help, I’m so desperate ![/img]](http://www.hiboox.fr/go/images/divers/img-0486,3c48a19c7746daaf9944683e0a3e9850.jpg.html)

Suggest you fight/win one battle at a time.

You “have not” initialized the Lcd - especially if you are using 4 bit mode! Google or SF should have listings showing how. Insure you insert sufficient “delays” between each init command. Simply “clearing” the Lcd is “Not” sufficient initialization - especially when in 4 bit mode!

  1. send a single character to Lcd row 1 - confirm that this works

  2. send a small string to the Lcd row 1 - same

  3. repeat (1) & (2) now to row 2 (bottom)

This will confirm that your delays, pulse widths are proper for your Lcd.

Only “after” this - would I extend your effort into the RTC.

You do have the contrast, backlight right. Look specifically now at:

a) initialization code - vital for 4 bit operation

b) understand & comply with RS management (i.e. low for command)

c) provide required delays: especially long during (a), long after CLS (clear screen command), and at least 40uS char to char

With “no” scope/tools suggest you create 1 second delay and place this everywhere - again especially between each instruction during 4 bit init.

While obnoxious - this is safe method which will remove “delay violation” from your list of “causes.” Good luck.

I was just looking at the that ds1307 code (writing one for pcf8563).

Set your date in setup. Also, when you call setdate its expecting specifically coded data to come in on the serial. You might want to just hardwire that for testing…

sec = 10;

minute = 9;

hour = 8;

etc.

Set your loop time to 1 second.

I2C is ‘relatively’ slow.

Recheck all the bytes and the i2c address(remember arduino wire is 7 bit)

Make sure your clock and data lines are correct. IIRC using the IDE and Wire, you cannot change the hardware I2C pins (unlike the lcd where you can declare the pins at init).