In: Electrical Engineering
Design a clock display to show the time in hours, minutes, and seconds. Assume that we have a clock of exactly 1 KHz (1000 clock pulses per second). It will use 6 seven-segment displays and operate either in military time (hours 00 to 23) or regular time (1 to 12, with AM and PM). An input line, x, differentiates between the two. A seventh display is used to show A or P in the latter case; it is blank otherwise. Assume that there is a BCD-to-seven-segment decoder driver available; one is needed for each display other than the AM /PM one.
#include <Wire.h>
#include <DS3231.h>
#define latchPin 5
#define clockPin 6
#define dataPin 4
#define dot 2
DS3231 RTC;
int h;
int m;
int thousands;
int hundreds;
int tens;
int unit;
bool h24;
bool PM;
void setup ()
{
Wire.begin();
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(dot,OUTPUT);
}
void loop ()
{
digitalWrite(dot,HIGH);
int h= RTC.getHour(h24, PM);
int m = RTC.getMinute();
int number = h*100+m;
int thousands = number/1000;
int hundreds = number/100;
int tens = number/10;
int unit = number;
int t= unit;
int u= tens;
int v= hundreds;
int w= thousands;
switch (t)
{
case 0:
unit = 63;
break;
case 1:
unit = 06;
break;
case 2:
unit =91;
break;
case 3:
unit=79;
break;
case 4:
unit=102;
break;
case 5:
unit = 109;
break;
case 6:
unit =125;
case 7:
unit = 07;
break;
case 8:
unit = 127;
break;
case 9:
unit =103;
break;
}
switch (u)
{
case 0:
tens = 63;
break;
case 1:
tens = 06;
break;
case 2:
tens =91;
break;
case 3:
tens=79;
break;
case 4:
tens=102;
break;
case 5:
tens= 109;
break;
case 6:
tens =125;
case 7:
tens = 07;
break;
case 8:
tens = 127;
break;
case 9:
tens =103;
break;
}
switch (v)
{
case 0:
hundreds = 63;
break;
case 1:
hundreds = 06;
break;
case 2:
hundreds =91;
break;
case 3:
hundreds=79;
break;
case 4:
hundreds=102;
break;
case 5:
hundreds = 109;
break;
case 6:
hundreds =125;
case 7:
hundreds = 07;
break;
case 8:
hundreds = 127;
break;
case 9:
hundreds =103;
break;
}
switch (w)
{
case 0:
thousands = 63;
break;
case 1:
thousands = 06;
break;
case 2:
thousands =91;
break;
case 3:
thousands=79;
break;
case 4:
thousands=102;
break;
case 5:
thousands = 109;
break;
case 6:
thousands =125;
case 7:
thousands = 07;
break;
case 8:
thousands= 127;
break;
case 9:
thousands =103;
break;
}
digitalWrite(9, LOW);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST,thousands);
digitalWrite(latchPin, HIGH);
digitalWrite(9, HIGH);
delay(5);
digitalWrite(10, LOW);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST,hundreds );
digitalWrite(latchPin, HIGH);
digitalWrite(10, HIGH);
delay(5);
digitalWrite(11, LOW);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST,tens);
digitalWrite(latchPin, HIGH);
digitalWrite(11, HIGH);
delay(5);
digitalWrite(12, LOW);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST,unit);
digitalWrite(latchPin, HIGH);
digitalWrite(12, HIGH);
delay(5);
}