Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. #include <Wire.h>
  2.  
  3. #include <LiquidCrystal_I2C.h>
  4. LiquidCrystal_I2C lcd (0X27, 24, 2);
  5.  
  6. byte hours = 15, minutes = 17, seconds = 5;
  7. byte days = 3, months = 5, years = 19;
  8.  
  9. byte bcdToDec(byte val){
  10. val = val / 16 * 10 + val % 16;
  11. return val;
  12. }
  13.  
  14. byte decToBcd(byte val){
  15. val = val / 10 * 16 + val % 10;
  16. return val;
  17. }
  18.  
  19. void setupTime(int second, int minute, int hour){
  20. Wire.beginTransmission(0x68);
  21. Wire.write(0x0);
  22. Wire.write(decToBcd(second));
  23. Wire.write(decToBcd(minute));
  24. Wire.write(decToBcd(hour));
  25. Wire.endTransmission();
  26. }
  27.  
  28. void readTime(){
  29. Wire.beginTransmission(0x68);
  30. Wire.write(0x0);
  31. Wire.endTransmission();
  32. Wire.requestFrom(0x68, 3);
  33. if(Wire.available()){
  34. seconds = bcdToDec(Wire.read());
  35. minutes = bcdToDec(Wire.read());
  36. hours = bcdToDec(Wire.read() & 0b111111);
  37. }
  38. }
  39.  
  40. void printTime(){
  41. readTime();
  42.  
  43. lcd.setCursor(0, 0);
  44. Serial.print(hours / 10);
  45. lcd.setCursor(1, 0);
  46. Serial.print(hours % 10);
  47. lcd.setCursor(2, 0);
  48. Serial.print(":");
  49. lcd.setCursor(3, 0);
  50. Serial.print(minutes / 10);
  51. lcd.setCursor(4, 0);
  52. Serial.print(minutes % 10);
  53. }
  54.  
  55. void setupDate(int day, int month, int year) {
  56. Wire.beginTransmission(0x68);
  57. Wire.write(0x3);
  58. Wire.write(0);
  59. Wire.write(decToBcd(day));
  60. Wire.write(decToBcd(month));
  61. Wire.write(decToBcd(year));
  62. Wire.endTransmission();
  63. }
  64.  
  65. void readDate(){
  66. Wire.beginTransmission(0x68);
  67. Wire.write(3);
  68. Wire.endTransmission();
  69. Wire.requestFrom(0x68,4);
  70. if(Wire.available()){
  71. bcdToDec(Wire.read());
  72. days = bcdToDec(Wire.read());
  73. months = bcdToDec(Wire.read());
  74. years = bcdToDec(Wire.read());
  75. }
  76. }
  77.  
  78. void printDate(){
  79. readDate();
  80. lcd.setCursor(6, 0);
  81. Serial.print(dateArray[0] / 10);
  82. lcd.setCursor(7, 0);
  83. Serial.print(dateArray[0] % 10);
  84. lcd.setCursor(8, 0);
  85. Serial.print("/");
  86. lcd.setCursor(9, 0);
  87. Serial.print(dateArray[1] / 10);
  88. lcd.setCursor(10, 0);
  89. Serial.print(dateArray[1] % 10);
  90. lcd.setCursor(11, 0);
  91. Serial.print("/");
  92. lcd.setCursor(12, 0);
  93. Serial.print(dateArray[2] / 10);
  94. lcd.setCursor(13, 0);
  95. Serial.print(dateArray[2] % 10);
  96. }
  97.  
  98. void setup() {
  99. Serial.begin(9600);
  100.  
  101. Wire.begin();
  102.  
  103. lcd.begin();
  104. lcd.backlight();
  105.  
  106. setupTime(seconds, minutes, hours);
  107. setupDate(days, months, years);
  108. }
  109.  
  110. void loop() {
  111. printTime();
  112. printDate();
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement