You are on page 1of 19

26/9/2018 Interfacing RTC DS3231 with 8051 Microcontroller - Codrey Electronics

Subscribe to get
updates
Your Name

Email Address *

HOME ELECTRONICS EMBEDDED SYSTEMS MICROCONTROLLERS ARDUINO

PROGRAMMING
SUBSCRIBE

YOU ARE HERE: HOME / MICROCONTROLLER PROJECTS / INTERFACING RTC DS3231 WITH 8051 MICROCONTROLLER

Interfacing RTC DS3231 with 8051


Microcontroller
LAST UPDATED ON JUNE 3, 2018 BY KUMAR — LEAVE A COMMENT

Have you ever thought how your computer and smart gadgets displays time? This is
done using an RTC (Real Time Clock). This tutorial explains DS3231 RTC interfacing with
8051 using I2C.

After the end of the tutorial, you will know how to write an embedded c code for DS3231
RTC using I2C protocol.

To show the date and time, I have connected both PC as well as LCD. In this article, I will
explain how to display date and time on LCD using DS3231.

Contents [hide]

1 DS3231 RTC Hardware Connection


2 Steps to Program DS3231

https://www.codrey.com/microcontroller-projects/interfacing-rtc-ds3231-with-8051-microcontroller/ 1/19
26/9/2018 Interfacing RTC DS3231 with 8051 Microcontroller - Codrey Electronics

3 C code for RTC DS3231


4 Proteus Simulation

Subscribe to get
5 Applications of DS3231
6 Final Thoughts
updates
DS3231 RTC Hardware Connection
Your Name

I have picked up 8051 microcontroller as master and DS3231 acting as a slave device.
Email Address *

Pull up resistors are connected to the SDA and SCL pins of RTC to the microcontroller.

High-quality Python and


XGS series and sensors
SUBSCRIBE

from Aptina, Cypress and …


Ad FRAMOS

Learn more

It is recommended to use 10K ohms pull up resistance for the proper working of RTC.

The INT_RTC is left unconnected and it can be used to generate alarm interrupts for
external or internal events.

Connection Diagram

https://www.codrey.com/microcontroller-projects/interfacing-rtc-ds3231-with-8051-microcontroller/ 2/19
26/9/2018 Interfacing RTC DS3231 with 8051 Microcontroller - Codrey Electronics

To get started with Real Time Clock it is required to know the working of DS3231. Here
are steps required to program the RTC.

Subscribe to get
Steps to Program DS3231 updates
#1 Initialize the RTC by setting the Control Register
Your Name

void Initialize_RTC (void) Email Address *


{
Write_To__RTC(control_Register, 0x00);
Write_To__RTC(Status_Register, 0x08);
} SUBSCRIBE

When all the bits in the control register are set to zero, the oscillator will run its clock
until VBAT is applied.

But, it is not required for date and time. As temperature conversion is not required for
displaying time and date, set the CONV=0.

Bit Name Setting

0 A1IE 0

1 A2IE 0

2 INTCN 0

3 RS1 0

4 RS2 0

5 CONV 0

6 BBSQW 0

7 EOSC 0

#2 Initialize the RTC by setting the Status Register


https://www.codrey.com/microcontroller-projects/interfacing-rtc-ds3231-with-8051-microcontroller/ 3/19
26/9/2018 Interfacing RTC DS3231 with 8051 Microcontroller - Codrey Electronics

Status Register Byte:

Subscribe to get
This register monitors the status of time keeping registers such as date, month, year,
minute, hour and second.
updates
To enable this register set EN32kHz bit to ‘1’. Your Name

Bit Name Email Address


Setting *

0 A1F 0

1 A2F 0
SUBSCRIBE

2 BSY 0

3 EN32kHz 1

4 0 0

5 0 0

6 0 0

7 OSF 0

#3 Set the Date and Time using Time keeping registers

 RTC Registers in DS3231

RTC Address 0x68

Read Address D1

Write Address D0

Date 0x04

Month 0x05

https://www.codrey.com/microcontroller-projects/interfacing-rtc-ds3231-with-8051-microcontroller/ 4/19
26/9/2018 Interfacing RTC DS3231 with 8051 Microcontroller - Codrey Electronics

Year 0x06

Second Subscribe
0x00 to get

Minute updates
0x01
Your Name
Hour 0x02

Email Address *
Now, you can set the date and time parameters by using setTime and setDate functions.

