top of page
Writer's pictureSanskruti Ashtikar

Real-Time Clock and Calendar Using 8051 Microcontroller

Updated: 4 days ago

Introduction


A real-time clock (RTC) and calendar system keeps track of the current time and date. This article explores the design, components, and implementation of a real-time clock and calendar using the 8051 microcontroller. The project utilizes an RTC module to maintain accurate timekeeping and a display to show the time and date.





Required


  1. 8051 Microcontroller: The core processing unit for the project.

  2. DS1307 RTC Module: Keeps track of the current time and date.

  3. I2C Bus: Used for communication between the microcontroller and RTC module.

  4. LCD Display: To display the time and date.

  5. Push Buttons: For setting the time and date.

  6. Resistors, Capacitors, and other Passive Components: For building the supporting circuitry.

  7. Power Supply: To provide necessary voltage to the circuit.

  8. Programming Cable and Software: For uploading the code to the microcontroller.


Working Principle


The DS1307 RTC module maintains the current time and date even when the power is off, thanks to its built-in battery backup. The 8051 microcontroller communicates with the RTC module using the I2C protocol to read the time and date. The microcontroller then displays this information on an LCD screen. Push buttons are used to set the current time and date.


Circuit Design


  1. Microcontroller (8051): The core of the project, interfaced with the RTC module and LCD.

  2. DS1307 RTC Module: Communicates with the microcontroller via I2C.

  3. LCD Display: Shows the current time and date.

  4. Push Buttons: Allow the user to set the time and date.


Circuit Diagram

Here's a simplified connection outline:

  • 8051 Microcontroller Pins:

  • P1: Connected to Data Lines of the LCD.

  • P2: Control Lines for the LCD.

  • P3.0 - P3.1: Connected to the I2C bus for RTC communication.

  • P3.2 - P3.4: Connected to push buttons.


Software Implementation


  1. Initializing I/O Ports: Set up ports for controlling the LCD, reading button inputs, and I2C communication.

  2. I2C Communication: Establish communication with the DS1307 RTC module.

  3. Reading Time and Date: Continuously read the time and date from the RTC module.

  4. Display Update: Show the current time and date on the LCD.

  5. Button Handling: Set the time and date using the push buttons.



#include <reg51.h>
#include <stdio.h>
sbit RS = P2^0; // LCD control pin RS
sbit RW = P2^1; // LCD control pin RW
sbit EN = P2^2; // LCD control pin EN
sbit SCL = P3^0; // I2C clock pin
sbit SDA = P3^1; // I2C data pin
sbit SET_BUTTON = P3^2; // Set button
sbit INC_BUTTON = P3^3; // Increment button
sbit DEC_BUTTON = P3^4; // Decrement button
unsigned char time[7]; // Array to store time and date values
void delay(unsigned int time);
void lcd_init(void);
void lcd_cmd(unsigned char cmd);
void lcd_data(unsigned char data);
void lcd_string(char *str);
void i2c_start(void);
void i2c_stop(void);
void i2c_write(unsigned char data);
unsigned char i2c_read(void);
void ds1307_write(unsigned char address, unsigned char data);
unsigned char ds1307_read(unsigned char address);
void set_time(void);
void display_time(void);
void main(void) {
    lcd_init(); // Initialize LCD
    lcd_string("RTC with 8051");
    delay(2000);
    lcd_cmd(0x01); // Clear LCD display
    while (1) {
        if (SET_BUTTON == 0) { // Check if set button is pressed
            delay(1000); // Debounce delay
            set_time(); // Set the time and date
        }
        display_time(); // Display the current time and date
        delay(1000);
    }
}
// Function to initialize LCD
void lcd_init(void) {
    lcd_cmd(0x38); // 8-bit mode, 2 lines, 5x7 matrix
    lcd_cmd(0x0C); // Display ON, cursor OFF
    lcd_cmd(0x06); // Increment cursor
    lcd_cmd(0x01); // Clear display
    delay(10);
}
// Function to send command to LCD
void lcd_cmd(unsigned char cmd) {
    P1 = cmd; // Send command to data pins
    RS = 0; // Command mode
    RW = 0; // Write operation
    EN = 1; // Enable pulse
    delay(1);
    EN = 0;
}


