Bevor das Echtzeit(Real Time Clock)-Modul DS1307 verwendet werden kann, muss nach dem Einlegen einer 3V-Knopfbatterie zunächst das richtige Datum und die Uhrzeit gestellt werden.
Der detaillierte Aufbau und die Funktionsweise des RTC-Moduls sind unter "Uhr mit 12bit RGB-LED-Ring" beschrieben. Darauf zu achten sind:
Sobald der Sketch auf den Arduino geladen und ausgeführt wird, erscheint in er seriellen Konsole folgende Ausgabe:
// I²C Address for the RTC module
#define DS1307_ADDRESS 0x68
#include <Wire.h>
void setup()
{
Wire.begin();
Serial.begin(9600);
while (!Serial); // wait for serial port to connect. Needed for native USB
Serial.flush();
printDateTime();
Serial.println("Please change to newline ending the settings of the Serial Monitor");
Serial.println("Would you like to set the date and time now? Y/N");
while (!Serial.available()) delay(10);
if (Serial.read() == 'y' || Serial.read() == 'Y') {
Serial.read();
setDateTime();
printDateTime();
}
}
void loop()
{
}
/**
* Sets date/time of the RTC module via the serial monitor
*/
void setDateTime()
{
byte second = 0, minute = 0, hour = 0, weekday = 0, monthday = 0, month = 0, year = 0;
Serial.print(F("Year (00-99): "));
year = readByte();
Serial.println(year);
Serial.print(F("Month (1-12): "));
month = readByte();
Serial.println(month);
Serial.print(F("Day of the month (1-31): "));
monthday = readByte();
Serial.println(monthday);
Serial.print(F("Day of the week (1-7): "));
weekday = readByte();
Serial.println(weekday);
Serial.print(F("Hour (0-23): "));
hour = readByte();
Serial.println(hour);
Serial.print(F("Minute (0-59): "));
minute = readByte();
Serial.println(minute);
second = 0;
// The following codes transmits the data to the RTC
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(byte(0));
Wire.write(decToBcd(second));
Wire.write(decToBcd(minute));
Wire.write(decToBcd(hour));
Wire.write(decToBcd(weekday));
Wire.write(decToBcd(monthday));
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));
Wire.write(byte(0));
Wire.endTransmission();
}
byte readByte()
{
while (!Serial.available()) {
delay(10);
}
byte reading = 0;
byte incomingByte = Serial.read();
while (incomingByte != '\n') {
if (incomingByte >= '0' && incomingByte <= '9') {
reading = reading * 10 + (incomingByte - '0');
}
incomingByte = Serial.read();
}
Serial.flush();
return reading;
}
/**
* Prints the current date/time set in the RTC module to the serial monitor
*/
void printDateTime()
{
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(0x00);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
byte nowSeconds = bcdToDec(Wire.read());
byte nowMinute = bcdToDec(Wire.read());
byte nowHour = bcdToDec(Wire.read() & 0b111111);
byte nowWeekDay = bcdToDec(Wire.read());
byte nowMonthDay = bcdToDec(Wire.read());
byte nowMonth = bcdToDec(Wire.read());
byte nowYear = bcdToDec(Wire.read());
char data[20] = "";
sprintf(data, "20%02d-%02d-%02d %02d:%02d:%02d", nowYear, nowMonth, nowMonthDay, nowHour, nowMinute, nowSeconds);
Serial.print("Current datetime: ");
Serial.println(data);
}
/**
* Converts a decimal (Base-10) integer to BCD (Binary-coded decimal)
*/
int decToBcd(int value)
{
return ((value/10*16) + (value%10));
}
/**
* Converts a BCD (Binary-coded decimal) to decimal (Base-10) integer
*/
int bcdToDec(int value)
{
return ((value/16*10) + (value%16));
}
zurück