4.1 8051 - 7 Segment LED Display
4.1 8051 - 7 Segment LED Display
An LED (Light Emitting Diode) is one of the most basic output devices.
You see them everywhere around you, starting from the TV ON/OFF light, Fancy decoration lights,
Traffic indicators etc. They are simply turned On or OFF by a basic 1/0 logic.
The LED has an Anode which needs a logic 1 and a cathode which needs a logic 0.
When you hold the LED in your hand, you will notice the Anode to be slightly longer than the Cathode.
There are two ways to connect an LED.
Sourcing Method
We connect the Anode to a port pin and the Cathode to ground. Here we need to make the port pin logic
1 to turn ON the LED. The current is “Sourced” by µC and will “sink” into ground.
Hence it is called Sourcing method.
Sinking Method
We connect the Cathode to a port pin and the Anode to Vcc. Here we need to make the port pin logic 0
to turn ON the LED. The current is “Sourced” by Vcc and the µC will “Sink” it.
Hence it is called Sinking method.
Algorithm
Make P1.0 1
Call a Delay subroutine
Make P1.0 0
Call a Delay subroutine
Program
Algorithm
Program
A 7 segment LED is used at various places like floor indicators in a lift, temperature indicators, time
indicators, cab meters and so on. It is formed by 7 LEDs put together to display digits. In fact in places
where a point is needed, there is an 8th LED which acts as a decimal point.
A 7 segment LED can be connected in two ways: Common Cathode or Common Anode.
In Common Cathode, the cathode of all LEDs is commonly connected to ground and the Anode is
connected to the port pins. Here, sending a 1 on any LED will turn it ON.
In Common Anode, the anode of all LEDs is commonly connected to Vcc and the Cathode is
connected to the port pins. Here, sending a 0 on any LED will turn it ON.
Look Up Table
To display a number like 1, we don’t really send “1” to the LED. We send a code called a 7 segment
code. It will turn ON some and turn OFF some LEDs such that the required digit “1” comes up on the
display. The digits are named a, b, c, d, e, f, g and dp for the decimal point. To display 1, we need to
turn ON b and c and turn OFF the rest of them.
For common Anode, driven by Port 1 pins, the codes will be as follows:
0 1 1 0 0 0 0 0 0 C0 H
1 1 1 1 1 1 0 0 1 F9 H
2 1 0 1 0 0 1 0 0 A4 H
3 1 0 1 1 0 0 0 0 B0 H
4 1 0 0 1 1 0 0 1 99 H
5 1 0 0 1 0 0 1 0 92 H
6 1 0 0 0 0 0 1 0 82 H
7 1 1 1 1 1 0 0 0 F8 H
8 1 0 0 0 0 0 0 0 80 H
9 1 0 0 1 0 0 0 0 90 H
Do understand that some programmer may connect P1.0… P1.7 in the order dp – to – a instead of the
order a – to – dp. Then the nibbles will be interchanged.
Similarly, if common cathode method is used, then all values will be inverted as “1” will turn the LED
ON and “0” would then turn it OFF. Either way, the programmer creates the look up table as per her/his
circuit connections and stores them in the memory.
Let us say the table is stored at memory location 0400H. This is done using the following instructions:
ORG 0400H
Table DB C0H, F9H, A4H, B0H, 99H, 92H, 82H, F8H, 80H, 90H
This creates a look up table of 7 segment codes from ROM location 0400H
This creates a look up table of 7 segment codes from ROM location 0400H
Now, to get the code a number lets say 5, these are the steps to be performed:
MOV DPTR, #0400H ; Initialise DPTR as a pointer to starting of the look up table = 0400H
MOV A, #05H ; A has the number to be translated into its 7 segment code = 05H
MOVC A, @A+DPTR ; A gets the 7 segment code of 5 from the location 0400 + 5 = 0405H
NOTES
Interface a 7 segment LED to port 1 and write a program to display a countdown from 9 to 0.
Algorithm
Program
ORG 0400H
Table DB C0H, F9H, A4H, B0H, 99H, 92H, 82H, F8H, 80H, 90H
MOV DPTR, #0400H ; Initialise DPTR as a pointer to starting of the look up table = 0400H
MOV R7, #0AH ; Initialise R7 = 0AH, Loop count of 10
MOV R6, #09H ; Initialise R6 = 09H, Starting number to be displayed
Back: MOV A, R6 ;A Number to be displayed
MOVC A, @A+DPTR ; A gets 7 segment code of the number to be displayed
MOV P1, A ; P1 A, the number gets displayed on the 7 segment LED
ACALL Delay ; Call a delay of 1 second (Refer previous chapter
; for the delay routine)
DEC R6 ; Decrement R6, the number to be displayed
DJNZ R7, Back ; Decrement loop count and jump to back 10 times
Here: SJMP Here ; Program ends
Interface a 4 digit 7 segment multiplexed LED to 8051 and hence write a program to display
1605.
Interface
Fig. 4.1.1
A 4 digit 7 segment display is formed by multiplexing 4 digits on a single port say P1. 7 segment codes
for all 4 digits will be sent out on P1, one by one.
The lines of P2 will be used to select the digit to be activated.
Suppose we wish to display 1605 on the 4 digits. We will send the 7 segment codes in this manner…
Send code of “1” on P1 and Activate the 1st digit using P2.3
Send code of “6” on P1 and Activate the 2nd digit using P2.2 Send code of “0” on P1 and Activate the
3rd digit using P2.1 Send code of “5” on P1 and Activate the 4th digit using P2.0
Now, repeat the above cycle continuously, so fast that we beat “Persistence of Vision”.
The human eye needs roughly 80-100 milliseconds to register an event.
If we can loop back within that much time, the human eye will get the “Illusion” that all 4 digits are on
simultaneously though that’s not the real case. This is the same principle behind motion picture or
movies. The µC however is so fast that it can finish the loop in less than one millisecond.
We therefore take the liberty of adding a small delay (5-10 milliseconds) as each digit is displayed, to
allow the LEDs enough time to fire up. It still gives the µC enough time to finish the loop in the
stipulated time window.
Algorithm
Program
ORG 0400H
Table DB C0H, F9H, A4H, B0H, 99H, 92H, 82H, F8H, 80H, 90H
MOV DPTR, #0400H ; Initialise DPTR as a pointer to starting of the look up table = 0400H
First: MOV A, #01H ;A “1”
MOVC A, @A+DPTR ;A 7 segment code of “1”
MOV P1, A ; Send code of “1” on P1
MOV P2, #07H ; P2 = 0000 0111… this will select 1st digit on P2.3
ACALL Delay ; Call a delay of 10 millisecond
; (Refer previous chapter for delay routine)
Second: MOV A, #06H ;A “6”
MOVC A, @A+DPTR ;A 7 segment code of “6”
MOV P1, A ; Send code of “6” on P1
MOV P2, #0BH ; P2 = 0000 1011… this will select 2nd digit on P2.2
ACALL Delay ; Call a delay of 10 millisecond (Refer previous chapter
; for delay routine)
Third: MOV A, #00H ;A “0”
MOVC A, @A+DPTR ;A 7 segment code of “0”
MOV P1, A ; Send code of “0” on P1
MOV P2, #0DH ; P2 = 0000 1101… this will select 3rd digit on P2.1
ACALL Delay ; Call a delay of 10 millisecond
; (Refer previous chapter for delay routine)
1st Digit 0 0 0 0 0 1 1 1 07 H
2nd Digit 0 0 0 0 1 0 1 1 0B H
3rd Digit 0 0 0 0 1 1 0 1 0D H
4th Digit 0 0 0 0 1 1 1 0 0E H
Please Note :
Next time you see a multi-digit 7 segment LED E.g.:: railway station indicator, taxi meter etc. remove
your phone camera. The camera captures images at a much higher rate than human eye. Point the camera towards
the 7 segment LED. You will see the digits coming in a wavy sequence, especially when you shoot the video in slo-
mo.
Common Cathode
For Common Cathode, driven by Port 1 pins, the codes will be as follows :
Value P1.7 P1.6 P1.5 P1.4 P1.3 P1.2 P1.1 P1.0 7 segment code
dp g f e d c b a
0 1 1 0 0 0 0 0 0 3F H
1 1 1 1 1 1 0 0 1 06 H
2 1 0 1 0 0 1 0 0 5B H
3 1 0 1 1 0 0 0 0 4F H
4 1 0 0 1 1 0 0 1 66 H
5 1 0 0 1 0 0 1 0 6D H
6 1 0 0 0 0 0 1 0 7D H
7 1 1 1 1 1 0 0 0 07 H
8 1 0 0 0 0 0 0 0 7F H
9 1 0 0 1 0 0 0 0 67 H