8051 – RTC Interfacing (Alarm Clock)

In our previous tutorial, we have interfaced the 8051 with EEPROM. In this project, we are going to see RTC Interfacing with 8051. Before that, we should know about the RTC (DS1307) operations. Let’s start.

You can also read PIC16F877A RTC, LPC2148 ARM7 Introduction.

Prerequisites

RTC (DS1307)

Introduction

The DS1307 serial real-time clock (RTC) is a low-power, full binary-coded decimal (BCD) clock/calendar plus 56 bytes of NV SRAM. Address and data are transferred serially through an I²C, bidirectional bus. 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. The DS1307 has a built-in power sense circuit that detects power failures and automatically switches to the backup supply. Timekeeping operation continues while the part operates from the backup supply.

Features

  • Completely Manages All Timekeeping Functions
    • Real-Time Clock Counts Seconds, Minutes, Hours, Date of the Month, Month, Day of the Week, and Year with Leap-Year Compensation Valid Up to 2100
    • 56-Byte, Battery-Backed, General-Purpose RAM with Unlimited Writes
    • Programmable Square-Wave Output Signal
  • Simple Serial Port Interfaces to Most Microcontrollers
    • I2C Serial Interface
  • Low Power Operation Extends Battery Backup Run Time
    • Consumes Less than 500nA in Battery-Backup Mode with Oscillator Running
    • Automatic Power-Fail Detect and Switch Circuitry
  • 8-Pin DIP and 8-Pin SO Minimizes Required Space
  • Optional Industrial Temperature Range: -40°C to +85°C Supports Operation in a Wide Range of Applications
  • Underwriters Laboratories® (UL) Recognized

VCC, GND: These pins are used to provide power to the chip. When 5V is applied within normal limits, the device is fully accessible and data can be written and read. When a 3V battery is connected to the device and VCC is below 1.25 x VBAT, reads and writes are inhibited. However, the timekeeping function continues unaffected by the lower input voltage. As VCC falls below VBAT the RAM and timekeeper are switched over to the external power supply (nominal 3.0V DC) at VBAT.

X1-X2:Pins to connect the external 32.768kHz oscillator that provides the clock source to the chip.

Vbat: A 3.3v Lithium battery can be connected to this pin in order to provide the power source when the external supply voltage is not available. Battery voltage must be held between 2.0V and 3.5V for proper operation.

SCL: This pin must be connected to the SCL pin of the I2C Bus/Master.

SDA: This pin must be connected to the SDA pin of the I2C Bus/Master.

SQW/OUT: When enabled, the SQWE bit set to 1, the SQW/OUT pin outputs one of four square wave frequencies (1Hz, 4kHz, 8kHz, 32kHz).

  • Note: The SCL, SDA, and SQW are open drain and must be pulled up with appropriate pull-up resistors

Connection circuit

DS1307 requires a very less number of components to operate. One of the mandatory components is the crystal oscillator of 32.768kHz. The battery backup is optional as you can connect the power source to the Vbatt pin. But It is good to have the battery connected. When running on battery it only consumes 500 nA of current just for the operation of the clock and to maintain the content of NV RAM. You can use a coin cell-type battery as a backup source for DS1307.

The following image shows a typical connection diagram for DS1307 RTC.

DS1307 Memory

All the registers of DS1307 store the value as BCD, i.e. if the clock is at 49 seconds then register 0x00 will show 0x49 as a binary coded decimal value of 49. This makes it easy for programmers to read and display numbers on LCD or serial terminals. So if you want to use register values of RTC then first convert the value from BCD to decimal and then use it.

The RTC keeps the date and time arranged in its memory as shown below:

Register 0x07 is the control register for the square wave output pin. RS0 and RS1 bit select the output frequency as per the table below:

You can also use SQW/Out pin as a GPO pin when the SQW function of DS1307 is not used. Bit 7 controls the output level of the pin. If OUT bit is 1 then the OUT pin is high and when 0 OUT pin will be LOW.

