Home / Embedded Systems / External Event Counter – Seven Segment Displays

External Event Counter – Seven Segment Displays

External event counters are pretty useful things to have around. Once I had a feud with a shopkeeper for selling 4 motors in 3 different RPMs. Well, I don’t know if you have ever tried making a robot that had wheels rotating at different speeds, I did. It wan’t a very memorable experience.  The shopkeeper agreed that the RPM on one of the motors was of a different RPM and other ‘slightly-off’ but the others where just fine. It took me a lot of time to convince him that his definition of ‘slight’ was way-too-much-off.

Anyways, reason I brought that up was because, external event counters can be used for a variety of reasons. One of them is to make your own tachometer (and I did it) to prove your argument in a street fight. But of-course you need to be jobless to start with.

In my previous post we discussed the basic interface seven segment displays and how the concept of persistence of vision can be used to used to decrease the pin count of the embedded device.

Counters can either count up from zero or count down to zero. In the embedded space, down counter gives a marginal improvements in performance as most processors have a decrement and jump if not zero kind of instructions. But don’t worry that was just “fun facts” you don’t have to worry about it. Your optimizing C compiler will take care that for you.

In this post we will discuss a basics application of the concept that we discussed earlier and proceed along to make a physical counter that keeps a track of an external event (key press).

First, we will take up the task to write different data into all the 4 digits of the 7 segment display. Let’s say we want to print 1234 on the display. Here is a flow chart that will help you better understand the concept of persistence of vision (POV).

pov 7 segment display 0 to 99 counter

So if you write 1 to the data bus and enable the first segment, you will have to first step. Likewise, write 2 to data bus and enable the second segment. Similarly do the third and fourth step. Once this cycle is finished, repeat the step above steps at a frequency. For the sake of argument lets say 50 Hz.

If you did it correctly, you should see a static 1234 appear over the 4 digits of the 7 segment display. This is how you should program the above logic.

Here is a small video that I made to demonstrate the working of the above procedure.

Event Counter

Now that the persistence of vision section has been dealt with, we can get started with the counter. For this post we will use a micro switch for providing the input to the microcontroller. But in practical application any digital input (such as that from a IR interruption system) can be used to increment the counter.

Here is a flowchart to help you understand the working of the counter. Since the controller runs endlessly in a while(1) loop, there is not Stop block in the flowchart.

o to 99 counter flowchart

In the main function, the controller first checks if there is a counter overflow condition. If there is overflow, it will reset it back to 0. After this it tests the state of the input switch. If the switch is pressed, it increments the counter. After this, it send the value of the counter to the seg_wrt() function to write the data to the display.

Here a program to implement the above logic in embedded C.

Here is a demonstration of the above procedure,

I hope this post was helpful in understanding the interface of 7 Segment Displays and how they can be used to display numeric data. In the upcoming posts we will start working with these displays to make some real projects.

Edit History:

  • Article first published 15th Aug 2013
  • Code updated to xc8 compiler and republished  6th Dec 2015

About Siddharth

Siddharth is a Firmware Engineer, techie, and a movie-buff. His interests include, Programming, Embedded Systems, Linux, Robotics, CV, Carpentry and a lot more. At times, you could see some of his sunday projects converge on release quality. You get to know him on the following social channels.

Check Also

PIC with serial port

Arduino Like Serial / UART Library for PIC 18 Microcontrollers

In my last post describing the implementation of a circular buffer I mentioned that I …

  • Shri

    What software do you use to program the IC ?

    • Hi Shri,

      The IDE used for compiling the code is Mplab X with a c18 lite version Compiler. Mplab X has built-in support for pickit2 (programmer tool) to program/debug the IC.

  • How do I do in flowcode V4? I have this project that I have to build a flowchart and then download it to a chip PIC16F84A. How can I count up to 100 in the quad 7 segment ?

    • I haven’t used flowcode so far. But I assume it is much simpler than having to write the code. The logic remains the same, you will have to port this C code to a flowchart and change the condition if(ctr%99 == 0) to if(ctr%100 == 0) and add some extra code (in your case some extra blocks) to the seg_wrt() function to handle the hundreds segment (you wont need the 4th segment so tie its enable line to GND).

      PS. Why are you using flowcode? it can never be as good as C. Any development even remotely serious, is almost never happening in flowcode.

  • freddy

    how would you write a code in mplab C18 that will increment and decrement a seven segment display from 0 to F in hex

    • Dear Freddy, the concept is the same, you will have to create the bit map for digits A through F and add it to the look up table and use a push buttom to increment a counter from 0 to 15 and if it reached 15 loop back to 0. Use this counter as the look up table index and write the table’s content to the port connected to the 7 segment display.

      • freddy

        but how will it know to decrement. lets say you have two input switches one will increment the number and one will decrement. so if you increment lets say up to 8 how will it know to start from that position to decrement the number

        • Freddy, I gave you the logic for the increment right? then what is the problem in modifying it to decrement? All you have to do is read another input and use that input the decrement the counter variable. You will have to add some priority as to which will be taken first (increment or decrement)

  • sheikhmuaz

    how u want to program using embedded program using two 7 segment with not using same port… for example using port 1 and port 2

    • Please read through the post and you will understand how it is done.

  • Noble

    Hi can Mikro C be used to write the programme

    • Yes it can be used but I have never used it and most probably wont. I think there are way too many libraries in MicroC and it take more time to read and remember the functions with their parameters. I prefer to write code from scratch.

  • Barath

    Hi anyone can help me. By saying is there any books which can be explain from the basic programs and also what are the ways it can be done

    • you can try reading this book. c and unix tools for software design

  • Ankush Gupta

    How i can print 0 to 9 acc to user input in C program

  • zaki

    how to program seven segment to count from 0 to 20
    by using micro c program

  • Justin Kerrdoer

    My children required AZ CRF2M1 yesterday and found a great service with 6,000,000 forms . If others want AZ CRF2M1 too , here’s http://goo.gl/nT7nFn

Keep in touch with the current trends!
Did you like this article? Sign up and get our latest posts delivered to your inbox!
  We hate spam and never share your details.