Skip to content

Instantly share code, notes, and snippets.

@PhirePhly
Created January 7, 2010 08:15
Show Gist options
  • Save PhirePhly/271081 to your computer and use it in GitHub Desktop.
Save PhirePhly/271081 to your computer and use it in GitHub Desktop.
// 2009 Kenneth Finnegan
// kennethfinnegan.blogspot.com
// http://kennethfinnegan.blogspot.com/2010/01/arduino-binary-clock.html
#include <Wire.h>
#define DS1307 B1101000
#define LEDDATA 7
#define LEDCLOCK 8
#define LEDLATCH 9
#define LEDAMPM 13
#define MINUTEBUTTON 2
#define HOURBUTTON 3
byte second=0, minute=0, hour=0;
void updatedisplay();
void updatetime();
byte DECTOBCD(byte val);
byte BCDTODEC(byte val);
void setup() {
Wire.begin();
pinMode(LEDDATA, OUTPUT);
pinMode(LEDCLOCK, OUTPUT);
pinMode(LEDLATCH, OUTPUT);
pinMode(LEDAMPM, OUTPUT);
digitalWrite(LEDLATCH, HIGH);
pinMode(MINUTEBUTTON, INPUT);
pinMode(HOURBUTTON, INPUT);
digitalWrite(MINUTEBUTTON, HIGH); // activate pullup R
digitalWrite(HOURBUTTON, HIGH); // activate pullup R
}
void loop() {
updatedisplay();
if (digitalRead(MINUTEBUTTON) == LOW) {
minute = (minute+1) % 60;
// Set seconds to zero so that the clock stop
// flag gets upset on the DS1307. It's also nice
// to have the seconds zero out to get the time
// setting exact.
second = 0;
updatetime();
updatedisplay();
delay(300);
}
if (digitalRead(HOURBUTTON) == LOW) {
hour = (hour + 1) % 24;
updatetime();
updatedisplay();
delay(300);
}
}
void updatedisplay() {
Wire.beginTransmission(DS1307);
Wire.send(0x00);
Wire.endTransmission();
Wire.requestFrom(DS1307, 3);
second = BCDTODEC(Wire.receive() & 0x7f);
minute = BCDTODEC(Wire.receive());
hour = BCDTODEC(Wire.receive() & 0x3f);
byte temphour = hour % 12;
if (temphour == 0) {
// Not zero, not one, but 12. What the F, world?
temphour = 12;
}
int leds = second + (minute<<6) + (temphour<<12);
digitalWrite(LEDLATCH, LOW); // lock display before updating
// Arduino gives us a nice shiftOut function for shift registers
shiftOut(LEDDATA, LEDCLOCK, MSBFIRST, highByte(leds));
shiftOut(LEDDATA, LEDCLOCK, MSBFIRST, lowByte(leds));
digitalWrite(LEDLATCH, HIGH);
digitalWrite(LEDAMPM, hour/12);
}
void updatetime() {
// Send the time back to the DS1307 when the user
// presses a button to advance the hours or minutes
Wire.beginTransmission(DS1307);
Wire.send(0x00);
Wire.send(DECTOBCD(second));
Wire.send(DECTOBCD(minute));
Wire.send(DECTOBCD(hour));
Wire.endTransmission();
}
byte DECTOBCD(byte val) {
return ((val/10)<<4) + (val%10);
}
byte BCDTODEC(byte val) {
return (val>>4) * 10 + (val & 0xf);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment