Pages: [1]   Go Down
Author Topic: RTC DS1307 issue  (Read 2163 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, I'm an Arduino beginner. I'm trying to use a RTC DS1307 circuit with an Arduino Leonardo.
I'm experiencing some trouble. because it doesn't behave correctly.
The code I'm using is:

Code:
//Function: This procedure applies to the Arduino MEGA 2560 to read and write the DS1307.Using the serial port to display the current time.
//Time: August 16, 2012
#include <Wire.h>
  uint8_t seconds = 30;//Initialization time
  uint8_t minutes = 43;
  uint8_t hours = 14;
  uint8_t week = 4;
  uint8_t days = 16;
  uint8_t months = 8;
  uint16_t Year = 12;
int bcd2bin(int temp)//BCD  to decimal
{
int a,b,c;
a=temp;
b=0;
if(a>=16)
{
while(a>=16)
{
a=a-16;
b=b+10;
c=a+b;
temp=c;
}
}
return temp;
}

int bin_to_bcd(int temp)//decimal to BCD
{
int a,b,c;
a=temp;
b=0;
if(a>=10)
{
while(a>=10)
{
a=a-10;
b=b+16;
c=a+b;
temp=c;
}
}
return temp;
}
void setup () {
    Serial.begin(9600);
    Wire.begin();
    //begin(void) {return 1;}
}
 
void loop () {
  /*Wire.beginTransmission(0x68);//DS1307 write the initial time
  Wire.write(0);
  Wire.requestFrom(0x68, 7);
  Wire.write(bin_to_bcd(seconds));
  Wire.write(bin_to_bcd(minutes));
  Wire.write(bin_to_bcd(hours));
  Wire.write(bin_to_bcd(week));
  Wire.write(bin_to_bcd(days));
  Wire.write(bin_to_bcd(months));
  Wire.write(bin_to_bcd(Year));
  Wire.endTransmission();*/
  
  while(1){
  Wire.beginTransmission(0x68);//Send the address of DS1307
  Wire.write(0);//Sending address
  Wire.endTransmission();//The end of the IIC communication
  
  Wire.requestFrom(0x68, 7);//IIC communication is started, you can continue to access another address (address auto increase) and the number of visits
    seconds = bcd2bin(Wire.read());//read time
    minutes = bcd2bin(Wire.read());
    hours = bcd2bin(Wire.read());
    week = Wire.read();
    days = bcd2bin(Wire.read());
    months = bcd2bin(Wire.read());
    Year = bcd2bin(Wire.read()) + 2000;
  
      Serial.print(Year, DEC);//Serial display time
      Serial.print('/');
      Serial.print(months, DEC);
      Serial.print('/');
      Serial.print(days, DEC);
      Serial.print(' ');
      Serial.print(hours, DEC);
      Serial.print(':');
      Serial.print(minutes, DEC);
      Serial.print(':');
      Serial.print(seconds, DEC);
      Serial.println();
      Serial.print(" week: ");
      Serial.print(week, DEC);
      Serial.println();
      delay(5000);
  }
}
and I connect the RTC DS1307 to the Leonardo card in the way described in the attached file.
Now, the problem is when I compile the code and transfer it to the arduino, the serial monitor does not print the right values, but just a series of 255 values.
What can be the reason? Could anyone help me?
thanks
Antonio


* DFRobot DS1307 connection Diagram.jpg (108.71 KB, 994x631 - viewed 146 times.)
« Last Edit: October 10, 2012, 03:54:25 pm by joyce973 » Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Read this before posting a programming question

Please use code tags.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12577
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Now, the problem is when I compile the code and transfer it to the arduino, the serial monitor does not print the right values, but just a series of 255 values.

Have you got an example of the output? As far as I can see, the output should at least be formatted as a date/time value, even if the numbers are wrong.

I don't know what the Wire interface does, but Serial returns -1 (0xFF or 255) when there is no byte available. Do you need to wait until there is a byte available on the Wire interface before reading it?
« Last Edit: October 10, 2012, 04:44:51 pm by PeterH » Logged

I only provide help via the forum - please do not contact me for private consultancy.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Code:
Wire.write(0);
  Wire.requestFrom(0x68, 7);
  Wire.write(bin_to_bcd(seconds));
  Wire.write(bin_to_bcd(minutes));
  Wire.write(bin_to_bcd(hours));
  Wire.write(bin_to_bcd(week));
  Wire.write(bin_to_bcd(days));
  Wire.write(bin_to_bcd(months));
  Wire.write(bin_to_bcd(Year));
  Wire.endTransmission();*/

This is not how you use Wire. Please read this:

http://www.gammon.com.au/i2c
Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 107
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is there a reason you aren't using one of the RTC libraries?  You can certainly do it yourself, but it seems like the hard way to do it.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello again. After some months I've tried again with the RTC library.
The code I'm using is:
Code:
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib

#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 RTC;

void setup () {
    Serial.begin(9600);
    Wire.begin();
    RTC.begin();

  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }
}

void loop () {
    DateTime now = RTC.now();
   
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();

    delay(3000);

but the result is not good.
The log I'm receiving is:

2165/165/165 165:165:85
2165/165/165 165:165:85
2165/165/165 165:165:85

Why? I really don't understand what are my mistake.
Could you help me, please?
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 72
Posts: 7171
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Read this, middle of page about TWI:

http://arduino.cc/en/Main/ArduinoBoardLeonardo

This leonardo has I2C bus on different pins than what you expect on UNO. You wired A4 and A5 to the RTC, as if you were using an UNO. Wire them to digital 2 and 3 or the dedicated I2C headers instead.
Logged


0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Now it works!!!
Thank you sooo much smiley
A
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry again, but after few minutes the RTC module lost all the data stored, showing "2000/1/1 0:00:00".
Maybe the battery is exhausted... What do you think about?
Logged

Dee Why NSW
Offline Offline
Edison Member
*
Karma: 47
Posts: 2200
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The battery in the clock module should be good for ten years.

This sort of output     2165/165/165 165:165:85         could be indicative of inadequate power to the main board.
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 72
Posts: 7171
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The battery in the clock module should be good for ten years.

This sort of output     2165/165/165 165:165:85         could be indicative of inadequate power to the main board.

In this case, it's probably because the pins weren't connected correctly. Some battery is good for 10 years but I'm not sure about this particular one. If after removing the battery the clock runs without problem, then it's probably the battery.

I recommend the OP to simply use an RTC library so the code is guaranteed to work then the focus could be on code other than I2C communication and wiring of the circuit.
Logged


0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok. But now it stores date/time correctly and I can read it (i.e. 01/23/2013 10.23), ok?
But when I disconnect the RTC from the Arduino board and reconnect it after 2-3 minutes, the date/time is reset again.
That's why I believed the battery was low.
was I right?
A
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 72
Posts: 7171
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes you are right. Get a new battery and make sure it is inserted in the holder correctly.
Logged


0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thank you smiley
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tested the battery and found it was completely exhausted. I tried again using a new CR 2032 but it seems not to work, maybe because it's a 3V battery instead of 3.6V. Could that be the reason?

Logged

Pages: [1]   Go Up
Jump to: