Hi ALL,
For Some Reasons, I had to use SoftI2CMaster Library to make a software I2C to connect Arduino MEGA with RTC Module.
After a lot of Tests, it works fine but the problem is little weird.
The Test Code writes data in a certain location then read it again and then prints it in the serial monitor, But I found that numbers that have its hex representation not included A to F (10 TO 16) works fine, But if it has A To F, it gets wrong data.
For example: when I tried to save (0x23), I get the right data in the Serial Monitor which is (35), But when I tried (0x1A), I get wrong Data.
And that always happen whenever I have (A to F) in the data number.
// Simple sketch to read out one register of an I2C device
#define SDA_PORT PORTD
#define SDA_PIN 1 // = A4
#define SCL_PORT PORTD
#define SCL_PIN 0 // = A5
#define I2C_PULLUP 1
#include <SoftI2CMaster.h>
#define I2C_7BITADDR 0x68 // DS1307
#define MEMLOC 0x0A
#define SECLOC 0x00
#define MINLOC 0x01
#define HOURLOC 0x02
#define DAYLOC 0x03
#define DATELOC 0x04
#define MONTHLOC 0x05
#define YEARLOC 0x06
void setup(void) {
pinMode(15,OUTPUT);
digitalWrite(15,HIGH);
Serial.begin(57600);
if (!i2c_init()) // Initialize everything and check for bus lockup
Serial.println("I2C init failed");
}
void loop(void){
if (!i2c_start((I2C_7BITADDR<<1)|I2C_WRITE)) { // start transfer
Serial.println("I2C device busy");
return;
}
//i2c_write(0x00);
while(!i2c_write(SECLOC)); // send memory address
while(!i2c_write(0x29));
i2c_stop();
delay(10);
if (!i2c_start_wait((I2C_7BITADDR<<1)|I2C_WRITE)) {
Serial.println("I2C device busy");
delay(1000);
return;
}
//i2c_write(0x00);
while(!i2c_write(SECLOC));
while(!i2c_rep_start((I2C_7BITADDR<<1)|I2C_READ));
byte val = i2c_read(true); // read one byte and send NAK to terminate
i2c_stop(); // send stop condition
Serial.println(val);
Serial.println("");
delay(1000);
}