How to drive 7-Segment Displays with a 8-bit PIC

How to drive 7-Segment Displays with a 8-bit PIC

7-segment displays are practical and intuitive to read numbers and symbols out of PIC microcontroller, which are intuitively recognizable for humans. Because the displays are made out of LEDs, it's easy to control and show the right number. This tutorial is about a sequence where all the numbers are displayed, one second at a time. Timer 1 is used for counting the seconds. At the end, you can download the code for free. Continue reading to learn how to drive 7-segment displays.

Before reading this, you will need to know how to setup MPLAB IDE and how to connect the PICkit to the PIC microcontroller. If not, check them out first. Check out also another projects for beginners in 8-bit PIC microcontrollers.

Requirements to drive 7-Segment Displays

Components and Devices

In this tutorial, all the components are Through Hole.

  • PIC16F684: 1 unit.
  • Resistors:
    • 330Ω, 1/2 or 1/4W: 7 units.
  • Numeral 7-segment displays: 1 unit. In this tutorial, Common Anode are used.
  • 32768KHz crystal oscillator: 1 unit.
  • Capacitors:
    • 22pF: 2 units.
  • PIC16F684
  • 7-segment display
  • 32768Hz quartz

PIC microcontroller

For this tutorial, I will use the 8-bit microcontroller PIC16F684. This one is the very first one that I learned to program. Very practical too. Be sure to select Through Hole or ‘TH' version. Surface Mounted won't fit in the breadboard.

PIC6F684
PIC6F684

Download the datasheet here. I recommend that you print in reduced A4 all the datasheet because it will serve you as quick reference.

Tools and Machinery

  • Breadboard: 1 unit.
  • PICkit (3 or 4): 1 unit.
  • AC/DC Power Adapter to 5V DC, with at least 500mA: 1 unit.
  • Jumper or UTP Wires: various.
  • PICKIT 4

7-segment displays. How do they work.

As it was pointed out before, 7-segment displays are made out of LEDs, 7 of them for the number and 1 decimal point. To light up a segment, you have to connect a resistor in series with the LED, as usual with any LED. That's it; that is how you drive a 7-segment display.

The connection

There are two types, Common Anode and Common Cathode. In the first case, the positive power source terminal + is connected directly to a common node and every LED is connected in series to the negative power source terminal . In the second case, it is exactly the opposite.

For the Common Anode display, there are two pins for the power source and one pin for every segment.

7-segment display pinout
7-segment display pinout

Schematics for 7-Segment Displays

The PIC16F684 has to provide the sink for the current from the 7-segment display, which means that a pin with 0V will turn on a LED. Every segment has to go in series with a 330Ω resistor. It doesn't matter to which pin because that is handled in the code. The 32768Hz crystal must be placed in the RA4 and RA5 pins along with the 22pF capacitors as shown below.

Don't forget to connect the PICkit for programming. After everything is connected, let's see how to drive 7-segment displays with a 8-bit PIC microcontroller.

Code for 7-Segment Displays

Initial Configuration

The PIC16F684 uses the internal 4MHz oscillator, therefore the pins RA4 and RA5 are left to be used as general I/O. Also, MCLR and WDT are turned off.

C

The pins used for the 7-segment display are set as outputs. The rest of the registers are turned off, so they don't interfere with the operation.

C

The setup for Timer 1 for 1 second interruptions. If you haven't checked the tutorial yet, please click here.

C

Also, Interrupts must be enabled.

C

Aliases of the pins

The aliases are defined for easy management to avoid clutter and disorder.

C

Routine of Timer 1

The routine to attend the interruption of Timer 1. Upon each interruption, the sequence advances like turns. Each iteration, it displays a different number in order.

C

At the end, Timer 1‘s register has to be modified in order to interrupt again every second. If you haven't checked the tutorial yet, please click here.

C

Extra Coding

Don’t forget to add the While(1) in the Main function to make it run forever!

C

Download the code of the 7-Segment Displays

If you would like to see and read the whole code through, enter your name and e-mail in the form below to download the project. I promise that I won't send you spam; just relevant content to the blog. If you don't see any form below, please click here.

Download the Code

and subscribe to get updates from techZorro direct in your inbox.

    We won't send you spam. Unsubscribe at any time.

    Testing the 7-Segment Displays

    If you don't know how to compile and code a project into a PIC microcontroller, follow this link for the tutorial.

    Let's program the PIC microcontroller and let's see how to drive 7-segment displays. If everything executed properly, you should see a message in the bottom of the Output window confirming that the programming process has been complete.

    Photo

    A picture of the circuit was taken as reference.

    7-segment display
    7-segment display on the breadboard

    Video

    See it with your own eyes.

    Conclusion

    The tutorial has shown how to drive 7-segment displays with a PIC microcontroller and it works marvelously. The Timer 1 executes every second a change of number in the display. The code and schematic are shown and they are downloadable. Unfortunately, it requires 7 pins for the operation but there is a solution or workaround for this.

    The next tutorial will implement an integrated circuit to drive the 7-segment display for us. The main advantage is the reduction of pins required of use.

    Resources

    You have reached this far!

    Thank you for reading the blog post. Your comments and suggestions are welcomed. At the bottom of this page, leave a message or just say hi! The whole team of techZorro will appreciate it. Don't forget to share it on social media as well.

    techZorro’s Index of Content

    Click on the following link to browse likewise content in the blog in techZorro. This index will help you see what you are looking for in a bird’s eye view.

    techZorro's Newsletter!

    If you enjoyed this blog post, please subscribe to techZorro’s newsletter so you don’t miss any future blog posts!

    techZorro's Index of Content

    Keep Reading!

    3 Comments

    Leave a Reply

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

    Don't know how to start?

    Find out how you can learn step-by-step.

      Juditova

      Variable Frequency Drives: how this controller can transform induction motors forever

      Juditova         Juditova        
      Variable Frequency Drives: how this controller can transform induction motors forever           Variable Frequency Drives: how this controller can transform induction motors forever          
      Back 15 seconds
      Forward 60 seconds
      more
        Back 15 seconds
        Forward 60 seconds
        Currently Playing