Skip to content

Instantly share code, notes, and snippets.

@PhirePhly
Created December 1, 2009 02:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PhirePhly/245992 to your computer and use it in GitHub Desktop.
Save PhirePhly/245992 to your computer and use it in GitHub Desktop.
// 2009 Kenneth Finnegan
// kennethfinnegan.blogspot.com
#include <Wire.h>
// I2C address
#define DS1307 B1101000
// pinout for the 7 segment display
// Starts top center, clock-wise, then
// center line, then dot
byte segment[] = {
3, 2, 5, 6, 7, 8, 9, 4};
byte second=0, minute=0, hour=0;
void updatetime();
void printtime();
void print7digit(byte number);
byte DECTOBCD(byte val);
byte BCDTODEC(byte val);
void setup() {
byte i;
Wire.begin();
for (i=0; i<8; i++) {
pinMode(segment[i], OUTPUT);
digitalWrite(segment[i], LOW);
}
}
void loop() {
updatetime();
printtime();
}
// Pull current time off the DS1307
void updatetime() {
Wire.beginTransmission(DS1307);
Wire.send(0x00);
Wire.endTransmission();
Wire.requestFrom(DS1307, 3);
// Don't actually use seconds value. Feel free to.
second = BCDTODEC(Wire.receive() & 0x7f);
minute = BCDTODEC(Wire.receive());
hour = BCDTODEC(Wire.receive() & 0x3f);
}
// Flash each digit of the time in turn on the 7 segment
void printtime() {
byte j;
// Convert from 24 hour to 12 hour mode
// Should be done on the DS1307. I'm sorry.
byte temphour = hour % 12;
if (temphour == 0) {
// Not zero based, not one based, but 12 based.
temphour = 12;
}
// Only display highest digit if it's nonzero
if (temphour > 9) {
print7digit(temphour/10);
delay(500); // Repeated magic values in my code? Never!
for (j=0; j<8; j++) {
digitalWrite(segment[j], LOW);
}
delay(150);
}
print7digit(temphour % 10);
// Light the dot on last hour digit
digitalWrite(segment[7], HIGH);
delay(500);
for (j=0; j<8; j++) {
digitalWrite(segment[j], LOW);
}
delay(500);
print7digit(minute/10);
delay(500);
for (j=0; j<8; j++) {
digitalWrite(segment[j], LOW);
}
delay(150);
print7digit(minute%10);
delay(500);
for (j=0; j<8; j++) {
digitalWrite(segment[j], LOW);
}
delay(2000);
}
// Display a single digit on the 7 segment
void print7digit(byte number) {
// Bitfield for digits 0-9
const byte numtable[] = {
B00111111,
B00000110,
B01011011,
B01001111,
B01100110,
B01101101,
B01111101,
B00000111,
B01111111,
B01100111 };
byte litpins = numtable[number];
byte i;
for (i=0; i<8; i++) {
digitalWrite(segment[i], (litpins>>i) & 1);
}
}
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