RTC Interfacing with 8051

DS1307 uses I2C Protocol and acts as a slave device and I2C Master can read/write the register of RTC. To communicate with the slave device, the master needs the slave address of a device connected to the bus. DS1307 has a fixed slave address which makes it impossible to connect two RTC devices on the same bus, don’t worry occurrence of such a scenario is close to zero.
Slave address for I2C Write: 0b11010000 = 0xD0
Slave address for I2C Read: 0b11010001 = 0xD1

Components Required

  • 8051 Development board
  • LCD Module
  • Keypad
  • RTC DS1307
  • Buzzer

Circuit Diagram

EXPLANATION

  • SDA – P2.0, SCL – P2.1, Set button – P3.2, Alarm button – P3.3.

  • The LCD data lines are given to Port1. RS is given to port pin P2.7, RW to P2.6, and enable to P2.5.

  • The keypad is connected to P0.

  • The buzzer is connected to P2.3.

RTC Interfacing with 8051 – Programming

In this code, I’m printing the Time, Date in LCD Module. There are two switches. One is for set alarm and another one is for set time and date. The keypad is used to enter the time and date. Whenever time is matching to alarm, buzzer makes a sound to indicate us. Here I have posted only the main code. If you want to download the full project, please click here.

void main()
{
    unsigned char sec, min, hour, date, month, year;
    buz=0;
    lcd_init();
    cmd(0x80);
    show("  DIGITAL CLOCK ");
    cmd(0xc0);
    show("   WITH ALARM   "); 
    del(60000);
    cmd(0x01);
    buz=1;
    while(1) {
        cmd(0x80);
        show("TIME  : ");

        sec   = get_rtc(0x00);  
        min   = get_rtc(0x01);  
        hour  = get_rtc(0x02);
        date  = get_rtc(0x04);
        month = get_rtc(0x05);
        year  = get_rtc(0x06);

        if((ho==hour) && (mi==min)) {
            buz=0;
            del(8000);
            buz=1;
            del(2000);
            buz=0;
            del(8000);
            buz=1;
        } else
            buz=1;

        if((ho==100)&&(mi==100)) {
            disp_date(date,month,year);
        } else {
            cmd(0xc0);
            show("Alarm :         ");
            cmd(0xc8);
            dat((ho/16)+48);
            dat((ho%16)+48);
            dat(' ');
            dat(':');
            dat(' ');
            dat((mi/16)+48);
            dat((mi%16)+48);
            show("  ");
        }    
        if(set==0)
            seT();
        if(alarm==0)
            al_set();

        disp_time(hour,min,sec);
    }
}

Hardware Setup

RTC Interfacing with 8051

RTC Interfacing with 8051 – Output

EXPLANATION

  • Here RTC DS1307 is connected into 8051 with help of I2C.

  • The details of time and date are given into the microcontroller

  • The keypad is used to set the time, date, and alarm.

  • LCD is used to display the time and date.

  • If an alarm occurs buzzer will be activated.

Watch On Youtube: Digital Alarm Clock

                                      [Download this Project ]

If you want PPT and Report for this Project Please contact us. In the subject please mention the project name.

In our next tutorial, we will see how to interface the PIR sensor with 8051.

You can also read the below tutorials.

Linux Device Driver TutorialsC Programming Tutorials
FreeRTOS TutorialsNuttX RTOS Tutorials
RTX RTOS TutorialsInterrupts Basics
I2C Protocol – Part 1 (Basics)I2C Protocol – Part 2 (Advanced Topics)
STM32 TutorialsLPC2148 (ARM7) Tutorials
PIC16F877A Tutorials8051 Tutorials
Unit Testing in C TutorialsESP32-IDF Tutorials
Raspberry Pi TutorialsEmbedded Interview Topics
Reset Sequence in ARM Cortex-M4BLE Basics
VIC and NVIC in ARMSPI – Serial Peripheral Interface Protocol
Bootloader Tutorials
5 1 vote
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x