The RTC has set with 0x60, 0x40 to enable hour format and AM/PM.

SUBSCRIBE
void setTime(unsigned char sethour, unsigned char setminute, unsigned char setsecond,
short am_pm, short hour_twelve_twentyfour)
{
unsigned char temp = 0;
Write_To__RTC(second, Convert_DECIMAL_BCD(setsecond));
Write_To__RTC(minute, Convert_DECIMAL_BCD(setminute));
switch(hour_twelve_twentyfour)
{
case 1:
{
switch(am_pm)
{
case 1:
{
temp = 0x60;
break;
}
default:
{
temp = 0x40;
break;
}
}
Write_To__RTC(hour,((temp|(0x1F&(Convert_DECIMAL_BCD(sethour))))));
break;
}

https://www.codrey.com/microcontroller-projects/interfacing-rtc-ds3231-with-8051-microcontroller/ 5/19
26/9/2018 Interfacing RTC DS3231 with 8051 Microcontroller - Codrey Electronics

default:
{
Write_To__RTC(hour, (0x3F & (Convert_DECIMAL_BCD(sethour))));
break;
Subscribe to get
} updates
}
Your Name
}

Email Address *
To set the day, date, month year use 04, 05, 06 address. The time and calendar registers
are prede ned in BCD format. So, we have to convert the Decimal format to BCD notation
to write data to RTC.

SUBSCRIBE
void setDate(unsigned char setday, unsigned char setdate, unsigned char setmonth,
unsigned char setyear)
{
Write_To__RTC(day, (Convert_DECIMAL_BCD(setday)));
Write_To__RTC(date, (Convert_DECIMAL_BCD(setdate)));
Write_To__RTC(month, (Convert_DECIMAL_BCD(setmonth)));
Write_To__RTC(year, (Convert_DECIMAL_BCD(setyear)));
}

After writing the data, we can read the timekeeping registers using following functions.

#4 Reading the date and Time

The calendar registers and timekeeping register in RTC gives BCD format. Hence, to read
the RTC date and time, convert BCD format to decimal notation.

void Readtime(void)
{
unsigned char temp = 0,hour_twelve_twentyfour,t;
RTCsec = Read_From_RTC(second);
RTCsec = Convert_BCD_DECIMAL (RTCsec);
RTCmin = Read_From_RTC (minute);

https://www.codrey.com/microcontroller-projects/interfacing-rtc-ds3231-with-8051-microcontroller/ 6/19
26/9/2018 Interfacing RTC DS3231 with 8051 Microcontroller - Codrey Electronics

RTCmin = Convert_BCD_DECIMAL (RTCmin);


hour_twelve_twentyfour=0; //For 24 hour type
switch (hour_twelve_twentyfour)
{
Subscribe to get
case 1: updates
{
Your Name
temp = Read_From_RTC(hour);
temp &= 0x20;
Email Address *
t= (short)(temp>> 5);
RTC_hour = (0x1F & Read_From_RTC(hour));
RTC_hour = Convert_BCD_DECIMAL(RTC_hour);
break;
} SUBSCRIBE
default:
{
RTC_hour = (0x3F & Read_From_RTC(hour));
RTC_hour = Convert_BCD_DECIMAL(RTC_hour);
break;
}
}
}
void ReadDate(void)
{
RTCyear = Read_From_RTC(year);
RTCyear = Convert_BCD_DECIMAL(RTCyear );
RTCmonth = (0x1F & Read_From_RTC(month));
RTCmonth = Convert_BCD_DECIMAL(RTCmonth );
RTCdate= (0x3F & Read_From_RTC(date));
RTCdate= Convert_BCD_DECIMAL(RTCdate);
RTCday= (0x07 & Read_From_RTC(day));
RTCday= Convert_BCD_DECIMAL(RTCday);
}

LCD Functions for RTC DS3231

You might struggle if you are working with LCD to display date and time. Because the
time and date format of RTC is in integer format. The problem is the LCD displays only
ASCII characters. Hence, I have written an integer to ASCII function for LCD.

https://www.codrey.com/microcontroller-projects/interfacing-rtc-ds3231-with-8051-microcontroller/ 7/19
26/9/2018 Interfacing RTC DS3231 with 8051 Microcontroller - Codrey Electronics

Function to Convert an Integer to ASCII format

1. Extract the integer.


