Seven Segment Display

 

seven segment display is the most basic electronic display device that can display digits from 0-9 and also some alphabets like (A,b,D,d, E, F). They find wide application in devices that display numeric information like digital clocks, radio, microwave ovens, electronic meters etc. The most common configuration has an array of eight LEDs arranged in a special pattern to display these digits. They are laid out as a squared-off figure ‘8’. Every LED is assigned a name from 'a' to 'h' and is identified by its name. Seven LEDs 'a' to 'g' are used to display the numericals while eighth LED 'h' is used to display the dot/decimal.

A seven segment is generally available in ten pin package. While eight pins correspond to the eight LEDs, the remaining two pins (at middle) are common and internally shorted. These segments come in two configurations, namely, Common cathode (CC) and Common anode (CA). In CC configuration, the negative terminals of all LEDs are connected to the common pins. The common is connected to ground and a particular LED glows when its corresponding pin is given high. In CA arrangement, the common pin is given a high logic and the LED pins are given low to display a number. 

In a simple LED package, typically all of the cathodes (negative terminals) or all of the anodes (positive terminals) of the segment LEDs are connected and brought out to a common pin; this is referred to as a "common cathode" or "common anode" device. Hence a 7 segment plus decimal point package will only require nine pins.

 

Now, I will tell you about 7 segment displays with common anode, which i have run with 8051 microcontroller. To run that obviously, you have to connect the common anode to power supply of your choice (either 5v or 12v). I connected 12v via ULN IC, but if its getting too hot while glowing switch to 5v. It will be a bit dim, but good to go.

To operate a common cathode 7 segment, you just need to connect common cathode to ground. and then you can pnly use 5v comming from MCU to glow the LEDs in 7-segment. Although, its possible to use again ICs like ULN to drive the segment, but at all, its better to use MCUs as sink, but not as source.

Anyways, we can continue with common cathode seven segment. I have also shown the image of how to connect 7-segment iwith MCU of 8051 architecture or any other. Just, you have to connect the digital pins (output) to seven segment. If there is CC type segment, connect common pin to ground and drive digital output pin of MCU high to make a particular LED glow,  and if there is CA type segment, then connect the common pin to voltage of your choice and drive digital pins of MCU low each time you want to glow a particular LED of segment.

Its preety simple. And yes! if by chance, you dont know if your segment is CC or CA, then use your multimeter to detect that. Its also very simple. Connect the multimeter common pin to common pin of segment and check if resistance is low accross the LEDs control pins. Reverse it and check it again. If resistance is lesser, when you connect the common pin of multimeter to common pin of segment, then it means that you have Common cathode, because LED is like PN junction diode, if forward biased, there is less resistance as compared to backward bias.

C Code (Example with 8051 MCU)


Now, circuit is done. Moving to programming again, i will redirect you all to my git repository and run that program. For explaination of program, firstly, lets study, what our basic logic is. For that, look at the table. Usually, there are decoders available for 7-segment, which takes value in 4 bit binary and output the values for which display shows numbers, but when programming an MCU for running 7-segment, Its not a good practice to use much external devices. We must then, reduce hardwares and try to implement them inside our code. So, now basically, i can say that, we have to program a decoder function which will be running on 8051 MCU (in this case) and will do the same work as external decoder. Here, we will also have benefits like we can make binary input into decimal, means we can now directly call a function with argument which will be an integer and then we can print it by decoding program.

So, lets write this decoding program now. To start with, i hope that you have understand that if we want to display 0, then i have to switch particulat LEDs like (a, b, c, d, e, f will be on and g will be off). h is for decimal point, so it will have no effect. In this code, you can see that firstly, i got the seperate identifier of all the Dpins of my MCU which are connected to segment with help of sbit keyword.

Then i made some helping function like init(x) to clear garbage data and be assure that there was not previous state of pins which could give the segment some wierd symbols. Next we have the main working function named display and taking one integral argument. Its mapping all the input from 0-9 to the value of Dpins as shown in table above. For example: for 0, its init(1) that means all pin high and making the middle pin g low resulting into 0 formation on segment. Look! i have used this helping function to avoid my LOCs. Otherwise the dumbest way would have been making every pin high low in every step or caluculating hex value and allocating whole one port to 7-segemnt which would be a restriction in bigger project.

 

Now, for the bigger number, more than 9, we will more than 1 seven segment display and will connect the common cathode to the controller's seperate pins to switching them ON/OFF. Now MCu has to not only calculate the value, but also switch the correct place segment for which the data is calculated. Also, MCU has to change the data very frequently and very fast that, it seems to our eyes, different data on 2 segements whoes data pins are shorted. So in this way, you can drive 2 or more segemnts just by using 1 pin per addition of segemnt. 

For this, you should write another function, which will be using the display function you made and will oscillate the integeral bigger values different digits among the enabled segemnts very fast. To avoid confusion, i am not including that code here, but once, you got this concept, i am sure that you can easily code that program with the knowledge which i just told you. But if you sould not run different data on different segemnts and wants code from me, then just comtact me from contact page. I will give you link or send you the code.

I hope that this tutorial helped you regarding 7-segment operation. Now, you know the concept, so now, you can program 7-segment for any platform, be it VHDL, C, PIC and so on. If you want to run the program given by me or your own programs, then go to the articles of 8051 intro and 8051 microcontroller programming setup in learning section of this blog.