Tiny RTC

From Elecrow
Jump to: navigation, search

Contents

Introduction

This tiny RTC module is based on the clock chip DS1307 which supports the I2C protocol. It uses a Lithium cell battery (CR1225). The clock/calendar provides seconds, minutes, hours, day, date,month, and year information. The end of the month date is automatically adjusted for months with fewer than 31 days, including corrections for leap year. The clock operates in either the 24-hour or 12-hour format with AM/PM indicator.
Model: SMI00101S

RTC.jpg RTC2.jpg

Features

  • 5V DC supply
  • Programmable Square-Wave output signal
  • Automatic Power-Fail detect and switch circuitry
  • Consumes less than 500nA in Battery-Backup Mode with Oscillator Running
  • 56-Byte, Battery-Backed, Nonvolatile (NV)RAM for data storage

Usage

Hardware

The RTC module use the I2C bus to communicate with Arduino&Crowduinom. Arduino has one I2C port with A4 and A5, connect the RTC module to Arduino as below:
TinyRTC hardware.jpg

Programming

You can set Tiny RTC time and get the current time from this module, copy the following programs to you Arduino IDE and download to you Arduino, please refer to Here to learn how to install the library and download the programs.

#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68  // the I2C address of Tiny RTC
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
// 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) );
}
// Function to set the currnt time, change the second&minute&hour to the right time
void setDateDs1307()                
{
  second =45; 
  minute = 29;
  hour  = 13;
  dayOfWeek = 2;
  dayOfMonth =18;
  month =9;
  year= 12;
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.write(decToBcd(0));
  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();
}
// Function to gets the date and time from the ds1307 and prints result
void getDateDs1307()
{
 // Reset the register pointer
 Wire.beginTransmission(DS1307_I2C_ADDRESS);
 Wire.write(decToBcd(0));
 Wire.endTransmission();
 Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
 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.print("  ");
 Serial.println();
 //Serial.print("Day of week:");
}
void setup() {
 Wire.begin();
 Serial.begin(19200);
 setDateDs1307(); //Set current time;
}
void loop() 
{
 delay(2000);
 getDateDs1307();//get the time data from tiny RTC
 }

You can set the current time in the setDateDs1307() function, Open a Sscom32 terminal, and set the baudrate to 19200, you will see the time that you set and changing with time.

Personal tools
Namespaces

Variants
Actions
Navigation
Elecrow Products
Toolbox