0% found this document useful (0 votes)
16 views8 pages

4.1 8051 - 7 Segment LED Display

The document discusses interfacing a 7-segment LED display with a microcontroller. It describes the basics of 7-segment LED displays and how they can be connected in common cathode or common anode configurations. Lookup tables are used to store the codes needed to display numbers on the LED display based on the configuration. Codes are retrieved from the table by using the address and loading the value into a register to output to the display.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
16 views8 pages

4.1 8051 - 7 Segment LED Display

The document discusses interfacing a 7-segment LED display with a microcontroller. It describes the basics of 7-segment LED displays and how they can be connected in common cathode or common anode configurations. Lookup tables are used to store the codes needed to display numbers on the LED display based on the configuration. Codes are retrieved from the table by using the address and loading the value into a register to output to the display.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

Microcontrollers & Applications (MU-Sem.

6-E&TC) 4-2 8051 Interfacing & Applications

4.1 8051 | 7 Segment LED Display

4.1.1 Basics of LED

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.

Fig. 4.1.1 : Basics of 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.

4.1.2 Blinking LED program

Write a program to blink an LED connected to P1.0 Algorithm

Algorithm

Make P1.0 1
Call a Delay subroutine
Make P1.0 0
Call a Delay subroutine

Tech-Neo Publications...........Where Authors inspire innovation .....A SACHIN SHAH Venture


Microcontrollers & Applications (MU-Sem. 6-E&TC) 4-3 8051 Interfacing & Applications

Program

Program : Loop this infinitely


Again : SETB P1.0 ; P1.0 1
ACALL Delay ; Call a delay routine of typically half a second
CLR P1.0 ; P1.0 0
ACALL Delay ; Call a delay routine of typically half a second
SJMP Again ; Jump to label “Here” and end the program
Note : We have made ample Delay Routines in the previous chapter. You could use any of them for the
delay.
There are 8 LEDs connected to the port pins of P1. Write a program to blink them alternately.

Algorithm

Send 55H to P1. (55H = 0 1 0 1 0 1 0 1)


This makes 4 LEDs on and 4 OFF alternately
Call a Delay routine (roughly 500 milliseconds)
Send AAH to P1. (AAH = 1 0 1 0 1 0 1 0)
This makes the other 4 LEDs on and 4 OFF alternately
Call a Delay routine (roughly 500 milliseconds)
Loop this infinitely

Program

Again : MOV P1, 55H ; P1.0 0101 0101


ACALL Delay ; Call a delay routine of typically half a second
MOV P1, 0AAH ; P1.0 1111 1111
ACALL Delay ; Call a delay routine of typically half a second
SJMP Again ; Jump to label “Here” and end the program

4.1.3 7 Segment LED

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.

Tech-Neo Publications...........Where Authors inspire innovation .....A SACHIN SHAH Venture


Microcontrollers & Applications (MU-Sem. 6-E&TC) 4-4 8051 Interfacing & Applications

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:

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 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

Tech-Neo Publications...........Where Authors inspire innovation .....A SACHIN SHAH Venture


Microcontrollers & Applications (MU-Sem. 6-E&TC) 4-5 8051 Interfacing & Applications

7 segment code 7 segment code


Address
Common Anode Common Cathode
C0 H 3F H
DPTR = 0400 0400 H
(Code of 0) (Code of 0)
F9 H 06 H
& A = 05 0401 H
(Code of 1) (Code of 1)
A4 H 5B H
0402 H
(Code of 2) (Code of 2)
B0 H 4F H
0403 H
(Code of 3) (Code of 3)
99 H 66 H
0404 H
(Code of 4) (Code of 4)
92 H 6D H
@A+DPTR 0405 H A gets code of 5
(Code of 5) (Code of 5)
82 H 7D H
0406 H
(Code of 6) (Code of 6)
F8 H 07 H
0407 H
(Code of 7) (Code of 7)
80 H 7F H
0408 H
(Code of 8) (Code of 8)
90 H 67 H
0409 H
(Code of 9) (Code of 9)

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

Tech-Neo Publications...........Where Authors inspire innovation .....A SACHIN SHAH Venture


Microcontrollers & Applications (MU-Sem. 6-E&TC) 4-6 8051 Interfacing & Applications

4.1.4 Program to display a countdown from 9…0.

Interface a 7 segment LED to port 1 and write a program to display a countdown from 9 to 0.

There must be a delay of 1 second after each count displayed.

Algorithm

Create look up table at say 0400H


Initialise DPTR to look up table address
Initialise R7 as loop count of 10
Initialise R6 = 9 as the starting number to be displayed.
Move R6 into A
Translate A into its 7 segment code using the look up table
Put out A on port 1
Call a delay of 1 second
Decrement R6 (the number to be displayed)
Decrement R7, the loop count and loop back till R7 becomes 0.

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

Tech-Neo Publications...........Where Authors inspire innovation .....A SACHIN SHAH Venture


Microcontrollers & Applications (MU-Sem. 6-E&TC) 4-7 8051 Interfacing & Applications

4.1.5 Program and Interface 4 digit Multiplexed 7 segment LED

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.

Tech-Neo Publications...........Where Authors inspire innovation .....A SACHIN SHAH Venture


Microcontrollers & Applications (MU-Sem. 6-E&TC) 4-8 8051 Interfacing & Applications

Algorithm

Create Look up table of 7 segment codes at say 0400H


Make DPTR point to the table
Send code of 1 on P1, Activate P2.3, Call delay of 10 milliseconds
Send code of 6 on P1, Activate P2.2, Call delay of 10 milliseconds
Send code of 0 on P1, Activate P2.1, Call delay of 10 milliseconds
Send code of 5 on P1, Activate P2.0, Call delay of 10 milliseconds
Repeat sending the above 4 codes

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)

Tech-Neo Publications...........Where Authors inspire innovation .....A SACHIN SHAH Venture


Microcontrollers & Applications (MU-Sem. 6-E&TC) 4-9 8051 Interfacing & Applications

Fourth: MOV A, #05H ;A “5”


MOVC A, @A+DPTR ;A 7 segment code of “5”
MOV P1, A ; Send code of “5” on P1
MOV P2, #0EH ; P2 = 0000 1110… this will select 4th digit on P2.0
ACALL Delay ; Call a delay of 10 millisecond (Refer previous chapter
; for delay routine)
SJMP First ; Repeat sending the above four codes
Select Digit P2.7 P2.6 P2.5 P2.4 P2.3 P2.2 P2.1 P2.0 Value on Port 2

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

Tech-Neo Publications...........Where Authors inspire innovation .....A SACHIN SHAH Venture

You might also like