Subscribe to get
2. Add 0x30 to convert into ASCII.
3. Store the ASCII character in the array. updates
4. Divide the integer to until zero. Your Name
5. Reverse the array to convert to original form.

Email Address *

void Show_Integer_Ascii(unsigned char lineno, unsigned char cursor_position, unsigned


long value)
{
unsigned char a[11], b[11], i, m, n; SUBSCRIBE
if (value == 0 && cursor_position == 4)
{
goto loop1;
}
else if (value == 0)
{
b[0] = '0';
b[1] = '\0';
goto loop;
}
loop1: for (i = 0; i < 11; i++)
{
a[i] = (value % 10) + 0x30;
value = value / 10;
if (value == 0)
break;
}
a[i + 1] = '\0';
n= 0;
for (m = i; m >= 0; m--) {
b[n] = a[m];
n++;
if (m == 0)
break;
}
b[n] = '\0';

https://www.codrey.com/microcontroller-projects/interfacing-rtc-ds3231-with-8051-microcontroller/ 8/19
26/9/2018 Interfacing RTC DS3231 with 8051 Microcontroller - Codrey Electronics

loop: if (lineno == 1)
Disp_Data(lineno, cursor_position, b);
if (lineno == 2)
Disp_Data(lineno, cursor_position, b);
Subscribe to get
memset(b,0,sizeof(b)); updates
}
Your Name

Displaying Date on LCD Email Address *

void Display_Date_LCD(void)
{
ReadDate(); SUBSCRIBE
Disp_Data(1, 7, "/");
if(RTCdate>=0 && RTCdate <=9)
{
Disp_Data (1, 5, "0");
Show_Integer_Ascii (1, 6, RTCdate);
}
else
{
Show_Integer_Ascii(1,5,RTCdate);
}
Disp_Data(1, 10, "/");
if(RTCmonth>=0 && RTCmonth <=9)
{
Disp_Data(1, 8, "0");
Show_Integer_Ascii(1,9,RTCmonth);
}
else
{
Show_Integer_Ascii(1,8,RTCmonth);
}
if(RTCyear>=0 && RTCyear <=9)
{
Disp_Data(1, 11, "0");
Show_Integer_Ascii(1,12,RTCyear);
}
else

https://www.codrey.com/microcontroller-projects/interfacing-rtc-ds3231-with-8051-microcontroller/ 9/19
26/9/2018 Interfacing RTC DS3231 with 8051 Microcontroller - Codrey Electronics

{
Show_Integer_Ascii(1,11,RTCyear);

}
}
Subscribe to get
updates
Displaying Time on LCD Your Name

Email Address *
void Readtime(void)
{
unsigned char temp = 0,hour_twelve_twentyfour,t;
RTCsec = Read_From_RTC(second);
RTCsec = Convert_BCD_DECIMAL(RTCsec); SUBSCRIBE
RTCmin = Read_From_RTC(minute);
RTCmin = Convert_BCD_DECIMAL(RTCmin);
hour_twelve_twentyfour=0; //For 24 hour type
switch(hour_twelve_twentyfour)
{
case 1:
{
temp = Read_From_RTC(hour);
temp &= 0x20;
t= (short)(temp>> 5);
RTC_hour = (0x1F & Read_From_RTC(hour));
RTC_hour = Convert_BCD_DECIMAL (RTC_hour);
break;
}
default:
{
RTC_hour = (0x3F & Read_From_RTC(hour));
RTC_hour = Convert_BCD_DECIMAL (RTC_hour);
break;
}
}
}

C code for RTC DS3231

https://www.codrey.com/microcontroller-projects/interfacing-rtc-ds3231-with-8051-microcontroller/ 10/19
26/9/2018 Interfacing RTC DS3231 with 8051 Microcontroller - Codrey Electronics

Here is the complete C code to read RTC date and time. I have used two pins of 8051
microcontroller to communicate with Real Time Clock using I2C.

Subscribe to get
#include<reg51.h> updates
#include"header.h"
void main() Your Name

