Digital 6-digit 7 segment clock with microcontroller

By IKTech May 29, 2017

What to know about this project is:

1. 7 segment multiplexing

2. C language Functions and header file.

3. Ds1307 idea about RTC IC.

4. Ideas about I2C protocol.

So let’s start,


I know you do not read datasheet. So I will show the important topics of the datasheet here.

RTC = Real Time Clock.

This is the normal circuit of ds1307. Rpu’s value is 1K – 4.7K, VCC = 5v, Vbat = 3.7v, crystal = 32KHz

From the upper part, we can understand that the number of ds1307 hours register is 12-hour / 24-hour with the 6 bit. And 12-hour select 5 bit bit as AM / PM.

This table is very important. From the table, we can see data from any address. For example, we need to know hourly then we must take the data from address 0x02.

This rule requires to read data from our IC. S> 1101000 0> Address> Sr> 1101000 1> Read Data> P.


S = Start = soft_I2C_start ();

1101000 0 = 0xD0 = Device Address + Write mode

Address = Address from the address.

Sr = I2C_repeated_start (); [But we have used soft I2C here so that there is no function named repe_start (). But instead of being told in the library, soft_I2C_start (); Can be used.]

1101000 = 1 = 0xD1 = Device Address + read mode


P = Stop = soft_I2C_stop ();

This rule requires us to write data to IC. Similar analysis can be done. So I did not do that
Program Analysis

To make the program short and understandable, I have worked part of the program by parting the header file. The program is divided into 3 files

1. SimpleRTCclcok.c = Main file / code.

2. Ds1307.h = This code has been separated from ds1307 only.

3. 7segment.h = There are 7 segments in this code.

I have described the ds1307.h file above. You can also clear it again

Sbit Soft_I2C_Scl at RA2_bit;
Sbit Soft_I2C_Sda at RA3_bit;
Sbit Soft_I2C_Scl_Direction at TRISA2_bit;
Sbit Soft_I2C_Sda_Direction at TRISA3_bit;

As we have used soft I2C, I2C pins are mentioned here.

Unsigned short read_ds1307 (unsigned short address) {
Return r_data;

Read_ds1307 A function which will return data of unsigned short type.

Return r_data; Has been specified with the value tie return of r_data.

This function has an address named parameter. Which basically indicates that we want data from ds1307 IC address.

If (address! = 2) r_data = bcd2dec (r_data);

This line is said to be, when the value of the address is 0x02 [hour], then only r_data will convert from BCD to DEC.

[Note: ds1307 basically understands the BCD number. But 12-hour mode a hour is not converted to BCD. In order to read / write the HEX number.]

I’m not a little lazy to write_ds1307 and analyzed. ?


Void show_display (char y, unsigned short x) {// y for digit select, x for data
Unsigned short temp;
If (y == ‘H’) {

.. .. .. .. .. .. ..


This function has done data show in 7 segment.

Show_display (‘h’, value); // ‘H’ for hours, which will show in the value.

I do not see anything else in this file. Even if there is anybody’s problem I am there. ?


Hh = tmp & 0b00011111;

As already mentioned, hour is only HEX number. We have another 12-hour [6-bit ‘1’] mode

Hh = 0x51;

// PM = 0x61 – 0x72 [1 – 12]
// AM = 0x41- 0x52 [1 – 12]

0x61 = 0110 0001 // 5bit ‘1’ for PM.

Now, 0110 0001 & 0001 1111 = 0000 0001 = 0x01 = 1

So the display for 0x61 will show ’01’ hour. Clear ????

If (hh> 9) hh- = 6;

Since hh value is HEX, [dec 10 = hex F (dec 16)] So if the value of hh is greater than 9 -6 is done. Eg hh = 0x0F – 6 = 0x10;

For delay (delay = 0; delay <20; delay ++), using ds1307 has been delayed between taking data. [Important] Tmp = read_ds1307 (2); Keeping the value in tmp is due to am / pm [5bit] from this value. Time Match: There are 4 PUSH buttons for matching time. The work of the buttons is given below 1. SETUP: Push once to enter time setup mode. 2. SELECT: Push to next. Hour> Minute> Second> DONE

3. UP: Push to Increment current selected value.

4. DOWN: Push to Decrement current selected value.

Leave a Reply

Your email address will not be published.