ThePhilleBoy

7 segment clock/thermometer

May 1st, 2012
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. /*
  2. B number order
  3.  
  4. --3--
  5. 2 | | 4
  6. |--1--|
  7. 8 | | 6
  8. --7--o = 5
  9.  
  10. */
  11. float temperature = 0, kelvin = 0;
  12. int sensorPin = 0;
  13. #include "Wire.h"
  14. #define DS1307_ADDRESS 0x68
  15. byte zero = 0x00; //workaround for issue #527
  16. const byte symbols[3] = {
  17. B00000000, B10000000, B11110000
  18. };
  19.  
  20. const byte ledCharSet[10] =
  21. {
  22. B01110111, B00010100, B10110011, B10110110, B11010100,
  23. B11100110, B11100111, B00110100, B11110111, B11110110
  24. };
  25. const byte ledCharSetDot[10] =
  26. {
  27. B01111111, B00011100, B10111011, B10111110, B11011100,
  28. B11101110, B11101111, B00111100, B11111111, B11111110
  29. };
  30.  
  31. const int latchPin = 8;
  32. const int clockPin = 12;
  33. const int dataPin = 11;
  34.  
  35. void setup() {
  36. Wire.begin();
  37. setDateTime(); //MUST CONFIGURE IN FUNCTION
  38. pinMode(latchPin, OUTPUT);
  39. pinMode(clockPin, OUTPUT);
  40. pinMode(dataPin, OUTPUT);
  41. pinMode(sensorPin, INPUT);
  42. pinMode(10, OUTPUT);
  43.  
  44. }
  45.  
  46. void loop() {
  47. Wire.beginTransmission(DS1307_ADDRESS);
  48. Wire.write(zero);
  49. Wire.endTransmission();
  50. Wire.requestFrom(DS1307_ADDRESS, 7);
  51. int second = bcdToDec(Wire.read());
  52. int minute = bcdToDec(Wire.read());
  53. int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
  54. int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
  55. int monthDay = bcdToDec(Wire.read());
  56. int month = bcdToDec(Wire.read());
  57. int year = bcdToDec(Wire.read());
  58.  
  59. if((second/10)%2 == 1) {
  60. DisplayNumber();
  61. }
  62.  
  63. if((second/10)%2 == 0) {
  64. kelvin = analogRead(sensorPin)*0.004882812*100;
  65. temperature = kelvin - 12.5 - 273.15;
  66. displayTemp(temperature);
  67. delay(10000);
  68. }
  69. }
  70.  
  71.  
  72. void DisplayNumber() {
  73. Wire.beginTransmission(DS1307_ADDRESS);
  74. Wire.write(zero);
  75. Wire.endTransmission();
  76. Wire.requestFrom(DS1307_ADDRESS, 7);
  77. int second = bcdToDec(Wire.read());
  78. int minute = bcdToDec(Wire.read());
  79. int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
  80. int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
  81. int monthDay = bcdToDec(Wire.read());
  82. int month = bcdToDec(Wire.read());
  83. int year = bcdToDec(Wire.read());
  84. digitalWrite(latchPin, 0);
  85. shiftOut(dataPin, clockPin, MSBFIRST, ledCharSet[minute%10]);
  86. shiftOut(dataPin, clockPin, MSBFIRST, ledCharSet[minute/10]);
  87. shiftOut(dataPin, clockPin, MSBFIRST, ledCharSetDot[hour%10]);
  88. shiftOut(dataPin, clockPin, MSBFIRST, ledCharSet[hour/10]);
  89. digitalWrite(latchPin, 1);
  90. }
  91.  
  92. void displayTemp(int num) {
  93.  
  94. if (num < -10) {
  95. num = num*(-1);
  96. digitalWrite(latchPin, 0);
  97. shiftOut(dataPin, clockPin, MSBFIRST, symbols[0]);
  98. shiftOut(dataPin, clockPin, MSBFIRST, symbols[2]);
  99. shiftOut(dataPin, clockPin, MSBFIRST, ledCharSetDot[num%10]);
  100. shiftOut(dataPin, clockPin, MSBFIRST, ledCharSet[(num%100)/10]);
  101. shiftOut(dataPin, clockPin, MSBFIRST, symbols[1]);
  102. digitalWrite(latchPin, 1);
  103. }
  104.  
  105. if (num < 0 && num > -10) {
  106. num = num*(-1);
  107. digitalWrite(latchPin, 0);
  108.  
  109. shiftOut(dataPin, clockPin, MSBFIRST, symbols[0]);
  110. shiftOut(dataPin, clockPin, MSBFIRST, symbols[2]);
  111. shiftOut(dataPin, clockPin, MSBFIRST, ledCharSet[(num%1)*10]);
  112. shiftOut(dataPin, clockPin, MSBFIRST, ledCharSetDot[num%10]);
  113. shiftOut(dataPin, clockPin, MSBFIRST, symbols[1]);
  114. digitalWrite(latchPin, 1);
  115. }
  116.  
  117. if (num < 10 && num >= 0) {
  118. digitalWrite(latchPin, 0);
  119. shiftOut(dataPin, clockPin, MSBFIRST, symbols[0]);
  120. shiftOut(dataPin, clockPin, MSBFIRST, symbols[2]);
  121. shiftOut(dataPin, clockPin, MSBFIRST, ledCharSet[num%10]);
  122. shiftOut(dataPin, clockPin, MSBFIRST, symbols[0]);
  123. shiftOut(dataPin, clockPin, MSBFIRST, symbols[0]);
  124. digitalWrite(latchPin, 1);
  125. }
  126.  
  127. if (num >= 10) {
  128. digitalWrite(latchPin, 0);
  129. shiftOut(dataPin, clockPin, MSBFIRST, symbols[0]);
  130. shiftOut(dataPin, clockPin, MSBFIRST, symbols[2]);
  131. shiftOut(dataPin, clockPin, MSBFIRST, ledCharSet[num%10]);
  132. shiftOut(dataPin, clockPin, MSBFIRST, ledCharSet[(num%100)/10]);
  133. shiftOut(dataPin, clockPin, MSBFIRST, symbols[0]);
  134. digitalWrite(latchPin, 1);
  135. }
  136. }
  137.  
  138. void setDateTime(){
  139.  
  140. byte second = 00; //0-59
  141. byte minute = 18; //0-59
  142. byte hour = 12; //0-23
  143. byte weekDay = 3; //1-7
  144. byte monthDay = 25; //1-31
  145. byte month = 4; //1-12
  146. byte year = 12; //0-99
  147.  
  148. Wire.beginTransmission(DS1307_ADDRESS);
  149. Wire.write(zero); //stop Oscillator
  150.  
  151. Wire.write(decToBcd(second));
  152. Wire.write(decToBcd(minute));
  153. Wire.write(decToBcd(hour));
  154. Wire.write(decToBcd(weekDay));
  155. Wire.write(decToBcd(monthDay));
  156. Wire.write(decToBcd(month));
  157. Wire.write(decToBcd(year));
  158.  
  159. Wire.write(zero); //start
  160.  
  161. Wire.endTransmission();
  162.  
  163. }
  164.  
  165. byte decToBcd(byte val){
  166. // Convert normal decimal numbers to binary coded decimal
  167. return ( (val/10*16) + (val%10) );
  168. }
  169.  
  170. byte bcdToDec(byte val) {
  171. // Convert binary coded decimal to normal decimal numbers
  172. return ( (val/16*10) + (val%16) );
  173. }
Advertisement
Add Comment
Please, Sign In to add comment