62 lines
1.5 KiB
Arduino
62 lines
1.5 KiB
Arduino
|
// Wire Master Reader
|
||
|
// by Nicholas Zambetti <http://www.zambetti.com>
|
||
|
|
||
|
// Demonstrates use of the Wire library
|
||
|
// Reads data from an I2C/TWI slave device
|
||
|
// Refer to the "Wire Slave Sender" example for use with this
|
||
|
|
||
|
// Created 29 March 2006
|
||
|
|
||
|
// This example code is in the public domain.
|
||
|
|
||
|
|
||
|
#include <Wire.h>
|
||
|
|
||
|
void setup()
|
||
|
{
|
||
|
Wire.begin(); // join i2c bus (address optional for master)
|
||
|
Serial.begin(9600); // start serial for output
|
||
|
writeMCP(0x6F, 0x01, 0x30); //Minutes
|
||
|
writeMCP(0x6F, 0x02, 0x12); //Hora
|
||
|
writeMCP(0x6F, 0x03, 0x03); //Day
|
||
|
writeMCP(0x6F, 0x04, 0x26); //Date
|
||
|
writeMCP(0x6F, 0x05, 0x06); //Month
|
||
|
writeMCP(0x6F, 0x06, 0x13); //Year
|
||
|
writeMCP(0x6F, 0x00, 0x45 | 0x80); //Year
|
||
|
}
|
||
|
|
||
|
byte readMCP(int deviceaddress, byte address ) {
|
||
|
Wire.beginTransmission(deviceaddress);
|
||
|
Wire.write(address);
|
||
|
Wire.endTransmission();
|
||
|
Wire.requestFrom(deviceaddress, 1);
|
||
|
unsigned long time = millis();
|
||
|
while (!Wire.available()) if ((millis() - time)>500) return 0x1F;
|
||
|
return Wire.read();
|
||
|
}
|
||
|
|
||
|
void writeMCP(int deviceaddress, byte address, byte time ) {
|
||
|
Wire.beginTransmission(deviceaddress);
|
||
|
Wire.write(address);
|
||
|
Wire.write(time);
|
||
|
Wire.endTransmission();
|
||
|
delay(100);
|
||
|
}
|
||
|
|
||
|
void loop()
|
||
|
{
|
||
|
Serial.println(readMCP(0x6F, 0x00)&0x7F, HEX);
|
||
|
//Serial.println(F_CPU);
|
||
|
|
||
|
delay(1000);
|
||
|
|
||
|
|
||
|
// Wire.beginTransmission(4); // transmit to device #4
|
||
|
// Wire.write("x is "); // sends five bytes
|
||
|
// Wire.write(x); // sends one byte
|
||
|
// Wire.endTransmission(); // stop transmitting
|
||
|
//
|
||
|
// x++;
|
||
|
// delay(500);
|
||
|
}
|