Skip to content

gokulkrishnaks/PIC-rtc-clock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

PIC16F877A Real-Time Clock (RTC) with LCD Display using DS1307

This project demonstrates interfacing a DS1307 Real-Time Clock (RTC) module with a PIC16F877A microcontroller and displaying the current time and date on a 16x2 LCD. The communication with the RTC is handled through I2C protocol using the MSSP module of PIC.

Project Description

  • Microcontroller: PIC16F877A (PIC16 Series)

  • Features:

    • Real-time Clock functionality using DS1307 RTC
    • Display Time (HH:MM:SS) and Date (DD/MM/YY) on 16x2 LCD
    • I2C Communication (MSSP Module)
    • BCD to ASCII conversion for LCD display
    • Supports Proteus Simulation and Real Hardware Implementation
  • Application: Digital Clock Systems, Embedded Real-Time Applications, Academic Projects.

Hardware Requirements

  • PIC16F877A Development Board
  • 16x2 LCD (HD44780 Compatible)
  • DS1307 RTC Module with Battery Backup
  • Pull-up Resistors (4.7kΩ) for I2C Lines
  • Crystal Oscillator (20 MHz)
  • Capacitors (22pF for oscillator)
  • Power Supply (5V regulated)
  • Breadboard, Jumpers, Resistors

Software Requirements

  • MPLAB X IDE + XC8 Compiler (for code compilation & HEX generation)
  • Proteus Design Suite (for circuit simulation)
  • PICkit2/3 Programmer (if flashing to hardware)

Project Folder Structure

pic-rtc-clock/
├── src/
│   └── main.c               # C Source Code for RTC & LCD Interfacing
├── simulation/
│   └── project.pdsprj       # Proteus simulation file
└── README.md                # Project Overview & Instructions

Steps to Compile HEX File in MPLAB X IDE

  1. Open MPLAB X IDE.

  2. Create a New Project:

    • File > New Project > Standalone Project
    • Select Family: Mid-Range 8-bit MCUs (PIC16)
    • Device: PIC16F877A
  3. Add Source File:

    • Right-click 'Source Files' > Add Existing Item.
    • Select main.c from /src/.
  4. Configure Compiler Settings:

    • Go to Project Properties.
    • Set XC8 Compiler options if needed.
  5. Build the Project:

    • Click Build Project (Hammer Icon).
    • HEX file will be generated in the /dist/ folder.

Running the Simulation in Proteus

Simulation Prerequisites:

  • Proteus component libraries:

    • PIC16F877A Microcontroller
    • DS1307 RTC Module
    • HD44780 LCD (16x2)
    • Pull-up Resistors for I2C lines
    • Crystal Oscillator (20MHz) + Capacitors

Simulation Steps:

  1. Open Proteus Design Suite.

  2. Open the Provided Simulation File:

    • Navigate to /simulation/ folder.
    • Open rtc-clock.pdsprj.
  3. Load HEX File to PIC16F877A:

    • Double-click PIC16F877A symbol.
    • In Program File field, browse and select the compiled HEX file.
  4. Run Simulation:

    • Click Play (Run Simulation).

Operation Flow:

  • LCD will initially display “Time:” on Line 1 and “Date:” on Line 2.
  • After setting initial time via code, the RTC will maintain timekeeping.
  • LCD will continuously update current time in HH:MM:SS and date in DD/MM/YY format every half second.

How the Code Works

  • LCD is interfaced in 8-bit mode via PORTD (Data lines) and PORTB (Control lines RB0-RB2).
  • DS1307 communicates over I2C using MSSP Module on PORTC (SCL/SDA).
  • The code initializes the RTC with a default time/date and continuously reads current values.
  • Data from RTC (in BCD) is converted to ASCII and displayed on LCD.
  • I2C start, stop, read, write sequences are handled using SSPCON2bits for reliable communication.

Flowchart

Start
  |
  |---> Initialize LCD Display
  |---> Initialize I2C (MSSP Module)
  |---> Write Default Time & Date to DS1307
  |
Main Loop:
  |
  |---> Generate I2C START Condition
  |---> Send RTC Slave Address with Write Bit
  |---> Send Register Address to Read From (Seconds)
  |---> Generate Repeated START Condition
  |---> Send RTC Slave Address with Read Bit
  |---> Sequentially Read 7 Bytes (Time & Date)
  |---> Generate I2C STOP Condition
  |
  |---> Convert BCD Values to ASCII
  |---> Display Time on LCD Line 1 (HH:MM:SS)
  |---> Display Date on LCD Line 2 (DD/MM/YY)
  |
Repeat Main Loop

Notes

  • The LCD operates in 8-bit mode, connected to PORTD.
  • The RS, RW, EN control signals are connected to RB0, RB1, RB2 respectively.
  • I2C Lines (SCL/SDA) on RC3/RC4 require external 4.7kΩ pull-up resistors.
  • The initial time is set in code via DS1307_write() function.
  • The RTC maintains time using its onboard battery, even if PIC is powered off.

Troubleshooting

  • LCD Not Displaying?

    • Check contrast potentiometer and LCD wiring.
    • Ensure correct control pins (RB0, RB1, RB2).
  • RTC Not Communicating?

    • Verify pull-up resistors on SCL/SDA lines.
    • Check crystal oscillator stability.
  • Incorrect Time/Date Displayed?

    • Ensure initial time values are correctly written to DS1307.
    • Verify BCD to ASCII conversion logic.
  • Simulation Lags/Freezes?

    • Reduce simulation speed or increase system clock in Proteus.

License

This project is licensed under the MIT License. You are free to use, modify, and distribute this code and simulation files.

Preview (Add Screenshots in /docs/)

  • Simulation Circuit Diagram (RTC + LCD + PIC16F877A)
  • LCD displaying Real-Time Clock Interface
  • Close-up of I2C connections with pull-up resistors

Author

GOKUL KRISHNA K S

About

PIC16F877A Real-Time Clock Display using DS1307 RTC Module and 16x2 LCD via I2C

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages