Sunday, 30 June 2013

Digital Clock using 7 segment Display with PIC microcontroller (Code and Proteus Simulation)

This post will help us to understand how to multiplex 7 segment display with PIC microcontroller. One major importance of multiplexing 7 segment display is to get rid of external components such as latches or other ICs.

The trick behind multiplexing is that you enable one seven segment at a time. All of 7 segments are connected to a common parallel bus with a common node (ie Common Anode and Cathode) connected to transistor as shown in figure. So this means microcontroller will switch on specific transistor and put the segment data onto the port and only that 7 segment will light up. And this process go on for other 7 segments with a high speed fast enough that our eyes can’t see it. We only see all segments light up at all the time.

I am using MPLAB x IDE and C30 Compiler and Proteus Simulation on v8.0 (can be downloaded from downloads section at the bottom of this post). This code will work on PIC24, DsPIC33 and DsPIC30 (16bit microcontroller) by slightly changing code.

I supposed that you know following.

  • How to blink an LED (if not then I suggest you to read this (link) first before continuing this post)
  • How to Interface 7 segment display. (If not then I suggest you to read this (link) first before continuing this post).
  • How to use timer 1 interrupt. (If not then I suggest you to read this (link) first before continuing this post).
Proteus Simulation:

            In this figure, six 7 segments are connected to one portB 8bits LSB. And there transistors are connected to its PortB MSB. As I have 16bit port so I will mask or write to port at once.

Multiplexing 7 segment display


In the following figure. I first initialize the clock from 0 and made port as output. And initialize timer 1 interrupt after every 0.5Hz to blink LEDs of Clock and within this I used 1 second time after which seconds will incremented by ONE.

Main Function

Timer 1 interrupt function after which LEDs will toggle and seconds will incremented. After 59 seconds minutes will incremented and seconds will be reset to 0. And after 59 minutes hour will be incremented. And after 23 they will be reset to 0. This is simple implementation of digital clock.

Interrupt function timer 1

In the following figure UpdateDigitsOfSeconds () this will update the value of seconds to 7 segment display. And for updating minutes and hours we have UpdateDigitsOfMinutes() and UpdateDigitsOfHours() respectively.

Update time on 7 segment display.

Caution!!! In proteus simulation this clock may not work in realtime due to slow down process of Proteus but this circuit will work practically (100% guarantee).

Both Common cathode and Anode circuits and code is given you can download it from download sections.

Circuit for Common Anode.

Common Anode Circuit

Circuit for Common Cathode.

Common Cathode Circuit.

Reading Suggestions:


You can download Code and Proteus Simulation

                          For Common Anode Click here.
                          For Common Cathode Click here.


Getting started with Arduino coding. (A beginner’s quick guide)

Welcome to my first Arduino coding guide. This would take you to kickstart with Arduino boards. I would go through following things real...