Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //CLACK
- #include <DS1307RTC.h>
- #include <Time.h>
- #include <Wire.h>
- int latchPin = 9;//Pin connected to ST_CP of 74HC595
- int clockPin = 12;//Pin connected to SH_CP of 74HC595
- int dataPin = 11;////Pin connected to DS of 74HC595
- int ONE = 8;
- int TWO = 7;
- int FOUR = 6;
- int EIGHT = 5;
- int DP = 4 ;
- int button = 3;
- /////////////0,1,2,3,4,5,6,7,8,9 t,_,_,-,_,_
- boolean o[]={0,1,0,1,0,1,0,1,0,1, 0,1,0,1,0,1}; //one
- boolean t[]={0,0,1,1,0,0,1,1,0,0, 1,1,0,0,1,1}; //two
- boolean f[]={0,0,0,0,1,1,1,1,0,0, 0,0,1,1,1,1}; //four
- boolean e[]={0,0,0,0,0,0,0,0,1,1, 1,1,1,1,1,1}; //eight
- //////////////////--1--,--2--,--3--,--4--,--5--,--6--,--7--,--8--,--9--,--10-,--11-,--12-,--13-,--14-,--15-,--16-,
- int Displays[] = {65534,65533,65531,65527,65519,65503,65471,65407,65279,65023,64511,63487,61439,57343,49151,32767}; //the numbers that give a 16bit binary number with only one LOW bit
- //------------------- Y,Y,Y,Y,M,M,D,D,H,H,M,M,S,S,s,s
- int fullTime[6] = { 2000, 1, 1, 0, 0, 0};
- //------------------- Y,Y,Y,Y,M,M,D,D,H,H,m,m,S,S,s,s
- int dispTime[16] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
- byte Decimals[16] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0 };
- int Year; int Month; int Day; int Hour; int Minute; int Second;
- boolean CENTRUN = false;
- boolean CLOCKON = true;
- int cents = 0;
- boolean SQW = 1;
- boolean SQWLast = 1;
- tmElements_t tm; //with this DS3231 library, the values are placed into an object which are acsessed like "tm.Hour"
- void setup() {
- Serial.begin(9600);
- pinMode(button,INPUT);
- pinMode(latchPin, OUTPUT);
- pinMode(clockPin, OUTPUT);
- pinMode(dataPin, OUTPUT);
- pinMode(ONE, OUTPUT);
- pinMode(TWO, OUTPUT);
- pinMode(FOUR, OUTPUT);
- pinMode(EIGHT, OUTPUT);
- pinMode(DP, OUTPUT);
- pinMode(13, OUTPUT);
- //----------set control register to output square wave on pin 2 at 1Hz=
- Wire.begin();
- //setup square wavew at choice of 0=1Hz/8=1024Hz/16=4096Hz/24=8192Hz
- Wire.beginTransmission(104);
- //select control register
- Wire.write(0x0e);//one of the scaler registers on the DS3231
- Wire.write(0);//set square wave @ 1 Hz
- pinMode(2,INPUT);//Set Arduino Pullup HIGH
- digitalWrite(2,HIGH);
- blankall();
- CLOCKON = true;
- }
- void loop() {
- if(CLOCKON){
- if(CENTRUN){
- cents++;
- writeNumber(cents%10, Decimals[15], 15);
- writeNumber((cents/10)%10, Decimals[14], 14);
- if(cents == 99){CENTRUN = false;}
- delayMicroseconds(9200);
- }
- SQW = digitalRead(2);
- if((SQW == 0) && (SQWLast == 1)){
- MainTime();
- CENTRUN = true;
- Serial.println(cents);
- cents = 0;
- }
- SQWLast = SQW;
- }
- //---a button to set the time when the clock strikes 11:00
- if(digitalRead(button)){
- //set a time into the "tmElements_t tm" object
- tm.Year = (2014 - 1970); //tm.Year is stored in years since 1970
- tm.Month = 5;
- tm.Day = 9;
- tm.Hour = 23;
- tm.Minute = 00;
- tm.Second = 00;
- RTC.write(tm);//to DS3231
- }
- }
- void MainTime(){
- if(CLOCKON){
- if (RTC.read(tm)) { //the RTC is polled and its valus placed into tm, also it ruturns true if it works
- fullTime[0] = (tmYearToCalendar(tm.Year));
- fullTime[1] = tm.Month;
- fullTime[2] = tm.Day;
- fullTime[3] = tm.Hour;
- fullTime[4] = tm.Minute;
- fullTime[5] = tm.Second;
- Serial.println(tm.Year);
- }
- displayTime();
- }
- }
- void displayTime(){
- Year = fullTime[0];
- dispTime[3] = (Year % 10);
- Year = (Year / 10);
- dispTime[2] = (Year % 10);
- Year = (Year / 10);
- dispTime[1] = (Year % 10);
- Year = (Year / 10);
- dispTime[0] = (Year % 10);
- Month = fullTime[1];
- dispTime[5] = (Month % 10);
- Month = (Month / 10);
- dispTime[4] = (Month % 10);
- Day = fullTime[2];
- dispTime[7] = (Day % 10);
- Day = (Day / 10);
- dispTime[6] = (Day % 10);
- Hour = fullTime[3];
- dispTime[9] = (Hour % 10);
- Hour = (Hour / 10);
- dispTime[8] = (Hour % 10);
- Minute = fullTime[4];
- dispTime[11] = (Minute % 10);
- Minute = (Minute / 10);
- dispTime[10] = (Minute % 10);
- Second = fullTime[5];
- dispTime[13] = (Second % 10);
- Second = (Second / 10);
- dispTime[12] = (Second % 10);
- //print the array from display 0-13, 14 & 15 are used by the centiseconds and are not apart of the main time display
- for(int m = 0; m <= 13; m++){
- writeNumber(dispTime[m], Decimals[m], m);
- }
- }
- void blankall(){
- digitalWrite(ONE, o[11]);
- digitalWrite(TWO, t[11]);
- digitalWrite(FOUR, f[11]);
- digitalWrite(EIGHT, e[11]);
- Shift(0);//all displayes enabled for blanking
- }
- void blank(int a){
- digitalWrite(ONE, o[11]);
- digitalWrite(TWO, t[11]);
- digitalWrite(FOUR, f[11]);
- digitalWrite(EIGHT, e[11]);
- Shift(Displays[a]);
- }
- void writeNumber(int a, int dp,int disp){
- selectAndShift(disp);
- if(dp == 1){digitalWrite(DP, LOW);}
- else if(dp == 0){digitalWrite(DP, HIGH);}
- digitalWrite(ONE, o[a]);
- digitalWrite(TWO, t[a]);
- digitalWrite(FOUR, f[a]);
- digitalWrite(EIGHT, e[a]);
- }
- void selectAndShift(int displayNum){
- digitalWrite(latchPin, LOW);
- shiftOut(dataPin, clockPin, MSBFIRST, highByte(Displays[displayNum]));
- shiftOut(dataPin, clockPin, MSBFIRST, lowByte(Displays[displayNum]));
- digitalWrite(latchPin, HIGH);
- digitalWrite(latchPin, LOW);
- }
- void Shift(int Num){
- digitalWrite(latchPin, LOW);
- shiftOut(dataPin, clockPin, MSBFIRST, highByte(Num));
- shiftOut(dataPin, clockPin, MSBFIRST, lowByte(Num));
- digitalWrite(latchPin, HIGH);
- digitalWrite(latchPin, LOW);
- }
Advertisement
Add Comment
Please, Sign In to add comment