// Function to send data to LCD
void lcd_data(unsigned char data) {
    P1 = data; // Send data to data pins
    RS = 1; // Data mode
    RW = 0; // Write operation
    EN = 1; // Enable pulse
    delay(1);
    EN = 0;
}
// Function to display string on LCD
void lcd_string(char *str) {
    int i;
    for (i = 0; str[i] != '\0'; i++) {
        lcd_data(str[i]);
    }
}
// Function to start I2C communication
void i2c_start(void) {
    SDA = 1;
    SCL = 1;
    SDA = 0;
    SCL = 0;
}
// Function to stop I2C communication
void i2c_stop(void) {
    SDA = 0;
    SCL = 1;
    SDA = 1;
}
// Function to write data via I2C
void i2c_write(unsigned char data) {
    int i;
    for (i = 0; i < 8; i++) {
        SDA = (data & 0x80) >> 7;
        SCL = 1;
        data <<= 1;
        SCL = 0;
    }
    SDA = 1;
    SCL = 1;
    SCL = 0;
}
// Function to read data via I2C
unsigned char i2c_read(void) {
    unsigned char data = 0;
    int i;
    for (i = 0; i < 8; i++) {
        SCL = 1;
        data = (data << 1) | SDA;
        SCL = 0;
    }
    return data;
}
// Function to write data to DS1307 RTC
void ds1307_write(unsigned char address, unsigned char data) {
    i2c_start();
    i2c_write(0xD0); // DS1307 address + write
    i2c_write(address);
    i2c_write(data);
    i2c_stop();
}
// Function to read data from DS1307 RTC
unsigned char ds1307_read(unsigned char address) {
    unsigned char data;
    i2c_start();
    i2c_write(0xD0); // DS1307 address + write
    i2c_write(address);
    i2c_start();
    i2c_write(0xD1); // DS1307 address + read
    data = i2c_read();
    i2c_stop();
    return data;
}


// Function to set the time and date
void set_time(void) {
    unsigned char i;
    for (i = 0; i < 7; i++) {
        time[i] = 0;
    }
    // Set seconds
    while (1) {
        lcd_cmd(0x01); // Clear LCD display
        lcd_string("Set Seconds: ");
        lcd_data(time[0] / 10 + '0');
        lcd_data(time[0] % 10 + '0');
        if (INC_BUTTON == 0) { // Increment button
            delay(1000); // Debounce delay
            time[0]++;
            if (time[0] == 60) time[0] = 0;
        }
        if (DEC_BUTTON == 0) { // Decrement button
            delay(1000); // Debounce delay
            if (time[0] == 0) time[0] = 59;
            else time[0]--;
        }
        if (SET_BUTTON == 0) { // Set button
            delay(1000); // Debounce delay
            ds1307_write(0x00, time[0]);
            break;
        }
    }
    // Repeat the same process for minutes, hours, day, date, month, and year
    // Code omitted for brevity
}
// Function to display the time and date
void display_time(void) {
    unsigned char i;
    for (i = 0; i < 7; i++) {
        time[i] = ds1307_read(i);
    }
    lcd_cmd(0x01); // Clear LCD display
    lcd_string("Time: ");
    lcd_data(time[2] / 10 + '0');
    lcd_data(time[2] % 10 + '0');
    lcd_data(':');
    lcd_data(time[1] / 10 + '0');
    lcd_data(time[1] % 10 + '0');
    lcd_data(':');
    lcd_data(time[0] / 10 + '0');
    lcd_data(time[0] % 10 + '0');
    lcd_cmd(0xC0); // Move to second line
    lcd_string("Date: ");
    lcd_data(time[4] / 10 + '0');
    lcd_data(time[4] % 10 + '0');
    lcd_data('/');
    lcd_data(time[5] / 10 + '0');
    lcd_data(time[5] % 10 + '0');
    lcd_data('/');
    lcd_data(time[6] / 10 + '0');
    lcd_data(time[6] % 10 + '0');
}
// Delay function
void delay(unsigned int time) {
    int i, j;
    for (i = 0; i < time; i++)
        for (j = 0; j < 1275; j++);
}




Explanation


  1. I2C Communication: The I2C protocol is used for communication between the 8051 microcontroller and the DS1307 RTC module. The microcontroller acts as the master, and the RTC module is the slave device.

  2. Reading Time and Date: The ds1307_read function reads data from the RTC module. The data is then formatted and displayed on the LCD.

  3. Setting Time and Date: The set_time function allows the user to set the current time and date using push buttons. The values are written to the RTC module using the ds1307_write function.

  4. Display: The time and date are continuously updated and displayed on the LCD.


Conclusion


Building a real-time clock and calendar using the 8051 microcontroller and the DS1307 RTC module is an excellent project to learn about interfacing microcontrollers with external peripherals. The use of the I2C protocol for communication and the ability to set and display the time and date makes this project both practical and educational. With further enhancements, such as adding alarms or integrating with other home automation systems, this project can be expanded into a more complex and feature-rich system.


Want us to guide you through your project or make the project for you ?






Create Various Projects

Check out our Free Arduino Projects Playlist - Arduino Projects 

Check out our Free Raspberry Pi Projects Playlist - Raspberry Pi Projects 

Check out our Free TinkerCAD Projects Playlist - TinkerCAD Projects 

Check out our Free IoT Projects Playlist - IoT Projects 

Check out our Free Home Automation Projects Playlist - Home Automation Projects 

Check out our Free NodeMCu Projects Playlist - NodeMCu Projects 



1 view0 comments

Related Posts

See All

Kommentare