Beruflich Dokumente
Kultur Dokumente
Subscribe to get
updates
Your Name
Email Address *
PROGRAMMING
SUBSCRIBE
YOU ARE HERE: HOME / MICROCONTROLLER PROJECTS / INTERFACING RTC DS3231 WITH 8051 MICROCONTROLLER
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]
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
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.
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
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.
0 A1IE 0
1 A2IE 0
2 INTCN 0
3 RS1 0
4 RS2 0
5 CONV 0
6 BBSQW 0
7 EOSC 0
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
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
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.
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
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
Email Address *
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
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;
}
}
}
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( );
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;
}
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++);
}
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
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
Related Posts:
1. Using Real Time Clock (RTC) DS3231 with 8051 Microcontroller
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
POST COMMENT
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
Categories
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)
› Learn (2)
› Microcontroller (1)
› Microcontrollers (1)
› Resistor (5)
Tags
16x2 LCD (1) 555 Timer Projects (1) AC Circuits (4) AD584
(1) Arduino Nano (2) Attiny85 (3) Battery Charger
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
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
https://www.codrey.com/microcontroller-projects/interfacing-rtc-ds3231-with-8051-microcontroller/ 19/19