#include <Wire.h>

#define BAUD_RATE 19200
#define statusLED 12

// Addr. |Mode (162 + Read)
#define RTCAddressW 0xA2
#define RTCAddressR 0xA3

volatile int blinkStatus = LOW;

void setup(void)
{
// initialize inputs/outputs

// start serial
Serial.begin(BAUD_RATE);

Wire.begin();

Wire.beginTransmission(RTCAddressW);
Wire.send(0);
Wire.endTransmission();

// Registers

Wire.beginTransmission(RTCAddressW);
Wire.send(B00000000); // 00H Test mode
Wire.send(B00000000); // 01H Alarm mode
Wire.send(B00000001); // 02H Seconds
Wire.send(30); // 03H Minutes:p
Wire.send(19); // 04H Hours
Wire.send(11); // 05H Date
Wire.send(2); // 06H Day of the week
Wire.send(B00000000); // 07H Century bit 19XX = 1 | 20XX = 0
Wire.send(7); // 08H year
Wire.send(B00000000); // 09H Minute alarm
Wire.send(B00000000); // 0AH Hour alarm
Wire.send(B10000000); // 0BH Day alarm
Wire.send(B10000000); // 0CH Weekday alarm
Wire.send(B10000011); // 0DH CLKOUT control (00: 32.768Khz 11: 1Hz)
Wire.send(B00000000); // 0EH Timer control
Wire.send(B00000000); // 0FH Timer
Wire.endTransmission();

reportAVRState(3,0);
}

char i = 0;
#define numberOfRegisters 16
char c[numberOfRegisters];

void loop()
{
Wire.beginTransmission(RTCAddressR);
Wire.send(0);
Wire.endTransmission();

Wire.beginTransmission(RTCAddressR);
Wire.requestFrom(RTCAddressR, numberOfRegisters); // request 1 bytes from slave device #32

i = 0;
while(Wire.available()) // slave may send less than requested
{
c[i] = Wire.receive();
i++;
}
Wire.endTransmission();

for(i=0;i<numberOfRegisters;i++)
{
Serial.print(i);
Serial.print(":\t");
Serial.println(c, BIN); // receive a byte as character
}

Serial.println("Waiting a little");
delay(2000);
}

// Reports the state of the controller via pin 13's led and serial
void reportAVRState(int howManyTimes, int leaveOn)
{
int i;

pinMode(statusLED, OUTPUT);

for (i=0; i < howManyTimes; i++) {
digitalWrite(statusLED, HIGH);
delay(200);
digitalWrite(statusLED, LOW);
delay(200);
}

if (leaveOn) {
digitalWrite(statusLED, HIGH);
}

Serial.println();
Serial.println("AVR Initialized");
}