Interfacing RTC Module (DS1307) With 8051 Micro controller
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
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.
Go through our tutorial to learn more about interfacing LCD Module to 8051 in 4 bit mode and 8 bit mode.
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 are closed.