KRITSADA

POPX2 RTC show time and display on serial monitor

Nov 22nd, 2016
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.36 KB | None | 0 0
  1. /*****************************************************
  2.  *   TITLE : REAL TIME CLOCK
  3.  *   CATAGORIES : I2C
  4.  *   CREATED DATE : 22 NOVEMBER 2016
  5.  *   AUTHOR : INNOVATIVE EXPERIMENT CO., LTD. (INEX)
  6.  *   WEBSITE : HTTP://WWW.INEX.CO.TH
  7.  *   DESCRIPTION : Read date and time on MCP7940  (DS1307 Compatible)
  8.  *                 and send data to computer by serial port
  9.  *                 toggle between on and off
  10.  *                 and enable oscillator if disabled
  11.  *                 โปรแกรมแสดงเวลาจาก ZX-RTC ที่ใช้ DS1307
  12.  *                 run on arduino 1.7.10 and POP-X2 board
  13.  *                 โดยส่งค่าทาง Serial Port เพื่อแสดงบนคอมพิวเตอร์
  14.  *                 ถ้าโมดูลไม่ทำงาน จะกำหนดค่าและให้เริ่มทำงาน
  15. ******************************************************/
  16. #include <popx2.h>
  17. #include <Wire.h>
  18. #define DEVICE_ADDRESS 0x6F
  19. int second, minute, hour, dayOfWeek,dayOfMonth,month,year;
  20. int second1;
  21. void setup(){
  22.   Serial.begin(9600);
  23.   Wire.begin();
  24.   setDateTime(0, 59, 13, 3, 22, 11, 16);
  25.   setTextSize(2);
  26. }
  27. void loop(){
  28.   getDateTime();
  29.   Serial.print(hour);
  30.   Serial.print(":");
  31.   Serial.print(minute);
  32.   Serial.print(":");
  33.   Serial.print(second);
  34.   Serial.print("   ");
  35.   Serial.print(year);
  36.   Serial.print("/");
  37.   Serial.print(month);
  38.   Serial.print("/");
  39.   Serial.print(dayOfMonth);
  40.   Serial.print("\n\r");
  41. }
  42.  
  43. byte decToBcd(byte val){  return ((val / 10 * 16) + (val % 10)); } // Convert decimal to BCD function
  44. byte bcdToDec(byte val){  return ((val / 16 * 10) + (val % 16));} // Convert BCD to decimal function
  45. void getDateTime(){
  46.   // Begin I2C transmission with DS1307 address
  47.   Wire.beginTransmission(DEVICE_ADDRESS);  
  48.   Wire.write(0x00); // Send register address
  49.   Wire.endTransmission(); // End I2C trasmission
  50.   Wire.requestFrom(DEVICE_ADDRESS, 7); // Request next 7 register address
  51.   // Get date and time
  52.   second1 = Wire.read();
  53.   minute = bcdToDec(Wire.read());
  54.   hour   = bcdToDec(Wire.read() & 0x3f);
  55.   dayOfWeek = bcdToDec(Wire.read());
  56.   dayOfMonth = bcdToDec(Wire.read());
  57.   month   = bcdToDec(Wire.read());
  58.   year = bcdToDec(Wire.read());
  59.  
  60.   if ((second1&0x80)==0x00){
  61.     Wire.beginTransmission(DEVICE_ADDRESS);  
  62.     Wire.write(0x00);
  63.     Wire.write(second1|0x80);
  64.     Wire.endTransmission();
  65.   }  
  66.   second=bcdToDec(second1 &0x7F);
  67. }
  68.  
  69. void setDateTime(int sec, int minu, int hr, int daywk, int daymn, int mn, int yr){
  70.   // Begin I2C transmission with CMPS03 address
  71.   Wire.beginTransmission(DEVICE_ADDRESS);
  72.    Wire.write(0x00);
  73.   // Send date and time to DS1307
  74.   Wire.write(decToBcd(sec));
  75.   Wire.write(decToBcd(minu));
  76.   Wire.write(decToBcd(hr));
  77.   Wire.write(decToBcd(daywk));
  78.   Wire.write(decToBcd(daymn));
  79.   Wire.write(decToBcd(mn));
  80.   Wire.write(decToBcd(yr));
  81.  
  82.   // End I2C trasmission
  83.   Wire.endTransmission();
  84. }
  85.  
  86. // Set SQW
  87. void setSQW(boolean st)
  88. {
  89.   // Begin I2C transmission with DS1307 address
  90.   Wire.beginTransmission(DEVICE_ADDRESS);  
  91.  
  92.   // Send register address
  93.   Wire.write(0x07);
  94.  
  95.   if(st)
  96.   {
  97.     // Enable SQW command
  98.     Wire.write(0x10);
  99.   }
  100.   else
  101.   {
  102.     // Disable SQW command
  103.     Wire.write(0x00);
  104.   }
  105.  
  106.   // End I2C trasmission
  107.   Wire.endTransmission();  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment