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 :
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)