Hello there,
I'm building a circuit with two I2C controlled sensors: a 3 axis digital compass (HMC5883L) and a clock (DS1307). Both seems to work ok separatedly, but I'm not able to connect both together.
Both can be powered by +5 - GND, so i supposed this circuit:
Then, I initialize them with this code (most part is based on the examples included with sensors libraries):
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_HMC5883_U.h>
#include "RTClib.h"
Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);
RTC_DS1307 RTC;
void Start(){
Serial.begin(57600);
Wire.begin();
RTC.begin();
if(!RTC.isrunning()){
Serial.println("RTC is NOT running!");
}
if(!mag.begin()){
Serial.println("MAGNETOMETER is NOT running");
}
}
Let's suppose there are no compilation errors:
The Serial debugs "RTC is NOT running!", while Magnetometer gives correct values.
I also suppose the conections are ok, so it's a code error?
Anyone can help me to find a solution?
EDIT:
I get it work reading without libraries, but code is much larger:
#include <Wire.h>
#define RTSADDRESS 0x68
#define MGNADRESS 0x00 //Not defined
byte cDay, cMonth, cHour;
byte decToBcd(byte val){ //Convertir valores de binario Decimal a Binario BCD
return( (val/10*16) + (val%10) );
}
byte bcdToDec(byte val){ //Convertir valores de Binario BCD a Decimal
return( (val/16*10) + (val%16) );
}
void setRtsTime(byte second, byte minute, byte hour, byte dayOfWeek, byte
dayOfMonth, byte month, byte year)
{
// sets time and date data to DS3231
Wire.beginTransmission(RTSADDRESS);
Wire.write(0); // set next input to start at the seconds register
Wire.write(decToBcd(second)); // set seconds
Wire.write(decToBcd(minute)); // set minutes
Wire.write(decToBcd(hour)); // set hours
Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
Wire.write(decToBcd(month)); // set month
Wire.write(decToBcd(year)); // set year (0 to 99)
Wire.endTransmission();
}
void ReadRtsTime(){ //Only day, month and hour
Wire.beginTransmission(RTSADDRESS);
Wire.write(0); // set DS3231 register pointer to 00h
Wire.endTransmission();
Wire.requestFrom(RTSADDRESS, 7); //Coger 7 bytes de RTS empezando en el registro 00h (0)
int temp; //Guardamos segundos, minutos, dia de la semana, año -> Datos inutiles
temp = bcdToDec(Wire.read() & 0x7f); //Segundos (0x7f)
temp = bcdToDec(Wire.read()); //Minutos
cHour = bcdToDec(Wire.read() & 0x3f); //0x3f -> dirección en hexadecimal de la hora en bcd
temp = bcdToDec(Wire.read()); //Dia de la semana (1 -7)
cDay = bcdToDec(Wire.read());
cMonth = bcdToDec(Wire.read());
temp = bcdToDec(Wire.read());
}