0% found this document useful (0 votes)
113 views

Code RTC - Ser - Mon PDF

This code example uses the Wire library to access data from a real-time clock (RTC) chip and display the time on the serial monitor. It initializes the Wire library and defines constants for the RTC chip address and number of data fields. In the loop, it requests the time data from the RTC, converts the binary-coded decimal values to decimal, and displays the digital clock on the serial monitor by printing the time values with leading zeros for minutes and seconds. The task is to modify the code to display the time on a 16x2 LCD instead of the serial monitor.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views

Code RTC - Ser - Mon PDF

This code example uses the Wire library to access data from a real-time clock (RTC) chip and display the time on the serial monitor. It initializes the Wire library and defines constants for the RTC chip address and number of data fields. In the loop, it requests the time data from the RTC, converts the binary-coded decimal values to decimal, and displays the digital clock on the serial monitor by printing the time values with leading zeros for minutes and seconds. The task is to modify the code to display the time on a 16x2 LCD instead of the serial monitor.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

/* * 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() {
Serial.begin(9600);
Wire.begin();
}

void loop() {
Wire.beginTransmission(DS1307_CTRL_ID);
Wire.write((byte)0x00);
Wire.endTransmission();

// request the 7 data fields (secs, min, hr, dow, date, mth, yr)
Wire.requestFrom(DS1307_CTRL_ID, NumberOfFields);
Second = bcd2dec(Wire.read() & 0x7f);
Minute = bcd2dec(Wire.read() );
Hour = bcd2dec(Wire.read() & 0x3f);
// mask assumes 24hr clock
Wday = bcd2dec(Wire.read() );
Day = bcd2dec(Wire.read() );
Month = bcd2dec(Wire.read() );
Year = bcd2dec(Wire.read() );
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
Serial.print(Hour);
printDigits(Minute);
printDigits(Second);
Serial.print(" ");
Serial.print(Day);
Serial.print(" ");
Serial.print(Month);
Serial.print(" ");
Serial.print(Year);
Serial.println();
}
// utility function for clock display: prints preceding colon and
leading 0
void printDigits(int digits){
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}

Tugas
Ubah penampilnya menggunakan LCD 16x2

You might also like