Guest User

CLACK

a guest
May 14th, 2014
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.52 KB | None | 0 0
  1. //CLACK
  2.  
  3. #include <DS1307RTC.h>
  4. #include <Time.h>
  5. #include <Wire.h>
  6.  
  7.  
  8. int latchPin = 9;//Pin connected to ST_CP of 74HC595
  9. int clockPin = 12;//Pin connected to SH_CP of 74HC595
  10. int dataPin = 11;////Pin connected to DS of 74HC595
  11. int ONE = 8;
  12. int TWO = 7;
  13. int FOUR = 6;
  14. int EIGHT = 5;
  15. int DP = 4 ;
  16. int button = 3;
  17.  
  18. /////////////0,1,2,3,4,5,6,7,8,9   t,_,_,-,_,_
  19. boolean o[]={0,1,0,1,0,1,0,1,0,1,  0,1,0,1,0,1}; //one
  20. boolean t[]={0,0,1,1,0,0,1,1,0,0,  1,1,0,0,1,1}; //two
  21. boolean f[]={0,0,0,0,1,1,1,1,0,0,  0,0,1,1,1,1}; //four
  22. boolean e[]={0,0,0,0,0,0,0,0,1,1,  1,1,1,1,1,1}; //eight
  23. //////////////////--1--,--2--,--3--,--4--,--5--,--6--,--7--,--8--,--9--,--10-,--11-,--12-,--13-,--14-,--15-,--16-,
  24. 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
  25.  
  26. //------------------- Y,Y,Y,Y,M,M,D,D,H,H,M,M,S,S,s,s
  27. int  fullTime[6]  = { 2000,    1,   1,   0,   0,   0};
  28. //------------------- Y,Y,Y,Y,M,M,D,D,H,H,m,m,S,S,s,s
  29. int  dispTime[16] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
  30. byte Decimals[16] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0 };
  31. int Year; int Month; int Day; int Hour; int Minute; int Second;
  32.  
  33. boolean CENTRUN = false;
  34. boolean CLOCKON = true;
  35. int cents = 0;
  36. boolean SQW = 1;
  37. boolean SQWLast = 1;
  38.  
  39.  
  40. tmElements_t tm; //with this DS3231 library, the values are placed into an object which are acsessed like "tm.Hour"
  41.  
  42.  
  43. void setup() {
  44.   Serial.begin(9600);
  45.   pinMode(button,INPUT);
  46.   pinMode(latchPin, OUTPUT);
  47.   pinMode(clockPin, OUTPUT);
  48.   pinMode(dataPin, OUTPUT);
  49.   pinMode(ONE, OUTPUT);
  50.   pinMode(TWO, OUTPUT);
  51.   pinMode(FOUR, OUTPUT);
  52.   pinMode(EIGHT, OUTPUT);
  53.   pinMode(DP, OUTPUT);
  54.   pinMode(13, OUTPUT);
  55.  
  56.   //----------set control register to output square wave on pin 2 at 1Hz=
  57.   Wire.begin();
  58.   //setup square wavew at choice of 0=1Hz/8=1024Hz/16=4096Hz/24=8192Hz
  59.   Wire.beginTransmission(104);
  60.   //select control register
  61.   Wire.write(0x0e);//one of the scaler registers on the DS3231
  62.   Wire.write(0);//set square wave @ 1 Hz
  63.   pinMode(2,INPUT);//Set Arduino Pullup HIGH
  64.   digitalWrite(2,HIGH);
  65.  
  66.   blankall();
  67.  
  68.   CLOCKON = true;
  69.  
  70. }
  71.  
  72. void loop() {
  73.   if(CLOCKON){
  74.    
  75.     if(CENTRUN){
  76.       cents++;
  77.       writeNumber(cents%10, Decimals[15], 15);
  78.       writeNumber((cents/10)%10, Decimals[14], 14);  
  79.       if(cents == 99){CENTRUN = false;}
  80.       delayMicroseconds(9200);
  81.     }
  82.    
  83.     SQW = digitalRead(2);
  84.    
  85.     if((SQW == 0) && (SQWLast == 1)){
  86.       MainTime();
  87.       CENTRUN = true;
  88.       Serial.println(cents);
  89.       cents = 0;
  90.     }
  91.    
  92.     SQWLast = SQW;
  93.    
  94.   }
  95.   //---a button to set the time when the clock strikes 11:00
  96.   if(digitalRead(button)){
  97.     //set a time into the "tmElements_t tm" object
  98.     tm.Year = (2014 - 1970); //tm.Year is stored in years since 1970
  99.     tm.Month = 5;
  100.     tm.Day = 9;
  101.     tm.Hour = 23;
  102.     tm.Minute = 00;
  103.     tm.Second = 00;
  104.     RTC.write(tm);//to DS3231  
  105.   }
  106. }
  107.  
  108.  
  109. void MainTime(){
  110.   if(CLOCKON){
  111.     if (RTC.read(tm)) {   //the RTC is polled and its valus placed into tm, also it ruturns true if it works
  112.       fullTime[0] = (tmYearToCalendar(tm.Year));
  113.       fullTime[1] = tm.Month;
  114.       fullTime[2] = tm.Day;
  115.       fullTime[3] = tm.Hour;
  116.       fullTime[4] = tm.Minute;
  117.       fullTime[5] = tm.Second;
  118.       Serial.println(tm.Year);
  119.     }
  120.   displayTime();
  121.   }
  122. }
  123.  
  124.  
  125. void displayTime(){
  126.  
  127.   Year = fullTime[0];
  128.   dispTime[3] = (Year % 10);
  129.   Year = (Year / 10);
  130.   dispTime[2] = (Year % 10);
  131.   Year = (Year / 10);
  132.   dispTime[1] = (Year % 10);
  133.   Year = (Year / 10);
  134.   dispTime[0] = (Year % 10);
  135.  
  136.   Month = fullTime[1];
  137.   dispTime[5] = (Month % 10);
  138.   Month = (Month / 10);
  139.   dispTime[4] = (Month % 10);
  140.  
  141.   Day = fullTime[2];
  142.   dispTime[7] = (Day % 10);
  143.   Day = (Day / 10);
  144.   dispTime[6] = (Day % 10);
  145.  
  146.   Hour = fullTime[3];
  147.   dispTime[9] = (Hour % 10);
  148.   Hour = (Hour / 10);
  149.   dispTime[8] = (Hour % 10);
  150.  
  151.   Minute = fullTime[4];
  152.   dispTime[11] = (Minute % 10);
  153.   Minute = (Minute / 10);
  154.   dispTime[10] = (Minute % 10);
  155.  
  156.   Second = fullTime[5];
  157.   dispTime[13] = (Second % 10);
  158.   Second = (Second / 10);
  159.   dispTime[12] = (Second % 10);
  160.  
  161.   //print the array from display 0-13, 14 & 15 are used by the centiseconds and are not apart of the main time display
  162.   for(int m = 0; m <= 13; m++){
  163.     writeNumber(dispTime[m], Decimals[m], m);
  164.   }
  165.  
  166. }
  167.  
  168. void blankall(){
  169.  digitalWrite(ONE, o[11]);
  170.  digitalWrite(TWO, t[11]);
  171.  digitalWrite(FOUR, f[11]);
  172.  digitalWrite(EIGHT, e[11]);
  173.  Shift(0);//all displayes enabled for blanking
  174. }
  175.  
  176. void blank(int a){
  177.  digitalWrite(ONE, o[11]);
  178.  digitalWrite(TWO, t[11]);
  179.  digitalWrite(FOUR, f[11]);
  180.  digitalWrite(EIGHT, e[11]);
  181.   Shift(Displays[a]);
  182. }
  183.  
  184. void writeNumber(int a, int dp,int disp){
  185.  selectAndShift(disp);
  186.  if(dp == 1){digitalWrite(DP, LOW);}
  187.  else if(dp == 0){digitalWrite(DP, HIGH);}
  188.  digitalWrite(ONE, o[a]);
  189.  digitalWrite(TWO, t[a]);
  190.  digitalWrite(FOUR, f[a]);
  191.  digitalWrite(EIGHT, e[a]);
  192. }
  193.  
  194. void selectAndShift(int displayNum){
  195.   digitalWrite(latchPin, LOW);
  196.   shiftOut(dataPin, clockPin, MSBFIRST, highByte(Displays[displayNum]));
  197.   shiftOut(dataPin, clockPin, MSBFIRST, lowByte(Displays[displayNum]));
  198.   digitalWrite(latchPin, HIGH);
  199.   digitalWrite(latchPin, LOW);
  200. }
  201.  
  202. void Shift(int Num){
  203.   digitalWrite(latchPin, LOW);
  204.   shiftOut(dataPin, clockPin, MSBFIRST, highByte(Num));
  205.   shiftOut(dataPin, clockPin, MSBFIRST, lowByte(Num));
  206.   digitalWrite(latchPin, HIGH);
  207.   digitalWrite(latchPin, LOW);
  208. }
Advertisement
Add Comment
Please, Sign In to add comment