
Chapter 7. Basic Experiments
7.6. Experiment with LED Display
In normal system, LED and LCD are common output interface. LED display can be found in various devices and machines. It is a cheap, high-luminance, versatile display.
 
This experiment discusses the basic operation on LED. It’ll show you how to display 0 to 9 in the 1st LED display.

LED display is composed by 8 light emitting diodes, among which 7 diodes form the numerical number 8, and 1 diode represent the dot (decimal point).


According to the driving mode, LED can be divided to 2 types, common-anode and common-cathode. A common-anode LED display is that the one with 8 LEDs’ anodes connecting together. A common-cathode LED display is that the one with 8 LEDs’ cathodes connecting together. See the below diagram.

To make a LED display show the numerical number, we need to light on and off certain segments (diodes). For example, to display number 5, segment A, C, D, F, G should be light on. Thus expect for P0.1, P0.4, P0.7, the rest P0.0, P0.2, P0.3, P0.5, P0.6 should all be set to 1, high level.
Segment |
DP |
G |
F |
E |
D |
C |
B |
A |
code |
Pin |
P0.7 |
P0.6 |
P0.5 |
P0.4 |
P0.3 |
P0.2 |
P0.1 |
P0.0 |
“5” |
0 |
1 |
1 |
0 |
1 |
1 |
0 |
1 |
0x6d |
Likewise, we can list the table of display code for 0 to 9. Below is the table for common-anode and common-cathode.
Numeric number |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
common-cathode |
0x3F |
0x06 |
0x5B |
0x4F |
0x66 |
0x6D |
0x7D |
0x07 |
0x7F |
0x6F |
common-anode |
0xC0 |
0xF9 |
0xA4 |
0xB0 |
0x99 |
0x92 |
0x82 |
0xF8 |
0x80 |
0x90 |

01 #include <reg51.h>
02
03 unsigned char code Tab[]={ 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,
04 0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E};
05 sbit S1 = P2^0;
06
07 void Delay()
08 {
09 unsigned char i,j;
10 for(i=0;i<255;i++)
11 for(j=0;j<255;j++);
12 }
13
14 void main()
15 {
16 unsigned char i = 0;
17 S1 = 0;
18 while(1)
19 {
20 P0= Tab[i];
21 Delay();
22 i++;
23 if(i>9) i =0;
24 }
25 }
Program Notes
Line 1: include the 8051 register definition header file
Line 3-4: define common-anode display code table
Line 5: sbit define common control S1
Line 7-12: delay function
Line 14: main function
Line 16: define local variable i for counting
Line 17: give power to LED S1
Line 18: loop
Line 20: output display code to P0
Line 21: invoke delay function
Line 22: increment i
Line 23: limit i to be in the range from 0 to 9
The above experiment shows you how to display a single LED, but how do we make multiple LEDs display? Time sharing is a technique people often used to display more than 1 LED display. See the below codes.

01 #include <reg51.h>
02
03 unsigned char code Tab[]={ 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,
04 0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E};
05 sbit S1 = P2^0;
06 sbit S2 = P2^1;
07 sbit S3 = P2^2;
08 sbit S4 = P2^3;
09
10 void Delay()
11 {
12 unsigned char i;
13 for(i=0;i<255;i++);
14 }
15
16 void main()
17 {
18 while(1)
19 {
20 P0= Tab[1];
21 S1 = 0; //turn on S1
22 Delay();
23 S1 = 1; //turn off S1
24
25 P0= Tab[2];
26 S2 = 0; //turn on S2
27 Delay();
28 S2 = 1; //turn off S2
29
30 P0= Tab[3];
31 S3 = 0; //turn on S3
32 Delay();
33 S3 = 1; //turn off S3
34
35 P0= Tab[4];
36 S4 = 0; //turn on S4
37 Delay();
38 S4 = 1; //turn off S4
39 }
40 }
From line 20 to 38, we can see the whole time sharing procedure. Firstly, display code is output on data bus P0, then turn on a LED, delay a certain time, and then turn off it. Repeat this until 4 LED is all turn on for once. Then restart the procedure again. To make the most of human-being’s retentivity of vision, care should be taken with these points. To avoid the LED flashing, delay time should not be too long. To have equal luminance, each delay time must be the same. To increase the luminance, delay time can be shortened. If there are double images on LED, LED should be turn on and then turn off. |