Skip to main content

Learn to Build Real Time Clock using 8051and DS1307 RTC Module

In this article, we are going to see how to interface an RTC (Real Time Clock) Module to 8051 micro controller. There are different kinds of RTC module available in the market. We are using the most common RTC module that comes with the DS1307 IC, an LCD module and AT89S52 (8051 variant) for this tutorial. AT89S52 is a typical 8051 microcontroller manufactured by Atmel. Interfacing an RTC module to 8051 microcontroller is pretty simple. You only need to make 2 connections between the RTC module and 8051. So lets get to business!

A Real Time Clock module is basically a time tracking device which gives the current time and date. RTC modules that comes with DS3231 IC have the provision to set alarms.

Notes On DS1307 RTC Module

Here we are using an RTC module with clock chip DS1307 based on I2C protocol (Two Wire Protocol). The module provides details such us second, minute, hour, day of week, day of month, month and year including correction for leap year. It can operate either in 12 Hour or in 24 Hour format. Current consumption of this module is nano ampere range. Even a 3V battery can power it for 10 years maintaining an accurate clock and without any external power.
DS1307 has a memory area of 64 bytes of which the first 8 bytes are reserved as RTC register area and the remaining 56 bytes are allotted as general purpose RAM. The details about current, date and time is stored in its register area as Binary Coded Decimals(BCD). The module communicates with the microcontroller using a serial communication protocol called I2C. The I2C bus physically consists of 2 active wires. The wires, called SDA and SCL, are both bi-directional. SDA is the Serial Data line, and SCL is the Serial CLock line. Every device connected to the bus has its own unique device address, no matter whether it is an MCU or RTC module. Each of these chips can act as a receiver or transmitter, depending on the functionality.
DS1307 will act as slave in the communication network and controller can only access the slave by initiating a start condition along with a device address. Thereafter, we need to send the register number in order to access the value inside. The interface to the 8051 is simple I2C, with SDA and SCL pins connected to the any two pins of 8051. At the software side we are using a user defined library named “I2C” for I2C communication. This library allows you to communicate with I2C / TWI devices.
I hope you understood so far!  Lets get to the circuit diagram! So given below is the circuit diagram to connect RTC module to 8051.

Circuit Diagram – RTC Interfacing with 8051

Circuit Diagram – RTC 8051 Interfacing

Make the connections as shown!  Now  lets get to the coding part.

The Program/Code Explanation

The program for RTC (Real Time Clock) using 8051 is written in Embedded C (using Keil compiler).
We begin by including “LCD8bit” library into the program. It is a user defined library for interfacing microcontroller with LCD module in 8 bit mode. In this library, we included a few functions for initializing LCD, for sending commands and for sending data which is to be displayed.
The port which is to be connected to the data pins of the LCD and the pins which is to be connected to the command pins can be configured in the library itself. Here we defined port0 for data pins and P3.5, P3.6, & P3.7 for command pins.
Another user defined library used here is “delay” which consist of functions named “Delay_ms()” and “Delay_us()”. Delay_ms(1000) will halt the program for 1000 millisecond(ie. One second) and Delay_us(_) function is used when we need to halt the program for few microseconds.
In this project, we are focusing mainly on RTC module interfacing. As mentioned earlier  a library named “I2C” is used here for I2C communication.  The  I2CStart()  function will initiate the communication.  I2CSend() is the function used for sending data and I2Cread() is for reading registers.
Two functions named “readAllReg()” and “readSecReg()” are defined at the main program for reading time details from RTC module. Former one is for reading all the registers and latter is for reading ‘second’ register alone.
In DS1307, bytes are  stored as binary coded decimals. So before writing values into the registers, we need to convert it into BCD(Binary Coded Decimals). Like wise, after reading values from the register area, it should be converted into decimal. Dec_To_BCD() is the function used for this.
Before reading values from the RTC module, we must set the time and date at once. The function used  for this is the setTime() function. We should pass the actual time details as parameters. The actual format is like setTime(sec,min,hour,dow,dom,month,year); .
In our project, we are using DS1307 in 24 hour mode and we then convert it into 12 hour mode at the software side. So the time details should be in 24 hour mode while passing arguments through setTime() function.
Note:- After uploading the program at once, you should re-upload the code by commenting the setTime() function.  Otherwise the setTime() function will corrupt the current time while every time  you reset the microcontroller.

A function named displayTime() is used for displaying time and date in the LCD. Inside the function it will call the ”readSecreg()” at every one second. And the “readAllReg()” function will be called at every one minute. An array named “a[7]” is declared globally at the beginning of the program. The “readSecReg()” and “readAllReg()” functions will periodically update this array elements at every one second and one minute respectively.

Comments

Popular posts from this blog

Introduction To Smart Wi-Fi

We need faster tools and media to prove our ideas, and this is where ready-to use modules like Smart Wi-Fi enter. Fig. 1 shows the top side of Smart Wi-Fi module. Fig. 1: Smart Wi-Fi module Fig. 2: Block diagram of the introduction of Smart Wi-Fi The module is low-cost and versatile; all a developer needs is a USB cable and it is ready to work. For software developers, working with hardware is always a pain, and this is one of the biggest reasons why many ideas never exit a computer. Smart Wi-Fi enables making a product quickly and reliably. With open software resources and hardware data, moving to the final product after the proof of concept is also easy. One of the major aims for writing this article is to show the versatility of this product and show how quick the development can be. In this article a few tests with Smart Wi-Fi are discussed, which show the applicability of this module for development of the Internet of Things (IoT) products. Fig. 3: C

Make Your Own Air Quality Monitoring Device!

Air pollution and concern about air quality is not something new. Complaints were recorded even in the 13th century when coal was first used for industrial purposes in London. From the middle of the 19th century, the atmosphere of major British cities was regularly polluted by coal smoke in winters, giving rise to the infamous mixture of fog and smoke known as smog. Today the emphasis has shifted from pollution problems caused by the industry to the ones associated with motor vehicle emissions. Author’s Prototype Air Quality Index Air Quality Index, or AQI, is a number used by government agencies to communicate to the public how polluted the air currently is, or how polluted it is likely to become. As AQI increases, an increasingly large percentage of the population experiences severe adverse health effects. Different countries have their own air-quality indices corresponding to different national air-quality standards. Some of these are Air Quality Health Index (C

Hit Counter