#include <Wire.h>
#define DS1307_ADDRESS 0x68
int min1=0,x,y,hr1,a,b;
int inputs1[4] = {22,23,24,25};// A,B,C,D inputs
int inputs2[4] = {26,27,28,29};
byte BCD[10][4] ={{0,0,0,0},{1,0,0,0},{0,1,0,0},{1,1,0,0},{0,0,1,0},
{1,0,1,0},{0,1,1,0},{1,1,1,0},{0,0,0,1},{1,0,0,1}}; // BCD input array for ic 4511
//
byte controlPins[2][4] = {{0,0,0,0},{1,0,0,0}}; //BCD input to latch
int selectlatch[4] = {40,41,42,43}; // ABCD input multiplexer
void setup()
{
for(int v=0;v<2;v++) {
pinMode (selectlatch[v],OUTPUT);
delay(2000);}// set outputs
for(int z = 0; z < 4; z++){
pinMode(inputs1[z], OUTPUT); //set outputs
pinMode(inputs2[z],OUTPUT);
}
Wire.begin();
Serial.begin(9600);
}
void loop()
{
printDate();
delay (1000);
for(int c = 0; c < 4; c++){
digitalWrite(selectlatch[c],controlPins[1][c]);
digitalWrite(inputs1[c], BCD[y][c]);
digitalWrite(inputs2[c], BCD[x][c]);
delay (1000);}
for(int c = 0; c < 4; c++){
digitalWrite(selectlatch[c],controlPins[2][c]);
digitalWrite(inputs1[c], BCD[b][c]);
digitalWrite(inputs2[c], BCD[a][c]);
delay (1000);}
}
byte bcdToDec(byte val) {
// Convert binary coded decimal to normal decimal numbers
return ( (val/16*10) + (val%16) );
}
void printDate(){
// Reset the register pointer
Wire.beginTransmission(DS1307_ADDRESS);
byte zero = 0x00;
Wire.write(zero);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
int second = bcdToDec(Wire.read());
int minute = bcdToDec(Wire.read());
int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
int monthDay = bcdToDec(Wire.read());
int month = bcdToDec(Wire.read());
int year = bcdToDec(Wire.read());
//print the date EG 3/1/11 23:59:59
Serial.print(month);
Serial.print("/");
Serial.print(monthDay);
Serial.print("/");
Serial.print(year);
Serial.print(" ");
Serial.print(hour);
Serial.print(":");
Serial.print(minute);
Serial.print(":");
Serial.println(second);
min1=minute; // min1 contains minutes
hr1=hour;
y=min1/10; // First digit of minute
x=min1-(y*10); // Second digit of minute
b=hr1/10; // First digit of hour
a=hr1-(b *10); // Second digit of hour
return;
}