KRITSADA

POPX2 RTC Show analog Clock on GLCD

Nov 22nd, 2016
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.98 KB | None | 0 0
  1. /*****************************************************
  2.  *
  3.  *   TITLE : REAL TIME CLOCK MCP7940  (DS1307 Compatible)
  4.  *   CATAGORIES : I2C
  5.  *   CREATED DATE : NOVEMBER 22, 2016
  6.  *   AUTHOR : INNOVATIVE EXPERIMENT CO., LTD. (INEX)
  7.  *   WEBSITE : HTTP://WWW.INEX.CO.TH
  8.  *   MCU : POP-X2 witch Graphic LCD
  9.  *   DESCRIPTION : Read date and time on DS1307
  10.  *                 then show clock graphic on GLCD
  11.  *                 and enable oscillator if disabled
  12.  *                 โปรแกรมแสดงเวลาจาก ZX-RTC ที่ใช้ DS1307
  13.  *                 โดยจะแสดงเวลาที่ได้เป็นรูปหน้าปัดนาฬิกาบนจอ GLCD
  14.  *                 ถ้าโมดูลไม่ทำงาน จะกำหนดค่าและให้เริ่มทำงาน
  15.  *
  16.  ******************************************************/
  17.  
  18. #include <popx2.h>
  19. #include <Wire.h>
  20. // Define DS1307 Address
  21. #define DEVICE_ADDRESS 0x6F
  22.  
  23. // Define clock face radius
  24. #define CLOCK_SIZE 60
  25.  
  26. // Declare variable for date and time
  27. int second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  28. int second1;
  29. void setup()
  30. {
  31.   // Initiate I2C
  32.   Wire.begin();
  33.  
  34.   // If you need set date and time command
  35.   setDateTime(00, 07, 15, 3, 22, 11, 16);
  36.  
  37.   // Draw clock face
  38.   initClock();
  39. }
  40.  
  41. void loop()
  42. {
  43.   // Call get date and time function
  44.   getDateTime();
  45.  
  46.   // Draw clock pointers
  47.   UpdateClock();  
  48. }
  49.  
  50. void initClock()
  51. {
  52.   glcdFillCircle((GLCD_WIDTH / 2), (GLCD_HEIGHT  / 2)
  53.            , CLOCK_SIZE, GLCD_WHITE);
  54.   glcdLine(64, 25, 64, 35, GLCD_RED);
  55.   glcdLine(10, 80, 20, 80, GLCD_RED);
  56.   glcdLine(119, 80, 109, 80, GLCD_RED);
  57.   glcdLine(64, 135, 64, 125, GLCD_RED);
  58.   glcdLine(37, 33, 39, 38, GLCD_BLACK);
  59.   glcdLine(91, 33, 89, 38, GLCD_BLACK);
  60.   glcdLine(17, 53, 22, 55, GLCD_BLACK);
  61.   glcdLine(111, 53, 106, 55, GLCD_BLACK);
  62.   glcdLine(22, 105, 17, 107, GLCD_BLACK);
  63.   glcdLine(106, 105, 111, 107, GLCD_BLACK);
  64.   glcdLine(39, 122, 37, 127, GLCD_BLACK);
  65.   glcdLine(89, 122, 91, 127, GLCD_BLACK);
  66. }
  67.  
  68. // Variable to check second value was changed
  69. int tempSec = 0;
  70.  
  71. int xSecond = 0;
  72. int ySecond = 0;
  73. int xMinute = 0;
  74. int yMinute = 0;
  75. float xHour = 0;
  76. float yHour = 0;
  77.  
  78. // Draw clock pointer function
  79. void UpdateClock()
  80. {
  81.   if(tempSec != second) {
  82.     // Delete clock pointer (Replace white line)
  83.     glcdLine((GLCD_WIDTH / 2), (GLCD_HEIGHT  / 2)
  84.              , (GLCD_WIDTH / 2) + xSecond
  85.              , (GLCD_HEIGHT  / 2) + ySecond
  86.              , GLCD_WHITE);
  87.     glcdLine((GLCD_WIDTH / 2), (GLCD_HEIGHT  / 2)
  88.              , (GLCD_WIDTH / 2) + xMinute
  89.              , (GLCD_HEIGHT  / 2) + yMinute
  90.              , GLCD_WHITE);
  91.     glcdLine((GLCD_WIDTH / 2), (GLCD_HEIGHT  / 2)
  92.              , (GLCD_WIDTH / 2) + xHour
  93.              , (GLCD_HEIGHT  / 2) + yHour
  94.              , GLCD_WHITE);
  95.                
  96.     // Calculated x and y coordinate to draw clock pointer
  97.     xSecond = cos(((second * 6) - 90) * DEG_TO_RAD) * 40;
  98.     ySecond = sin(((second * 6) - 90) * DEG_TO_RAD) * 40;
  99.     xMinute = cos(((minute * 6) - 90) * DEG_TO_RAD) * 30;
  100.     yMinute = sin(((minute * 6) - 90) * DEG_TO_RAD) * 30;  
  101.     xHour = cos(((((float)hour + ((float)minute / 60)) * 30) - 90) * DEG_TO_RAD) * 25;
  102.     yHour = sin(((((float)hour + ((float)minute / 60)) * 30) - 90) * DEG_TO_RAD) * 25;  
  103.    
  104.     // Draw clock pointer
  105.     glcdLine((GLCD_WIDTH / 2), (GLCD_HEIGHT  / 2)
  106.              , (GLCD_WIDTH / 2) + xSecond
  107.              , (GLCD_HEIGHT  / 2) + ySecond
  108.              , GLCD_RED);
  109.     glcdLine((GLCD_WIDTH / 2), (GLCD_HEIGHT  / 2)
  110.              , (GLCD_WIDTH / 2) + xMinute
  111.              , (GLCD_HEIGHT  / 2) + yMinute
  112.              , GLCD_BLUE);  
  113.     glcdLine((GLCD_WIDTH / 2), (GLCD_HEIGHT  / 2)
  114.              , (GLCD_WIDTH / 2) + xHour
  115.              , (GLCD_HEIGHT  / 2) + yHour
  116.              , GLCD_BLACK);
  117.     tempSec = second;
  118.   }
  119. }
  120.  
  121. // Convert decimal to BCD function
  122. byte decToBcd(byte val)
  123. {
  124.   return ((val / 10 * 16) + (val % 10));
  125. }
  126.  
  127. // Convert BCD to decimal function
  128. byte bcdToDec(byte val)
  129. {
  130.   return ((val / 16 * 10) + (val % 16));
  131. }
  132.  
  133. // Get date and time function
  134. void getDateTime()
  135. {
  136.   // Begin I2C transmission with DS1307 address
  137.   Wire.beginTransmission(DEVICE_ADDRESS);  
  138.  
  139.   // Send register address
  140.   Wire.write(0x00);
  141.  
  142.   // End I2C trasmission
  143.   Wire.endTransmission();
  144.  
  145.   // Request next 7 register address
  146.   Wire.requestFrom(DEVICE_ADDRESS, 7);
  147.  
  148.   // Get date and time
  149.   second1 = Wire.read();
  150.   minute = bcdToDec(Wire.read());
  151.   hour   = bcdToDec(Wire.read() & 0x3f);
  152.   dayOfWeek = bcdToDec(Wire.read());
  153.   dayOfMonth = bcdToDec(Wire.read());
  154.   month   = bcdToDec(Wire.read());
  155.   year = bcdToDec(Wire.read());
  156.  
  157.   // If DS1307 clock halt (CH) bit is 1
  158.   // (Oscillator is disabled)
  159.   if ((second1&0x80)==0x00){
  160.     Wire.beginTransmission(DEVICE_ADDRESS);  
  161.     Wire.write(0x00);
  162.     Wire.write(second1|0x80);
  163.     Wire.endTransmission();
  164.   }  
  165.   second=bcdToDec(second1 &0x7F);
  166. }
  167.  
  168. // Set date and time function
  169. void setDateTime(int sec, int minu, int hr
  170.         , int daywk, int daymn, int mn, int yr)
  171. {
  172.   // Begin I2C transmission with CMPS03 address
  173.   Wire.beginTransmission(DEVICE_ADDRESS);
  174.  
  175.   // Send register address
  176.   Wire.write(0x00);
  177.  
  178.   // Send date and time to DS1307
  179.   Wire.write(decToBcd(sec));
  180.   Wire.write(decToBcd(minu));
  181.   Wire.write(decToBcd(hr));
  182.   Wire.write(decToBcd(daywk));
  183.   Wire.write(decToBcd(daymn));
  184.   Wire.write(decToBcd(mn));
  185.   Wire.write(decToBcd(yr));
  186.  
  187.   // End I2C trasmission
  188.   Wire.endTransmission();
  189. }
  190.  
  191. // Set SQW
  192. void setSQW(boolean st)
  193. {
  194.   // Begin I2C transmission with DS1307 address
  195.   Wire.beginTransmission(DEVICE_ADDRESS);  
  196.  
  197.   // Send register address
  198.   Wire.write(0x07);
  199.  
  200.   if(st)
  201.   {
  202.     // Enable SQW command
  203.     Wire.write(0x10);
  204.   }
  205.   else
  206.   {
  207.     // Disable SQW command
  208.     Wire.write(0x00);
  209.   }
  210.  
  211.   // End I2C trasmission
  212.   Wire.endTransmission();  
  213. }
Advertisement
Add Comment
Please, Sign In to add comment