In: Computer Science
Explain the section of code in the setup() function that implements the following instructions
Wire.beginTransmission(0x68);
Wire.write(0); // set the address
byte sec = Wire.read();
Wire.endTransmission(); // stop transmitting
Wire.beginTransmission(0x68);
Wire.write(0); // set the address
Wire.write(sec & 0x7F); // clear CH bit
Wire.endTransmission(); // stop transmitting
Why is this necessary?
Wire.beginTransmission(0x68);-Begins a transmission to I2C slave device with the address 0x68.
Wire.write(0); -write() is a function that writes data over the I2C BUS.Wire.write(0) will set the address to 0.
byte sec = Wire.read(); - It will read a byte that was transmitted from slave device to master and store in sec.
Wire.endTransmission(); -Ends the transmission to slave device that had been begun by beginTransmission().
Wire.beginTransmission(0x68);-It again begins a transmission to I2C slave device with the address 0x68.
Wire.write(0); -As expained earlier Wire.write(0) will again set the address to 0.(00H)
Wire.write(sec & 0x7F); -Here sec represents seconds and & represents AND operation which is performed between seconds with bit 7 and as a result it will clear Clock Hault(CH) bit.Bit 7 of register 0 is CH bit.
Wire.endTransmission(); -Ends the transmission to slave device that had been begun by beginTransmission().
Setup() importance-It is executed only once inthe beginning of your program.This function is useful to configure the Arduino. It is used for initializing variables,pin modes etc.It is here, for example, that you put the initial configurations, such as if a LED starts off or on, which the input and output pins are, among other things.