/* * I2C_RTC sketch * example code for using Wire library to access
real-time clock */
#include <Wire.h>
const byte DS1307_CTRL_ID = 0x68;
// address of the DS1307 real-time clock
const byte NumberOfFields = 7;
// the number of fields (bytes) to
// request from the RTC
int Second ;
int Minute;
int Hour;
int Day;
int Wday;
int Month;
int Year;
void setup() {
[Link](9600);
[Link]();
}
void loop() {
[Link](DS1307_CTRL_ID);
[Link]((byte)0x00);
[Link]();
// request the 7 data fields (secs, min, hr, dow, date, mth, yr)
[Link](DS1307_CTRL_ID, NumberOfFields);
Second = bcd2dec([Link]() & 0x7f);
Minute = bcd2dec([Link]() );
Hour = bcd2dec([Link]() & 0x3f);
// mask assumes 24hr clock
Wday = bcd2dec([Link]() );
Day = bcd2dec([Link]() );
Month = bcd2dec([Link]() );
Year = bcd2dec([Link]() );
Year = Year + 2000; // RTC year 0 is year 2000
digitalClockDisplay();
// display the time delay(1000);
}
// Convert Binary Coded Decimal (BCD) to Decimal
byte bcd2dec(byte num) {
return ((num/16 * 10) + (num % 16));
}
void digitalClockDisplay(){
// digital clock display of the time
[Link](Hour);
printDigits(Minute);
printDigits(Second);
[Link](" ");
[Link](Day);
[Link](" ");
[Link](Month);
[Link](" ");
[Link](Year);
[Link]();
}
// utility function for clock display: prints preceding colon and
leading 0
void printDigits(int digits){
[Link](":");
if(digits < 10)
[Link]('0');
[Link](digits);
}
Tugas
Ubah penampilnya menggunakan LCD 16x2