Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*****************************************************
- *
- * TITLE : REAL TIME CLOCK MCP7940 (DS1307 Compatible)
- * CATAGORIES : I2C
- * CREATED DATE : NOVEMBER 22, 2016
- * AUTHOR : INNOVATIVE EXPERIMENT CO., LTD. (INEX)
- * WEBSITE : HTTP://WWW.INEX.CO.TH
- * MCU : POP-X2 witch Graphic LCD
- * DESCRIPTION : Read date and time on DS1307
- * then show clock graphic on GLCD
- * and enable oscillator if disabled
- * โปรแกรมแสดงเวลาจาก ZX-RTC ที่ใช้ DS1307
- * โดยจะแสดงเวลาที่ได้เป็นรูปหน้าปัดนาฬิกาบนจอ GLCD
- * ถ้าโมดูลไม่ทำงาน จะกำหนดค่าและให้เริ่มทำงาน
- *
- ******************************************************/
- #include <popx2.h>
- #include <Wire.h>
- // Define DS1307 Address
- #define DEVICE_ADDRESS 0x6F
- // Define clock face radius
- #define CLOCK_SIZE 60
- // Declare variable for date and time
- int second, minute, hour, dayOfWeek, dayOfMonth, month, year;
- int second1;
- void setup()
- {
- // Initiate I2C
- Wire.begin();
- // If you need set date and time command
- setDateTime(00, 07, 15, 3, 22, 11, 16);
- // Draw clock face
- initClock();
- }
- void loop()
- {
- // Call get date and time function
- getDateTime();
- // Draw clock pointers
- UpdateClock();
- }
- void initClock()
- {
- glcdFillCircle((GLCD_WIDTH / 2), (GLCD_HEIGHT / 2)
- , CLOCK_SIZE, GLCD_WHITE);
- glcdLine(64, 25, 64, 35, GLCD_RED);
- glcdLine(10, 80, 20, 80, GLCD_RED);
- glcdLine(119, 80, 109, 80, GLCD_RED);
- glcdLine(64, 135, 64, 125, GLCD_RED);
- glcdLine(37, 33, 39, 38, GLCD_BLACK);
- glcdLine(91, 33, 89, 38, GLCD_BLACK);
- glcdLine(17, 53, 22, 55, GLCD_BLACK);
- glcdLine(111, 53, 106, 55, GLCD_BLACK);
- glcdLine(22, 105, 17, 107, GLCD_BLACK);
- glcdLine(106, 105, 111, 107, GLCD_BLACK);
- glcdLine(39, 122, 37, 127, GLCD_BLACK);
- glcdLine(89, 122, 91, 127, GLCD_BLACK);
- }
- // Variable to check second value was changed
- int tempSec = 0;
- int xSecond = 0;
- int ySecond = 0;
- int xMinute = 0;
- int yMinute = 0;
- float xHour = 0;
- float yHour = 0;
- // Draw clock pointer function
- void UpdateClock()
- {
- if(tempSec != second) {
- // Delete clock pointer (Replace white line)
- glcdLine((GLCD_WIDTH / 2), (GLCD_HEIGHT / 2)
- , (GLCD_WIDTH / 2) + xSecond
- , (GLCD_HEIGHT / 2) + ySecond
- , GLCD_WHITE);
- glcdLine((GLCD_WIDTH / 2), (GLCD_HEIGHT / 2)
- , (GLCD_WIDTH / 2) + xMinute
- , (GLCD_HEIGHT / 2) + yMinute
- , GLCD_WHITE);
- glcdLine((GLCD_WIDTH / 2), (GLCD_HEIGHT / 2)
- , (GLCD_WIDTH / 2) + xHour
- , (GLCD_HEIGHT / 2) + yHour
- , GLCD_WHITE);
- // Calculated x and y coordinate to draw clock pointer
- xSecond = cos(((second * 6) - 90) * DEG_TO_RAD) * 40;
- ySecond = sin(((second * 6) - 90) * DEG_TO_RAD) * 40;
- xMinute = cos(((minute * 6) - 90) * DEG_TO_RAD) * 30;
- yMinute = sin(((minute * 6) - 90) * DEG_TO_RAD) * 30;
- xHour = cos(((((float)hour + ((float)minute / 60)) * 30) - 90) * DEG_TO_RAD) * 25;
- yHour = sin(((((float)hour + ((float)minute / 60)) * 30) - 90) * DEG_TO_RAD) * 25;
- // Draw clock pointer
- glcdLine((GLCD_WIDTH / 2), (GLCD_HEIGHT / 2)
- , (GLCD_WIDTH / 2) + xSecond
- , (GLCD_HEIGHT / 2) + ySecond
- , GLCD_RED);
- glcdLine((GLCD_WIDTH / 2), (GLCD_HEIGHT / 2)
- , (GLCD_WIDTH / 2) + xMinute
- , (GLCD_HEIGHT / 2) + yMinute
- , GLCD_BLUE);
- glcdLine((GLCD_WIDTH / 2), (GLCD_HEIGHT / 2)
- , (GLCD_WIDTH / 2) + xHour
- , (GLCD_HEIGHT / 2) + yHour
- , GLCD_BLACK);
- tempSec = second;
- }
- }
- // Convert decimal to BCD function
- byte decToBcd(byte val)
- {
- return ((val / 10 * 16) + (val % 10));
- }
- // Convert BCD to decimal function
- byte bcdToDec(byte val)
- {
- return ((val / 16 * 10) + (val % 16));
- }
- // Get date and time function
- void getDateTime()
- {
- // Begin I2C transmission with DS1307 address
- Wire.beginTransmission(DEVICE_ADDRESS);
- // Send register address
- Wire.write(0x00);
- // End I2C trasmission
- Wire.endTransmission();
- // Request next 7 register address
- Wire.requestFrom(DEVICE_ADDRESS, 7);
- // 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 DS1307 clock halt (CH) bit is 1
- // (Oscillator is disabled)
- if ((second1&0x80)==0x00){
- Wire.beginTransmission(DEVICE_ADDRESS);
- Wire.write(0x00);
- Wire.write(second1|0x80);
- Wire.endTransmission();
- }
- second=bcdToDec(second1 &0x7F);
- }
- // Set date and time function
- 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);
- // Send register 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