I've built an RTC hardware based on DS1307, and connected it in my Arduino Leonardo's I2C bus.
Using RTCLib.h, made by Jee Labs, the setting of the clock is perfect, however, I want to make my own library, for specific purposes, and is here that i've got a problem.
I try to read and write data from/to the RTC using Wire.h functions, but I'm not able to set the correct time and read it before.
I tried to get codes in the internet that does te communication between Arduino and RTC via I2C, but none of them have worked... Can somebody help me with this? My code that doesnt work is on the following (IDE v1.01):
#include <Wire.h>
void setup(){
Wire.begin();
Serial.begin(9600);
//escrevendo os valores seg 0 min 2 hora 3 dia 1...
Wire.beginTransmission(0x68);
Wire.write(0);
Wire.write(0b00001000);
Wire.write(0b00001000);
Wire.write(0b00001000);
Wire.write(0b00000001);
Wire.write(0b00000001);
Wire.write(0b00000001);
Wire.write(0b00000001);
Wire.write(0b10000000);
Wire.endTransmission();
}
char ss, mm, hh, a, b, c, d;
void loop(){
//começamos leitura
Wire.beginTransmission(0x68);
//escolho 1 reg
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(0x68,7);
ss = Wire.read();
mm = Wire.read();
hh = Wire.read();
a = Wire.read();
b = Wire.read();
c = Wire.read();
d = Wire.read();
Serial.println(ss);
Serial.println(mm);
Serial.println(hh);
Serial.println(a);
Serial.println(b);
Serial.println(c);
Serial.println(d);
Serial.println("");
delay(3000);
}
Wire.beginTransmission(0x68);
Wire.write(0);
Wire.endTransmission();
*/
Wire.requestFrom(0x68,7);
ss = Wire.read();
mm = Wire.read();
hh = Wire.read();
a = Wire.read();
b = Wire.read();
c = Wire.read();
d = Wire.read();
and print them in the serial monitor...
As it is a RTC enabled, i should get the updated time each time the loop function restarts.
I'm sorry Nick Gammon. By "What does it print", I understood: What is it supouse to print.
It prints the same value for all variables (ss, mm, hh...). I tested setting diferent types of variables to ss, hh... If I set them as Byte, was print 255 for all. If integer, -1. If char ÿ.
And yes, I want all that information back from the RTC. All that 7 bytes I ask are useful date: The first 7 registers (each has a Byte long) have the current time and date.
I just figured out that the RTClib that I was using is not working as well when it tries to access the DS1307. What was working is a Class that uses Millis instead of the CI.
I have start thinking about and Hardware problem, instead of a code problem. What do you think? Anybody has used this Lib (by Jee Labs) and had positive results?
I've checked and rechecked the circuit and the wire up and I have conclued that it can only be the CI, that has a problem... I try to buy a new one and as soon as I have some results, I post here to make a conclusion. Thak you for your attention and time!
Near the bottom of that page is an I2C scanner. Load and run that sketch. See what address it prints (if any) for your RTC. That at least proves it is alive. It should display the address 0x68 since that is what you are using.
Nick,
I have run the scanner and it haven't found any device... I tried changing the values of the pull up resistors (that ones that is between the lines and the SDA, SCL gates) for a upper value (10k) and a lower level (1k), and I still have got anything. My original pull up resistors was 2k2.
I think I got a broken CI. I gonna try to change it and I post here my future results.
I'm a little embarassed about what was the real cause of the problem, but I think posting here can help other people with the same problem.
When I saw the pin out responsible for I2C in my arduino board, I visited a forum and the guy told me that SDA whas A4 and SCL was A5. BUT, I have Arduino LEONARDO, and the I2C pin out for this board is digital 2 (SDA) and digital 3(SCL). For this reason I was having problems reading and writing on the RTC. I discovered this thing not intentionally, reading Atmel's datasheet. I want to apologize for this mistake and thank by the time that everyone spent on this!
A special thank for Pete and Nick Gammon, that really helped!