I am trying to use the RTC exanple in the time library. I am sure I have my DS1307 connected correctly but When I upload the sketch it is hanging on the following line
setSyncProvider(RTC.get);
I am sure I have the DS1307 wired correctly.
Using the timRTC.pde example I am certain it is hanging on the above code line. I have also tried 3 different DS1307 chips but all behave the same.
Is the chip the right way round? Do you have the pins wired up in the correct order? (Sorry if this sounds annoying but it's always best to ask to ensure that it's not the issue)
The voltage of the backup battery should not affect it. I would suggest connecting the battery connection to ground to ensure it's not a funny battery though.
If you have a multimeter then check the voltage between 5v and gnd (on the chip) to maks sure that power is getting to the chip.
From 1307 datasheet:
Quote:
VBAT – Battery input for any standard 3V lithium cell or other energy source. Battery voltage must be held between 2.0V and 3.5V for proper operation.
Yes, for backup, it should work fine with a flat battery becuase when it is powered by the MCU, it does not use the battery. That is why you can ground that pin if you don't need backup.
Posted the wrong code that was my old code. Here is what I am currently using.
And it stops after the 'I am getting time'
#include <Time.h>
#include <Wire.h>
#include <DS1307RTC.h>
void setup() {
Serial.begin(9600);
Serial.println("I am Getting time");
setSyncProvider(RTC.get);
if(timeStatus()!= timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
}
void loop()
{
if(Serial.available())
{
time_t t = processSyncMessage();
if(t >0)
{
RTC.set(t);
setTime(t);
}
}
Serial.print("I am displaying time");
digitalClockDisplay();
delay(1000);
}
void digitalClockDisplay(){
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.println();
}
void printDigits(int digits){
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
/* code to process time sync messages from the serial port */
#define TIME_MSG_LEN 11 // time sync to PC is HEADER followed by unix time_t as ten ascii digits
#define TIME_HEADER 'T' // Header tag for serial time sync message
time_t processSyncMessage() {
// return the time if a valid sync message is received on the serial port.
while(Serial.available() >= TIME_MSG_LEN ){ // time message consists of a header and ten ascii digits
char c = Serial.read() ;
Serial.print(c);
if( c == TIME_HEADER ) {
time_t pctime = 0;
for(int i=0; i < TIME_MSG_LEN -1; i++){
c = Serial.read();
if( c >= '0' && c <= '9'){
pctime = (10 * pctime) + (c - '0') ; // convert digits to a number
}
}
return pctime;
}
}
return 0;
}
I am not using any pull-up resistors on the I2C signals. Instead I just rely on the internal pull-ups.
I would suggest trying without the 10k you are using. And if you want to use external resistors, use something smaller like 4.7k. Make sure you put them on both the Clock and Data line, not just one of them.
I brought two more DS1307's and still the same problem. However If i connect the circuit to my Arduino Mega it works fine. Does this mean my Duemilanove has a problem ?
I brought two more DS1307's and still the same problem. However If i connect the circuit to my Arduino Mega it works fine. Does this mean my Duemilanove has a problem ?
Also with the Arduino Mega it appears to work but does not set the time on the RTC if i use T1289663280 it should set it on the RTC and Arduino, but it does not if I reset the Arduino the time goes back to 0:0:0 31:12:1999
The DS1307 is connected with a battery backup etc, so should return the correct time when reset.
Have you tested the analogue input pins work correctly ?
Connect them to ground and take an analogue reading then connect to +5V and take another reading. You should get zero then 1023 (or close to that)