{
lcd_init(); Email Address *
lcd_delay(2);
Disp_Data(1,0, "DS3231 Interface");
Disp_Data(2,0, " By Codrey.com ");
lcd_delay(10);
SUBSCRIBE
Disp_Data(1,0, " ");
Disp_Data(2,0, " ");
Initialize_RTC( ); /*Initializing RTC DS3231*/
lcd_delay(2);
setTime( 10,20,25,0,0); /*Set Hour,Minute,Second */
setDate( 2,16,05,18); /*Set Day,Date,Month,Year*/

/*In nite WHILE Loop for reading date and time of RTC*/
Disp_Data(1,0, "DATE");
Disp_Data(2,0, "TIME");
ReadDate( ); //Gets the Day,month and year
Display_Date_LCD( );

ReadTime(); /*Gets the time from DS3231*/


Display_Time_LCD( );
}

I2c Functions for RTC

Here are the steps to communicate with RTC using I2c.

1. Start the I2C communication using start condition.


2. Write the RTC address.
3. Read the acknowledgement.
4. Write the register write address.
5. Read the data from the RTC.
https://www.codrey.com/microcontroller-projects/interfacing-rtc-ds3231-with-8051-microcontroller/ 11/19
26/9/2018 Interfacing RTC DS3231 with 8051 Microcontroller - Codrey Electronics

6. Read the acknowledegment


7. Write no acknowledgement.
8. Stop the i2c communication.
Subscribe to get
updates
void Start_I2C(void)
{ Your Name

SCL_DISABLE;
SDA_ENABLE; Email Address *
SCL_ENABLE;
SDA_DISABLE;
}
void Slave_Write(unsigned char byte)
SUBSCRIBE
{
unsigned char j,k;
k = byte;
for (j= 0; j < 8; j++)
{
SCL_DISABLE;
SDA=(k&(0x80>>j))?1:0; /*Writing bit_by_bit*/
SCL_ENABLE;
}
}
void Read_Ack(void)
{
SCL_DISABLE;
SDA_ENABLE;
SCL_ENABLE;
while(SDA);
}
void Read_Ack(void)
{
SCL_DISABLE;
SDA_ENABLE;
SCL_ENABLE;
while(SDA);
}

void No_Acknowledgement(void)

https://www.codrey.com/microcontroller-projects/interfacing-rtc-ds3231-with-8051-microcontroller/ 12/19
26/9/2018 Interfacing RTC DS3231 with 8051 Microcontroller - Codrey Electronics

{
SCL_DISABLE;
SDA_ENABLE;
SCL_ENABLE;
Subscribe to get
} updates
unsigned char Read_I2C_Data(void)
Your Name
{
unsigned char m,data_byte=0;
Email Address *
for(m=0;m<8;m++)
{
SCL_DISABLE;
SCL_ENABLE;
if(SDA) SUBSCRIBE
data_byte|=0x80>>m;
}
return data_byte;
}

unsigned char Read_From_RTC(unsigned char Address)


{
unsigned char val= 0;
Start_I2C();
Slave_Write (DS3231_Write);
Read_Ack();
Slave_Write (Address);
Read_Ack();
Start_I2C();
Slave_Write (DS3231_Read);
Read_Ack();
val=Read_I2C_Data();
No_Acknowledgement( );
Stop_I2C();
return val;
}

void Write_To__RTC(unsigned char regaddress, unsigned char val)


{
unsigned char loop_time;
Start_I2C();

https://www.codrey.com/microcontroller-projects/interfacing-rtc-ds3231-with-8051-microcontroller/ 13/19
26/9/2018 Interfacing RTC DS3231 with 8051 Microcontroller - Codrey Electronics

Slave_Write(DS3231_Write);
Read_Ack();
Slave_Write(regaddress);
Read_Ack();
Subscribe to get
Slave_Write(val); updates
Read_Ack();
Your Name
Stop_I2C();
for(loop_time=0;loop_time<255;loop_time++);
Email Address *
for(loop_time=0;loop_time<255;loop_time++);
}

Proteus Simulation SUBSCRIBE

Applications of DS3231
1. Used in GPS for getting date, time, latitude, longitude, position, height
2. Used in servers for implementing network timing protocol
3. Used in Smart power meters for tracking the date and time of current bills.

https://www.codrey.com/microcontroller-projects/interfacing-rtc-ds3231-with-8051-microcontroller/ 14/19
26/9/2018 Interfacing RTC DS3231 with 8051 Microcontroller - Codrey Electronics

4. Used in telecommunication and networking applications.

Final Thoughts Subscribe to get


RTC DS3231 is precise in maintaining accurate time updates
with low drift. This makes it a perfect
blend for timing applications.
Your Name

DS3231 is highly accurate that manages date, time, year, month, day, and seconds.
Email Address *

