Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*****************************************************
- * TITLE : REAL TIME CLOCK
- * CATAGORIES : I2C
- * CREATED DATE : 22 NOVEMBER 2016
- * AUTHOR : INNOVATIVE EXPERIMENT CO., LTD. (INEX)
- * WEBSITE : HTTP://WWW.INEX.CO.TH
- * DESCRIPTION : Read date and time on MCP7940 (DS1307 Compatible)
- * and send data to computer by serial port
- * toggle between on and off
- * and enable oscillator if disabled
- * โปรแกรมแสดงเวลาจาก ZX-RTC ที่ใช้ DS1307
- * run on arduino 1.7.10 and POP-X2 board
- * โดยส่งค่าทาง Serial Port เพื่อแสดงบนคอมพิวเตอร์
- * ถ้าโมดูลไม่ทำงาน จะกำหนดค่าและให้เริ่มทำงาน
- ******************************************************/
- #include <popx2.h>
- #include <Wire.h>
- #define DEVICE_ADDRESS 0x6F
- int second, minute, hour, dayOfWeek,dayOfMonth,month,year;
- int second1;
- void setup(){
- Serial.begin(9600);
- Wire.begin();
- setDateTime(0, 59, 13, 3, 22, 11, 16);
- setTextSize(2);
- }
- void loop(){
- getDateTime();
- Serial.print(hour);
- Serial.print(":");
- Serial.print(minute);
- Serial.print(":");
- Serial.print(second);
- Serial.print(" ");
- Serial.print(year);
- Serial.print("/");
- Serial.print(month);
- Serial.print("/");
- Serial.print(dayOfMonth);
- Serial.print("\n\r");
- }
- byte decToBcd(byte val){ return ((val / 10 * 16) + (val % 10)); } // Convert decimal to BCD function
- byte bcdToDec(byte val){ return ((val / 16 * 10) + (val % 16));} // Convert BCD to decimal function
- void getDateTime(){
- // Begin I2C transmission with DS1307 address
- Wire.beginTransmission(DEVICE_ADDRESS);
- Wire.write(0x00); // Send register address
- Wire.endTransmission(); // End I2C trasmission
- Wire.requestFrom(DEVICE_ADDRESS, 7); // Request next 7 register address
- // Get date and time
- second1 = Wire.read();
- minute = bcdToDec(Wire.read());
- hour = bcdToDec(Wire.read() & 0x3f);
- dayOfWeek = bcdToDec(Wire.read());
- dayOfMonth = bcdToDec(Wire.read());
- month = bcdToDec(Wire.read());
- year = bcdToDec(Wire.read());
- if ((second1&0x80)==0x00){
- Wire.beginTransmission(DEVICE_ADDRESS);
- Wire.write(0x00);
- Wire.write(second1|0x80);
- Wire.endTransmission();
- }
- second=bcdToDec(second1 &0x7F);
- }
- void setDateTime(int sec, int minu, int hr, int daywk, int daymn, int mn, int yr){
- // Begin I2C transmission with CMPS03 address
- Wire.beginTransmission(DEVICE_ADDRESS);
- Wire.write(0x00);
- // Send date and time to DS1307
- Wire.write(decToBcd(sec));
- Wire.write(decToBcd(minu));
- Wire.write(decToBcd(hr));
- Wire.write(decToBcd(daywk));
- Wire.write(decToBcd(daymn));
- Wire.write(decToBcd(mn));
- Wire.write(decToBcd(yr));
- // End I2C trasmission
- Wire.endTransmission();
- }
- // Set SQW
- void setSQW(boolean st)
- {
- // Begin I2C transmission with DS1307 address
- Wire.beginTransmission(DEVICE_ADDRESS);
- // Send register address
- Wire.write(0x07);
- if(st)
- {
- // Enable SQW command
- Wire.write(0x10);
- }
- else
- {
- // Disable SQW command
- Wire.write(0x00);
- }
- // End I2C trasmission
- Wire.endTransmission();
- }
Advertisement
Add Comment
Please, Sign In to add comment