/* Arduino uses a DS1307 RTC and a MAX7219 7-segment display driver to operate as a clock displaying hours, minutes, and seconds. The hours-minutes can be set by three http://www.bristolwatch.com/arduino/arduino_max7219b.htm Arduino RTC Clock with MAX7219 8-Digit LED Display switches SW0-SW2. Author: Lewis Loflin lewis@bvu.net http://www.bristolwatch.com */ #include // specify use of Wire.h library. #define blinkPin 13 #define ONE_HZ_SW 2 // one Hz square wave from Ds1307 #define SW0 4 // set time #define SW1 5 // minutes #define SW2 6 // hours #define RB5 7 // CS #define RB6 8 // DATA #define RB7 9 // CLK byte temp; void setup() { Wire.begin(); // join i2c bus (address optional for master) Serial.begin(9600); digitalWrite(blinkPin, 0); pinMode(ONE_HZ_SW, INPUT); pinMode(SW0, INPUT); // for this use a slide switch pinMode(SW1, INPUT); // N.O. push button switch pinMode(SW2, INPUT); // N.O. push button switch pinMode(RB5, OUTPUT); pinMode(RB6, OUTPUT); pinMode(RB7, OUTPUT); // pull-ups on digitalWrite(SW0, HIGH); digitalWrite(SW1, HIGH); digitalWrite(SW2, HIGH); init_MAX7219(); // setup MAX7219 } void loop() { // wait for HIGH on Dp2 while (!digitalRead(ONE_HZ_SW)) { } Wire.beginTransmission(0x68); Wire.write(0); // location pointer Wire.endTransmission(); Wire.requestFrom(0x68, 7); // read 7 bytes byte secs = Wire.read(); byte mins = Wire.read(); byte hrs = Wire.read(); byte day = Wire.read(); byte date = Wire.read(); byte month = Wire.read(); byte year = Wire.read(); // hours, minutes, seconds hrs = hrs & 0x3F; if (hrs <= 0x09) { writeMAX7219(8, 0x00); // mins 10s writeMAX7219(7, hrs); // mins units } if (hrs > 9) { temp = hrs & 0x0F; writeMAX7219(7, temp); // mins units temp = hrs / 16 ; // shift 4 places right writeMAX7219(8, temp); // mins 10s } writeMAX7219(6, 10); // type '-' if (mins <= 0x09) { writeMAX7219(5, 0x00); // mins 10s writeMAX7219(4, mins); // mins units } if (mins > 9) { temp = mins & 0x0F; writeMAX7219(4, temp); // mins units temp = mins / 16 ; // shift 4 places right writeMAX7219(5, temp); // mins 10s } writeMAX7219(3, 10); // type '-' secs = secs & 0x7F; // clear bit 7 if (secs <= 0x09) { writeMAX7219(2, 0x00); // seconds 10s writeMAX7219(1, secs); // seconds units } if (secs > 9) { temp = secs & 0x0F; writeMAX7219(1, temp); // seconds units temp = secs / 16 ; // shift 4 places right writeMAX7219(2, temp); // seconds 10s } if (!(digitalRead(SW0))) set_time(); // set time while (digitalRead(ONE_HZ_SW)) { } // wait or LOW toggle(blinkPin); // 2 HZ } // toggle the state on a pin void toggle(int pinNum) { int pinState = digitalRead(pinNum); pinState = !pinState; digitalWrite(pinNum, pinState); } void set_time() { byte minutes = 0; byte hours = 0; while (!digitalRead(SW0)) // set time switch must be released to exit { while (!digitalRead(SW1)) // set minutes { minutes++; if ((minutes & 0x0f) > 9) minutes = minutes + 6; if (minutes > 0x59) minutes = 0; // minutes routine if (minutes <= 0x09) { writeMAX7219(5, 0x00); // mins 10s writeMAX7219(4, minutes); // mins units } if (minutes > 9) { temp = minutes & 0x0F; writeMAX7219(4, temp); // mins units temp = minutes / 16 ; // shift 4 places right writeMAX7219(5, temp); // mins 10s } delay(750); } while (!digitalRead(SW2)) // set hours { hours++; if ((hours & 0x0f) > 9) hours = hours + 6; if (hours > 0x23) hours = 0; // hours routine if (hours <= 0x09) { writeMAX7219(8, 0x00); // mins 10s writeMAX7219(7, hours); // mins units } if (hours > 9) { temp = hours & 0x0F; writeMAX7219(7, temp); // hours units temp = hours / 16 ; // shift 4 places right writeMAX7219(8, temp); // hours 10s } delay(750); } Wire.beginTransmission(0x68); // activate Ds1307 Wire.write(0); // where to begin Wire.write(0x00); //seconds Wire.write(minutes); //minutes Wire.write(0x80 | hours); //hours (24hr time) Wire.write(0x06); // Day 01-07 Wire.write(0x16); // Date 0-31 Wire.write(0x11); // month 0-12 Wire.write(0x13); // Year 00-99 Wire.write(0x10); // Control 0x10 produces a 1 HZ square wave on pin 7. Wire.endTransmission(); } } // shift data to MAX7219 // RB7 -> CLK, RB6 -> DATA, RB5 -> CS not void ssrOut(unsigned char val) { int j; for (j = 1; j <= 8; j++) { // shift out MSB first unsigned char temp = val & 0x80; // MSB out first if (temp == 0x80) digitalWrite(RB6, 1); // Rb6 DATA else digitalWrite(RB6, 0); digitalWrite(RB7, 1); delayMicroseconds(20); digitalWrite(RB7, 0); val = val << 1; // shift one place left } // next j } void pulseCS(void) { digitalWrite(RB5, 1); delay(1); digitalWrite(RB5, 0); } void init_MAX7219(void) { digitalWrite(RB5, 0); // CS NOT // set decode mode ssrOut(0x09); // address // ssrOut(0x00); // no decode ssrOut(0xFF); // 4-bit BCD decode eight digits pulseCS(); // set intensity ssrOut(0x0A); // address ssrOut(0x0D); // 5/32s pulseCS(); // set scan limit 0-7 ssrOut(0x0B); // address // ssrOut(0x07); // 8 digits ssrOut(0x07); // 4 digits pulseCS(); // clear MAX7219 for (int i = 1; i <= 8; i++) { ssrOut(i); ssrOut(0x00); pulseCS(); } // set for normal operation ssrOut(0x0C); // address // ssrOut(0x00); // Off ssrOut(0x01); // On pulseCS(); } void writeMAX7219(char address, char data) { ssrOut(address); // valid numbers 1-8 ssrOut(data); pulseCS(); }