Difference between revisions of "Arduino: RTC DS3231 PC Sync"
Jump to navigation
Jump to search
Onnowpurbo (talk | contribs) (Created page with "Sumber: http://playground.arduino.cc/Code/time ==Siapkan Library== Download dari * http://playground.arduino.cc/uploads/Code/Time.zip Extrak cd /usr/share/arduino/libra...") |
Onnowpurbo (talk | contribs) (→Code) |
||
| Line 28: | Line 28: | ||
==Code== | ==Code== | ||
| + | #include "Wire.h" | ||
#include <Time.h> | #include <Time.h> | ||
| Line 34: | Line 35: | ||
#define TIME_REQUEST 7 // ASCII bell character requests a time sync message | #define TIME_REQUEST 7 // ASCII bell character requests a time sync message | ||
| − | // | + | #define DS3231_I2C_ADDRESS 0x68 |
| − | + | // Convert normal decimal numbers to binary coded decimal | |
| − | void setup() { | + | byte decToBcd(byte val) |
| + | { | ||
| + | return ( (val / 10 * 16) + (val % 10) ); | ||
| + | } | ||
| + | // Convert binary coded decimal to normal decimal numbers | ||
| + | byte bcdToDec(byte val) | ||
| + | { | ||
| + | return ( (val / 16 * 10) + (val % 16) ); | ||
| + | } | ||
| + | void setup() | ||
| + | { | ||
| + | Wire.begin(); | ||
Serial.begin(9600); | Serial.begin(9600); | ||
| + | |||
| + | // set the initial time here: | ||
| + | // DS3231 seconds, minutes, hours, day, date, month, year | ||
| + | // setDS3231time(30,33,9, 2 ,28,11,16); | ||
| + | } | ||
| + | void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte | ||
| + | dayOfMonth, byte month, byte year) | ||
| + | { | ||
| + | // sets time and date data to DS3231 | ||
| + | Wire.beginTransmission(DS3231_I2C_ADDRESS); | ||
| + | Wire.write(0); // set next input to start at the seconds register | ||
| + | Wire.write(decToBcd(second)); // set seconds | ||
| + | Wire.write(decToBcd(minute)); // set minutes | ||
| + | Wire.write(decToBcd(hour)); // set hours | ||
| + | Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday) | ||
| + | Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31) | ||
| + | Wire.write(decToBcd(month)); // set month | ||
| + | Wire.write(decToBcd(year)); // set year (0 to 99) | ||
| + | Wire.endTransmission(); | ||
| + | } | ||
| + | void readDS3231time(byte *second, | ||
| + | byte *minute, | ||
| + | byte *hour, | ||
| + | byte *dayOfWeek, | ||
| + | byte *dayOfMonth, | ||
| + | byte *month, | ||
| + | byte *year) | ||
| + | { | ||
| + | Wire.beginTransmission(DS3231_I2C_ADDRESS); | ||
| + | Wire.write(0); // set DS3231 register pointer to 00h | ||
| + | Wire.endTransmission(); | ||
| + | Wire.requestFrom(DS3231_I2C_ADDRESS, 7); | ||
| + | // request seven bytes of data from DS3231 starting from register 00h | ||
| + | *second = bcdToDec(Wire.read() & 0x7f); | ||
| + | *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()); | ||
| + | } | ||
| + | void displayTime() | ||
| + | { | ||
| + | byte second, minute, hour, dayOfWeek, dayOfMonth, month, year; | ||
| + | // retrieve data from DS3231 | ||
| + | readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, | ||
| + | &year); | ||
| + | // send it to the serial monitor | ||
| + | Serial.print(hour, DEC); | ||
| + | // convert the byte variable to a decimal number when displayed | ||
| + | Serial.print(":"); | ||
| + | if (minute < 10) | ||
| + | { | ||
| + | Serial.print("0"); | ||
| + | } | ||
| + | Serial.print(minute, DEC); | ||
| + | Serial.print(":"); | ||
| + | if (second < 10) | ||
| + | { | ||
| + | Serial.print("0"); | ||
| + | } | ||
| + | Serial.print(second, DEC); | ||
| + | Serial.print(" "); | ||
| + | Serial.print(dayOfMonth, DEC); | ||
| + | Serial.print("/"); | ||
| + | Serial.print(month, DEC); | ||
| + | Serial.print("/"); | ||
| + | Serial.print(year, DEC); | ||
| + | Serial.print(" Day of week: "); | ||
| + | switch (dayOfWeek) { | ||
| + | case 1: | ||
| + | Serial.println("Sunday"); | ||
| + | break; | ||
| + | case 2: | ||
| + | Serial.println("Monday"); | ||
| + | break; | ||
| + | case 3: | ||
| + | Serial.println("Tuesday"); | ||
| + | break; | ||
| + | case 4: | ||
| + | Serial.println("Wednesday"); | ||
| + | break; | ||
| + | case 5: | ||
| + | Serial.println("Thursday"); | ||
| + | break; | ||
| + | case 6: | ||
| + | Serial.println("Friday"); | ||
| + | break; | ||
| + | case 7: | ||
| + | Serial.println("Saturday"); | ||
| + | break; | ||
| + | } | ||
} | } | ||
| Line 48: | Line 152: | ||
Serial.println("waiting for sync message"); | Serial.println("waiting for sync message"); | ||
else | else | ||
| − | + | // digitalClockDisplay(); | |
| + | displayTime(); | ||
delay(1000); | delay(1000); | ||
} | } | ||
| − | + | ||
| + | |||
void digitalClockDisplay(){ | void digitalClockDisplay(){ | ||
// digital clock display of the time | // digital clock display of the time | ||
| Line 88: | Line 194: | ||
} | } | ||
setTime(pctime); // Sync Arduino clock to the time received on the serial port | setTime(pctime); // Sync Arduino clock to the time received on the serial port | ||
| + | // set the initial time here: | ||
| + | // DS3231 seconds, minutes, hours, day, date, month, year | ||
| + | setDS3231time(second(),minute(),hour()+7, 5 , 7, 6, 18); | ||
} | } | ||
} | } | ||
Latest revision as of 09:07, 7 June 2018
Sumber: http://playground.arduino.cc/Code/time
Siapkan Library
Download dari
Extrak
cd /usr/share/arduino/libraries/
atau
cd ~/Arduino/libraries
wget http://playground.arduino.cc/uploads/Code/Time.zip unzip Time.zip cd Time gedit DateStrings.cpp
Tambahkan di
char > char const PGM_P > PGM_P const
Code
#include "Wire.h"
#include <Time.h>
#define TIME_MSG_LEN 11 // time sync to PC is HEADER followed by Unix time_t as ten ASCII digits
#define TIME_HEADER 'T' // Header tag for serial time sync message
#define TIME_REQUEST 7 // ASCII bell character requests a time sync message
#define DS3231_I2C_ADDRESS 0x68
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return ( (val / 10 * 16) + (val % 10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return ( (val / 16 * 10) + (val % 16) );
}
void setup()
{
Wire.begin();
Serial.begin(9600);
// set the initial time here:
// DS3231 seconds, minutes, hours, day, date, month, year
// setDS3231time(30,33,9, 2 ,28,11,16);
}
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
dayOfMonth, byte month, byte year)
{
// sets time and date data to DS3231
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set next input to start at the seconds register
Wire.write(decToBcd(second)); // set seconds
Wire.write(decToBcd(minute)); // set minutes
Wire.write(decToBcd(hour)); // set hours
Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
Wire.write(decToBcd(month)); // set month
Wire.write(decToBcd(year)); // set year (0 to 99)
Wire.endTransmission();
}
void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set DS3231 register pointer to 00h
Wire.endTransmission();
Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
// request seven bytes of data from DS3231 starting from register 00h
*second = bcdToDec(Wire.read() & 0x7f);
*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());
}
void displayTime()
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
// retrieve data from DS3231
readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
&year);
// send it to the serial monitor
Serial.print(hour, DEC);
// convert the byte variable to a decimal number when displayed
Serial.print(":");
if (minute < 10)
{
Serial.print("0");
}
Serial.print(minute, DEC);
Serial.print(":");
if (second < 10)
{
Serial.print("0");
}
Serial.print(second, DEC);
Serial.print(" ");
Serial.print(dayOfMonth, DEC);
Serial.print("/");
Serial.print(month, DEC);
Serial.print("/");
Serial.print(year, DEC);
Serial.print(" Day of week: ");
switch (dayOfWeek) {
case 1:
Serial.println("Sunday");
break;
case 2:
Serial.println("Monday");
break;
case 3:
Serial.println("Tuesday");
break;
case 4:
Serial.println("Wednesday");
break;
case 5:
Serial.println("Thursday");
break;
case 6:
Serial.println("Friday");
break;
case 7:
Serial.println("Saturday");
break;
}
}
void loop(){
if(Serial.available() )
{
processSyncMessage();
}
if(timeStatus() == timeNotSet)
Serial.println("waiting for sync message");
else
// digitalClockDisplay();
displayTime();
delay(1000);
}
void digitalClockDisplay(){
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.println();
}
void printDigits(int digits){
// utility function for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
void processSyncMessage() {
// if time sync available from serial port, update time and return true
while(Serial.available() >= TIME_MSG_LEN ){ // time message consists of header & 10 ASCII digits
char c = Serial.read() ;
Serial.print(c);
if( c == TIME_HEADER ) {
time_t pctime = 0;
for(int i=0; i < TIME_MSG_LEN -1; i++){
c = Serial.read();
if( c >= '0' && c <= '9'){
pctime = (10 * pctime) + (c - '0') ; // convert digits to a number
}
}
setTime(pctime); // Sync Arduino clock to the time received on the serial port
// set the initial time here:
// DS3231 seconds, minutes, hours, day, date, month, year
setDS3231time(second(),minute(),hour()+7, 5 , 7, 6, 18);
}
}
}
PC Sync
Lakukan
date +T%s\n > /dev/ttyACM0
Untuk mensinkronisasi waktu