As an Amazon Associate we earn from qualifying purchases.
This project connects a PIC16F877A microcontroller to a DS1307 real time clock (RTC) and shows the date and time on a 16×2 LCD. The program is written in MikroC. It has one function to read the clock and one to set it, and the two communicate over I2C.
If you only need the basics of the chip, see our page on the RTC DS1307 module. This article focuses on the MikroC code.
What the DS1307 does
The DS1307 is a low-power clock and calendar chip. It keeps counting seconds, minutes, hours, day of the week, date, month and year, even while the microcontroller is off, as long as a backup battery is connected. It talks to the microcontroller over a two-wire I2C bus (SDA and SCL).
Some points about the chip that matter for the code:
- Time and date are stored in registers 0x00 to 0x06, in this order: seconds, minutes, hours, weekday, date, month, year.
- Values are stored in BCD (binary coded decimal). The number 25 is stored as 0x25, not 0x19.
- Bit 7 of the seconds register is the clock halt (CH) bit. When it is 1, the clock is stopped.
- The chip uses the I2C address 0x68. With the read/write bit added, this becomes 0xD0 for writing and 0xD1 for reading, which is what the code uses.
Hardware you need
- PIC16F877A microcontroller with a working oscillator
- DS1307 with a 32.768 kHz crystal, or a ready-made DS1307 module
- 3 V coin cell for the backup supply (VBAT)
- 16×2 character LCD
- Two pull-up resistors for SDA and SCL (many modules already include them)
Connections
- DS1307 SDA to RC4 (SDA) and DS1307 SCL to RC3 (SCL) on the PIC16F877A, each pulled up to 5 V.
- LCD in 4-bit mode, as defined in the code: RS to RD2, EN to RD3, and D4 to D7 on RD4 to RD7.
- Connect the DS1307 supply pin to 5 V and share a common ground between all parts.
How the program works
Initialization
The main() function starts the LCD, clears it, turns the cursor off and starts the hardware I2C module at 100 kHz with I2C1_Init(100000). It then calls Read_Time() once every second inside an endless loop.
Reading the time
To read the clock, the program sends a start condition and the write address 0xD0. It then writes 0 to set the register pointer to the seconds register, sends a repeated start, and addresses the chip again with 0xD1 for reading. It reads seven bytes in a row. The first six reads send an acknowledge (I2C1_Rd(1)), and the last read sends a not-acknowledge (I2C1_Rd(0)) to end the transfer before the stop condition.
Converting BCD for the LCD
Each value is split into two digits. The upper nibble is shifted right by four bits, the lower nibble is kept with a mask, and 0x30 is added to each to get an ASCII character. For example, 0x25 becomes the characters ‘2’ and ‘5’. The date is printed on the first LCD line and the time on the second.
Setting the time
The settime() function writes all seven registers in one transfer, starting at register 0. It first writes 0x80 to the seconds register, which halts the clock while the other values are loaded. A second transfer then writes 0 to register 0, which clears the halt bit and starts the clock at zero seconds. Run settime() once with your own values, then comment the call out again so the clock is not reset every time the PIC restarts.
Program
The original listing was wrapped in a block comment, so it would not compile as posted. It is cleaned up below. Function prototypes are added because MikroC needs functions declared before they are used. The seconds and hours values are masked so the halt bit and the 12/24-hour bit do not show up as wrong digits. The register comments in settime() are corrected to match the values, and the date now uses “/” as the separator.
unsigned char seconds, minute, hour, weekday, days, month, years;
// LCD module connections
sbit LCD_RS at RD2_bit;
sbit LCD_EN at RD3_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;
sbit LCD_RS_Direction at TRISD2_bit;
sbit LCD_EN_Direction at TRISD3_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;
// End LCD module connections
void Read_Time(void);
void settime(void);
void main()
{
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear LCD display
Lcd_Cmd(_LCD_CURSOR_OFF); // Turn cursor off
I2C1_Init(100000);
//settime(); // run once to set the clock, then comment out
while (1) {
Read_Time(); // read time from RTC (DS1307)
Delay_ms(1000);
}
}
void Read_Time(void)
{
I2C1_Start();
I2C1_Wr(0xD0);
I2C1_Wr(0);
I2C1_Repeated_Start();
I2C1_Wr(0xD1);
seconds = I2C1_Rd(1) & 0x7F; // mask the clock halt bit
minute = I2C1_Rd(1);
hour = I2C1_Rd(1) & 0x3F; // 24-hour mode
weekday = I2C1_Rd(1); // for example Monday = 2
days = I2C1_Rd(1);
month = I2C1_Rd(1);
years = I2C1_Rd(0); // last read: send NACK
I2C1_Stop();
// Line 1: date (DD/MM/YY)
LCD_Chr(1,1,((days & 0xf0) >> 4) + 0x30);
LCD_Chr_cp((days & 0x0f) + 0x30);
LCD_Chr_cp('/');
LCD_Chr_cp(((month & 0xf0) >> 4) + 0x30);
LCD_Chr_cp((month & 0x0f) + 0x30);
LCD_Chr_cp('/');
LCD_Chr_cp(((years & 0xf0) >> 4) + 0x30);
LCD_Chr_cp((years & 0x0f) + 0x30);
// Line 2: time (HH:MM:SS)
LCD_Chr(2,1,((hour & 0xf0) >> 4) + 0x30);
LCD_Chr_cp((hour & 0x0f) + 0x30);
LCD_Chr_cp(':');
LCD_Chr_cp(((minute & 0xf0) >> 4) + 0x30);
LCD_Chr_cp((minute & 0x0f) + 0x30);
LCD_Chr_cp(':');
LCD_Chr_cp(((seconds & 0xf0) >> 4) + 0x30);
LCD_Chr_cp((seconds & 0x0f) + 0x30);
}
void settime(void)
{
I2C1_Init(100000); // initialize full master mode
I2C1_Start(); // issue start signal
I2C1_Wr(0xD0); // address DS1307
I2C1_Wr(0); // start at register 0
I2C1_Wr(0x80); // REG0: halt clock, seconds = 00
I2C1_Wr(0x25); // REG1: minutes = 25
I2C1_Wr(0x16); // REG2: hours = 16 (24-hour mode)
I2C1_Wr(0x02); // REG3: weekday = 2 (Monday)
I2C1_Wr(0x24); // REG4: date = 24
I2C1_Wr(0x04); // REG5: month = 04 (April)
I2C1_Wr(0x15); // REG6: year = 15
I2C1_Stop(); // issue stop signal
I2C1_Start(); // issue start signal
I2C1_Wr(0xD0); // address DS1307
I2C1_Wr(0); // start at register 0
I2C1_Wr(0); // REG0 = 0: start counting, seconds = 00
I2C1_Stop(); // issue stop signal
Delay_ms(500);
}
Videos and reference image
SIMULATION VIDEO
Troubleshooting tips
- Blank or garbled LCD: check the RD2 to RD7 wiring against the
sbitdefinitions and the contrast pin of the LCD. - Time shows 00:00:00 and never changes: the clock is probably halted. Run
settime()once, or check the 32.768 kHz crystal. - Strange or random digits: check the pull-up resistors on SDA and SCL, and the common ground.
- Time resets after power off: check the backup battery on VBAT.
Related projects
If you use a different board, the same I2C register logic applies. See the PIC18F4550 with DS1307 and an LCD simulation, or try the DS1307 with Arduino clock. For other MikroC peripherals, we also cover ultrasonic sensor reading in MikroC and RFID in MikroC.
FAQ
Why is the time stored as BCD?
The DS1307 keeps each digit pair in a byte, with the tens in the upper four bits and the units in the lower four. This makes it easy to display, but you must convert the values if you want to do arithmetic on them.
Why do I need to call settime() only once?
The DS1307 keeps counting on its own, so it only needs the starting time once. If you leave settime() in the program, it will reset the clock on every restart.
What do the addresses 0xD0 and 0xD1 mean?
The DS1307 has the 7-bit I2C address 0x68. MikroC expects the address with the read/write bit included, so 0xD0 means write and 0xD1 means read.
Can I use the same code with other PIC chips?
Usually yes, as long as the chip has a hardware I2C module supported by MikroC. You may need to change the LCD pin definitions and the I2C function names to match your device.