RTC Module
Introduction
The Linker RTC module is based on the clock chip DS1307, which supports the I2C protocol. It utilizes a Lithium cell battery (CR1225). The clock/calendar provides seconds, minutes, hours, day, date, month, and year. The end of the month date is automatically adjusted for months with fewer than 31 days, including corrections for leap years. The clock operates in either the 24-hour or 12-hour format with AM/PM indicator. And it is valid up to 2100. In order to gain a robust performance, you must put a 3-Volt CR1225 lithium cell in the battery-holder. If you use the primary power only, the module may not work normally, because the crystal may not oscillate.
Schematics
Application Ideas
test code <syntaxhighlight lang="c">
- include "Wire.h"
- define DS1307_I2C_ADDRESS 0x68 // This is the I2C address
//Global Variables int command = 0; // This is the command char, in ascii form, sent from the serial port byte zero=0; //long previousMillis = 0; // will store last time Temp was updated byte second, minute, hour, dayOfWeek, dayOfMonth, month, year; byte test; //Convert normal decimal numbers to binary coded decimal byte second, minute, hour, dayOfWeek, dayOfMonth, month, year; byte test;
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 setDateDs1307(void) {
second = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48)); minute = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48)); hour = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48)); dayOfWeek = (byte) (Serial.read() - 48); dayOfMonth = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48)); month = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48)); year= (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48)); Wire.beginTransmission(DS1307_I2C_ADDRESS); Wire.write(zero); Wire.write(decToBcd(second)); // 0 to bit 7 starts the clock Wire.write(decToBcd(minute)); Wire.write(decToBcd(hour)); // If you want 12 hour am/pm you need to set // bit 6 (also need to change readDateDs1307) Wire.write(decToBcd(dayOfWeek)); Wire.write(decToBcd(dayOfMonth)); Wire.write(decToBcd(month)); Wire.write(decToBcd(year)); Wire.endTransmission();
}
//Gets the date and time from the ds1307 and prints result
void getDateDs1307() {
// Reset the register pointer Wire.beginTransmission(DS1307_I2C_ADDRESS); Wire.write(zero); Wire.endTransmission(); Wire.requestFrom(DS1307_I2C_ADDRESS, 7); // A few of these need masks because certain bits are control bits second = bcdToDec(Wire.read() & 0x7f); minute = bcdToDec(Wire.read()); hour = bcdToDec(Wire.read() & 0x3f); // Need to change this if 12 hour am/pm dayOfWeek = bcdToDec(Wire.read()); dayOfMonth = bcdToDec(Wire.read()); month = bcdToDec(Wire.read()); year = bcdToDec(Wire.read());
Serial.print(hour, DEC); Serial.print(":"); Serial.print(minute, DEC); Serial.print(":"); Serial.print(second, DEC); Serial.print(" "); Serial.print(month, DEC); Serial.print("/"); Serial.print(dayOfMonth, DEC); Serial.print("/"); Serial.print(year, DEC); Serial.println(" ");
}
void setup() {
Wire.begin(); Serial.begin(9600);
} void loop() {
delay(2000); /*T(00-59)(00-59)(00-23)(1-7)(01-31)(01-12)(00-99) - T(sec)(min)(hour)(dayOfWeek)(dayOfMonth)(month)(year) - T Sets the date of the RTC DS1307 Chip. Example to set the time for 02-DEC-10 @ 19:57:11 for the 3 day of the week, send this command - T1157193021210 */ if (Serial.available()) { // Look for char in serial que and process if found command = Serial.read(); if (command == 84) { //If command = "T" Set Date setDateDs1307(); getDateDs1307(); Serial.println(" "); } while(1) { getDateDs1307(); delay(1000); } }
} </syntaxhighlight>
Sample Code
How to buy
Here to buy RTC Module on store