Moreover, RTC is equipped with ±2ppm accuracy suitable for real time functions.

I hope you have understand how to interface DS3231 RTC with 8051 using I2C. As 8051
has no internal i2c, I have used bit banging method toSUBSCRIBE
generate clock and data for serial
communication.

The entire code has been tested with 8051 microcontroller and it will work with any
microcontroller with minor changes.

Attachments

Interfacing RTC DS3231 with 8051


File size: 83 KB

Related Posts:
1. Using Real Time Clock (RTC) DS3231 with 8051 Microcontroller

FILED UNDER: 8051 MICROCONTROLLER PROJECTS, MICROCONTROLLER PROJECTS


TAGGED WITH: RTC DS3231

https://www.codrey.com/microcontroller-projects/interfacing-rtc-ds3231-with-8051-microcontroller/ 15/19
26/9/2018 Interfacing RTC DS3231 with 8051 Microcontroller - Codrey Electronics

Leave a Reply
Subscribe to get
Your email address will not be published. Required elds are marked *
updates
Comment Your Name

Email Address *

SUBSCRIBE
Name *

Email *

Website

Notify me of follow-up comments by email.

Notify me of new posts by email.

POST COMMENT

Search this website …

https://www.codrey.com/microcontroller-projects/interfacing-rtc-ds3231-with-8051-microcontroller/ 16/19
26/9/2018 Interfacing RTC DS3231 with 8051 Microcontroller - Codrey Electronics

Subscribe to get
updates
Your Name

Email Address *

SUBSCRIBE

Recent Posts

› Loafer’s LED Wick

› Simple Thermostat for Electric Geyser

› Breathing New Life into BLDC Motors

› Over Voltage Protector (OVP) Switch

› Optical Smoke Alarm

Categories

› 555 Timer Circuits (3)

› 8051 Microcontroller Projects (2)

› Analog Circuits (4)

› Analog Electronics (2)

› Arduino Projects (10)

https://www.codrey.com/microcontroller-projects/interfacing-rtc-ds3231-with-8051-microcontroller/ 17/19
26/9/2018 Interfacing RTC DS3231 with 8051 Microcontroller - Codrey Electronics

› Books (1)

Subscribe to get
Circuit Elements (2)

updates
DC Circuits (21)

Your Name
› Electronic Circuits (46)

Email Address *
› Electronics (11)

› Embedded Systems (7)

› Fundamentals of Electricity (19)


SUBSCRIBE

› Learn (2)

› Microcontroller (1)

› Microcontroller Projects (4)

› Microcontrollers (1)

› PIC Microcontroller Projects (2)

› Power Supply (1)

› Resistor (5)

› Serial Communication (2)

› Solar Projects (1)

Tags

16x2 LCD (1) 555 Timer Projects (1) AC Circuits (4) AD584
(1) Arduino Nano (2) Attiny85 (3) Battery Charger

Circuits (3) BC547 (2) Career (1) DC Circuits

https://www.codrey.com/microcontroller-projects/interfacing-rtc-ds3231-with-8051-microcontroller/ 18/19
26/9/2018 Interfacing RTC DS3231 with 8051 Microcontroller - Codrey Electronics

(10) Digispark (3) DIY

Electronics to
Subscribe (46)
get
Embedded (3)

updates
ESP8266 (2) General Topics (14) GPS (1)

HYT 271 (1) I2C Protocol (1) Infrared (IR) (1) LED Circuits
Your Name
(5) Li-Ion Battery (1) LM358 (1) LM7805 (1) PIR Motion Sensor (1)
Pulse Width Modulation (PWM) (1) QX5252F (1) Radio
Email Address *
Frequency (RF) (2) Renewable Energy (1) Repellent
Circuits (2) RS232 (3) RS422 (1) RS485 (1) RTC DS3231 (2)
Sensor (2) Serial Communication (1) SG3525 (1) Solar

Circuits (2) SS8550 (1) Teardown (1) Tested Circuit (5)


SUBSCRIBE
Thermistor Circuits (1) Tone Decoder (1) TP4056 (1) USB (2)
WiFi (1)

Copyright © 2017-2018 Codrey Electronics | Privacy Statement | Terms of Use | Contact us .

https://www.codrey.com/microcontroller-projects/interfacing-rtc-ds3231-with-8051-microcontroller/ 19/19

You might also like