myuino:
Peter I had gone through the tutorial, in this case the DS1307 is read by the slave to display the clock which is connected on the same line with 0x68 register address.
In proper sequence of I2C communication, the SCL lines low, followed be transformation of data, and later it is made logic high, in order to avoid Confusion or collision do the master need to check for SCL low, before attempting command the slave what to do next.
--
Thanks in Advance
The I2C protocol uses the SCL line as the clock, it is driven by the Master device. The SDA (data) line driven by the sender.
so, for the 'master' to send a byte to the slave the master creates a Start condition.
The start condition is signified by the master pulling the clock (SCL) LOW, waiting a moment ( 1/2 bit time) then pulling the data (SDA) LOW. The next 7 transitions of the Clock (SCL) line are used to transfer the 7bit address of the slave one the data line (SDA) the 8th bit transfered is (READ or Write) if the 8th bit is HIGH it signifies a READ command, if it is LOW it signifies a Write operation. The Master releases the data (SDA) and monitors the data line for and acknowledge from the slave. This acknowledge is transmitted by the slave device holding the data line Low during the 9th bit of the clock.
After the Acknowledge bit the master either start sending the next byte (write mode) or listens to the data line (SDA) (read Mode) while it clocks the SCL line for each bit. to mark the end of transmission the master generates a stop condition by holding the clock line (SCL) LOW and releasing the data line (SDA) to a HIGH condition.
All of this bit twiddling is handled by the AVR hardware, the Wire library is the wrapper for the Two Wire Interface hardware.
to use the Wire library you only need to do a couple of things.
Here is simple code to read the current time in a DS1307 RTCC
#include <Wire.h>
#define rtccAddr 0x68 // DS1307 RTCC i2c Address
void setup(){
Serial.begin(9600); // for serial monitor
Wire.begin(); // initialize TWI hardware
/* set the internal RTCC register pointer to 0 */
Wire.beginTransmission(rtccAddr); // start communication with Slave device (0x68)
Wire.write((uint8_t)0); // add a byte to the Wire library transmit buffer
// write one byte of Zero, This sets the internal address pointer
// of the RTCC chip to 0
Wire.endTransmission(false); // actually send the buffer content to the slave, maintain ownership of the
// I2C bus
/* Read the current time /date from the RTCC */
Wire.requestFrom(rtccAddr,8,true); // read 8 byte from the RTCC, Starting with register 0
// the store it in the Internal Wire Receive buffer.
// release the I2C bus when completed,
// the I2C bus can have multiple 'masters' and multiple 'Slaves'
// at the same time. One at a time.
uint8_t x[8];
for(uint8_t i=0;i<8;i++){
x[i]=Wire.read(); // transfer from the internal Wire library buffer to my array x[]
}
Serial.print(x[2],HEX); // hours
Serial.print(':');
Serial.print(x[1],HEX); // minutes
Serial.print(':');
Serial.print(x[0],HEX); // seconds
Serial.print(' ');
Serial.print(x[5],HEX); // month
Serial.print('/');
Serial.print(x[4],HEX); // day
Serial.print('/');
Serial.print(x[6],HEX); // year
}
void loop(){}
Chuck.