DS1307 RTC driver for CCS C compiler

This topic shows how to easily interface the DS1307 real time clock chip with PIC16F877A microcontroller where time and date are displayed on 16×2 LCD screen.
The compiler used in this example is CCS C.

The DS1307 is an 8-pin integrated circuit that uses I2C protocol to communicate with master device (microprocessor, microcontroller …). This small chip can count seconds, minutes, hours, day of the week, day of month, month and year with leap-year up to year 2100.

The following image shows the DS1307 pin configurations:

DS1307 RTC pin configuration

And the following image shows a typical operating circuit for the DS1307 RTC:

DS1307 RTC basic circuit

A 3V battery can be connected between VBAT and GND which can be used as a backup supply input. With this battery the DS1307 keeps time running even if the main power source is off.
The DS1307 uses an external 32.768KHz crystal and there is no need to add any resistors or capacitors with it.
More information are in the DS1307 RTC datasheet.

DS1307 RTC driver (library) for CCS C compiler:
To simplify project C code, I wrote a small library for the DS1307. Library functions are listed below.

void RTC_Set(RTC_Time *time_t): this function sets time and date (writes time and date to the DS1307) where the variable time_t of type struct (structure) which collects the following unsigned 8-bit integer variables:
seconds, minutes, hours, dow, day, month and year.
Where dow is day of the week (Monday, Tuesday …) and day is month day.
seconds and minutes range: 0 – 59
hours range: 0 – 23 (24-hour format)
dow range: 1 – 7 (1 equals Sunday, 2 equals Monday …)
day range: 1 – 31 (depends on month)
month range: 1 – 12 (1 equals January)
year range: 0 – 99.

RTC_Time *RTC_Get(): reads time and date from the DS1307, this function returns them as a variable of type struct (same as the previous function).

void OSC_Enable(): enables the DS1307 oscillator.
void OSC_Disable(): disables the DS1307 oscillator.

uint8_t RTC_Read_Reg(uint8_t reg_address): this function returns the value stored in register of address reg_address.
void RTC_Write_Reg(uint8_t reg_address, uint8_t reg_value): writes reg_value to register of address reg_address.

void SQWE_Set(SQWE pin_out): this function configures the SQW/OUT pin of the DS1307, where pin_out is one of the following:
OUT_OFF: output is off (internally attached to ground)
OUT_ON: output is on (internally floating)
OUT_1Hz: output is square wave with frequency equals to 1Hz
OUT_4096Hz: output is square wave with frequency equals to 4096Hz
OUT_8192Hz: output is square wave with frequency equals to 8192Hz
OUT_32768Hz: output is square wave with frequency equals to 32768Hz

DS1307 driver for CCS C compiler download:
Driver source file download link is below, installation of this driver is easy, just add it to project folder or CCS C drivers folder (for example C:\Program Files (x86)\CCS\Drivers). Driver name is: DS1307.c.
DS1307 CCS C driver

DS1307 Library Example:
This is a small example that shows how to use this library.
In this example PIC16F877A microcontroller is used, hardware circuit diagram is shown below.

DS1307 with PIC16F877A microcontroller and LCD circuit

All grounded terminals are connected together.

The 1602 LCD screen is used to display time and date where:
RS —> pin RD0
RW —> pin RD1
E   —> pin RD2
D4 —> pin RD3
D5 —> pin RD4
D6 —> pin RD5
D7 —> pin RD6
VSS, D0, D1, D2, D3 and K are connected to circuit ground
VEE to the variable resistor (or potentiometer) output
VDD to +5V and A to +5V through 330 ohm resistor.

VEE pin is used to control the contrast of the LCD. A (anode) and K (cathode) are the back light LED pins.

In this project the PIC16F877A microcontroller runs with 8 MHz crystal oscillator.

CCS C code:
The C code below is for CCS C compiler, it was tested with version 5.051.

To be able to compile project C code with no error, a driver (library) for the DS1307 RTC is required, download link is above.
After you download the driver file which named DS1307.c, add it to your project folder.

Proteus simulation as well as hardware circuit of the example should give the result shown below (RTC starts updating immediately):

DS1307 with PIC16F877A Proteus simulation

Other projects where DS1307 RTC driver is used:
Interfacing PIC18F46K22 microcontroller with DS1307 RTC


Discover more from Simple Circuit

Subscribe to get the latest posts sent to your email.

1 thought on “DS1307 RTC driver for CCS C compiler”

Leave a Comment

Your email address will not be published. Required fields are marked *

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

Scroll to Top