﻿ Interfacing Seven segment display to 8051_Circuit Diagram World ﻿
Position: Index > Display Circuit >

# Interfacing Seven segment display to 8051

2016-12-05 13:23
Declaration:We aim to transmit more information by carrying articles . We will delete it soon, if we are involved in the problems of article content ,copyright or other problems.

### A Note about 7 segment LED display.

This article is about how to interface a seven segment LED display to an 8051 microcontroller. 7 segment LED display is  very popular and it can display digits from 0 to 9 and quite a few characters like A, b, C, ., H, E, e, F, n, o,t,u,y, etc. Knowledge about how to interface a seven segment display to a micro controller is very essential in designing embedded systems. A seven segment display consists of seven LEDs arranged in the form of a squarish’8′slightly inclined to the right and a single LED as the dot character. Different characters can be displayed by selectively glowing the required LED segments. Seven segment displays are of two types,common cathode and common anode.In common cathode type , the cathode of all LEDs are tied together to a single terminal which is usually labeled as ‘com‘   and the anode of all LEDs are left alone as individual pins labeled as a, b, c, d, e, f, g &  h (or dot) . In common anode type, the anode of all LEDs are tied together as a single terminal and cathodes are left alone as individual pins. The pin out scheme and picture of a typical 7 segment LED display is shown in the image below.

7 segment LED display

#### Digit drive pattern.

Digit drive pattern of a seven segment LED display is simply the different logic combinations of  its  terminals‘a’ to ‘h‘ in order to display different digits and characters. The common digit drive patterns (0 to 9) of a seven segment display are shown in the table below.

 Digit a b c d e f g 0 1 1 1 1 1 1 0 1 0 1 1 0 0 0 0 2 1 1 0 1 1 0 1 3 1 1 1 1 0 0 1 4 0 1 1 0 0 1 1 5 1 0 1 1 0 1 1 6 1 0 1 1 1 1 1 7 1 1 1 0 0 0 0 8 1 1 1 1 1 1 1 9 1 1 1 1 0 1 1

### Interfacing seven segment display to 8051.

Interfacing 7 segment display to 8051

The circuit diagram shown above is of an AT89S51 microcontroller based 0 to 9 counter which has a 7 segment LED display interfaced to it in order to display the count.  This simple circuit illustrates two things. How to setup simple 0 to 9 up counter using 8051 and more importantly how to interface a seven segment LED display to  8051 in order to display a particular result. The common cathode seven segment display D1 is connected to the Port 1 of the microcontroller (AT89S51) as shown in the circuit diagram. R3 to R10 are current limiting resistors. S3 is the reset switch and R2,C3 forms a debouncing circuitry. C1, C2 and X1 are related to the clock circuit. The software part of the project has to do the following tasks.

Form a 0 to 9 counter with a predetermined delay (around 1/2 second here).Convert the current count into digit drive pattern.Put the current digit drive pattern into a port for displaying.

All the above said tasks are accomplished by the program given below.

#### Program.

` ASM |  copy code |? 01ORG000H//initial starting address02START:MOVA,#00001001B//initial value of accumulator03MOVB,A04MOVR0,#0AH//Register R0 initialized as counter which counts from10to005LABEL:MOVA,B06INCA07MOVB,A08MOVC A,@APC//adds thebyteinA to the program counters address09MOVP1,A10ACALL DELAY//calls the delay of the timer11DECR0//Counter R0 decremented by112MOVA,R0//R0 moved to accumulator to checkifit is zeroinnext instruction.13JZSTART//Checks accumulatorforzeroandjumps to START.Done to checkifcounting has been finished.14SJMPLABEL15DB3FH//digit drive patternfor016DB06H//digit drive patternfor117DB5BH//digit drive patternfor218DB4FH//digit drive patternfor319DB66H//digit drive patternfor420DB6DH//digit drive patternfor521DB7DH//digit drive patternfor622DB07H//digit drive patternfor723DB7FH//digit drive patternfor824DB6FH//digit drive patternfor925DELAY:MOVR4,#05H//subroutinefordelay26WAIT1:MOVR3,#00H27WAIT2:MOVR2,#00H28WAIT3:DJNZ R2,WAIT329DJNZ R3,WAIT230DJNZ R4,WAIT131RET32END`

Instruction MOVC A,@A PC is the instruction that  produces the required digit drive pattern for the display. Execution of this instruction will add the value in the accumulator A with the content of the program counter(address of the next instruction) and will move the data present in the resultant address to A. After this the program resumes from the line after MOVC A,@A PC.

In the program,  initial value in A is 00001001B. Execution of  MOVC A,@A PC  will add oooo1001B to the content  in PC ( address of next instruction). The result  will be the address of command DB 3FH (line15) and the data present in this address ie 3FH (digit drive pattern for 0) gets moved into the accumulator. Moving this pattern in the accumulator to Port 1 will display 0 which is the first count.

At the next count, value in A will advance to 00001010 and after the execution of  MOVC A,@ PC  ,the value in A will be 06H which is the digit drive pattern for 1 and this will display 1 which is the next count  and this cycle gets repeated for subsequent counts.

The reason why accumulator is loaded with 00001001B (9 in decimal) initially  is that the instructions from line 9 to line 15 consumes 9 bytes in total.

The lines 15 to 24 in the program which starts with command DB can be called as aLook Up Table (LUT). Command DB is known as Define Byte – which defines a byte. This table defines the digit drive patterns for 7 segment display as bytes (in hex format). MOVC operator fetches the byte from this table based on the result of adding PC and contents in the accumulator.

Register B is used as a temporary storage of the initial value of the accumulator and the subsequent increments made to accumulator to fetch each digit drive pattern one by one from the look up table(LUT).

Note:-In line 6, Accumulator is incremented by 1 each time (each loop iteration) to select the next digit drive pattern. Since MOVC operator uses the value in A to fetch the digit drive pattern from LUT, value in ACC has to be incremented/manipulated accordingly. The digit drive patterns are arranged consecutively in LUT.

Register R0 is used as a counter  which counts from 10 down to 0. This ensures that digits from o to 9 are continuously displayed in the 7 segment LED. You may note lines 4, 11, 12, and 13 in the above program. Line 4 initializes R0 to 10 (OAh). When the program counter reaches line 11 for the first time, 7 segment LED has already displayed 0. So we can reduce one count and that is why we have written DEC Ro. We need to continuously check if R0 has reached full count (that is 0). In order to do that lines 12 and 13 are used. We move R0 to accumulator and then use the Jump if Zero (JZ) instruction to check if accumulator has reached zero. If Acc=0, then we makes the program to jump to START (initial state) and hence we restart the 7 segment LED to display from 0 to 9 again. If Acc not equal to zero, we continue the program to display the next digit (check line 14).

### Multiplexing 7 segment display to 8051.

Suppose you need a three digit display connected to the 8051. Each 7 segment display have 8 pins and so a total amount of 24 pins are to the connected to the microcontroller and there will be only 8 pins left with the microcontroller for other input output applications. Also the maximum number of displays that can be connected to the 8051 is limited to 4 because 8051 has only 4 ports. More over three 3 displays will be ON always and this consumes a considerable amount of power. All these problems associated with the straight forward method can be solved by  multiplexing .

In multiplexing all displays are connected in parallel to one port and only one display is allowed to turn ON at a time, for a short period. This cycle is repeated for at a fast rate and due to the persistence of vision of human eye, all digits seems to glow. The main advantages of this method are

Fewer number of port pins are required .Consumes less power.More number of display units can be interfaced (maximum 24).

The circuit diagram for multiplexing 2 seven segment displays to the 8051 is shown below.

Multiplexing 7 segement display to 8051

When assembled and powered on, the circuit will display the number ’16′ and let us see how it is done. Initially the first display is activated by making  P3.0 high and then digit drive pattern for “1″ is loaded to  the Port 1. This will make the first display to show “1″. In the mean time P3.1 will be low and so do the second display will be OFF. This condition is maintained for around 1ms and then P3.0 is made low. Now both displays will be OFF. Then the second display is activated by making P3.1 high and then the digit drive pattern for “6″ is loaded to the port 1. This will make the second display to show “6″. In the mean time P3.0 will be low and so the second display will be OFF.  This condition is maintained for another 1ms and then port 3.1 is made low. This cycle is repeated and due to the persistence of vision you will feel it as “16″.

Transistor Q1 drives the first display (D1) and transistor Q2 drives the second display (D2). R11 and R12 are the base current limiting resistors of Q1 and Q2. The purpose of other components are explained in the first circuit.

#### Program.

` ASM |  copy code |? 01ORG000H//initial starting address02MOVP1,#00000000B//clears port103MOVR6,#1H//stores"1"04MOVR7,#6H//stores"6"05MOVP3,#00000000B//clears port306MOVDPTR,#LABEL1//loads the adress of line29to DPTR07MAIN:MOVA,R6//"1"is moved to accumulator08SETBP3.0//activates 1st display09ACALL DISPLAY//calls the displaysubroutineforgetting the patternfor"1"10MOVP1,A//moves the patternfor"1"intoport111ACALL DELAY//calls the 1ms delay12CLR P3.0//deactivates the 1st display13MOVA,R7//"2"is moved to accumulator14SETBP3.1//activates 2nd display15ACALL DISPLAY//calls the displaysubroutineforgetting the patternfor"2"16MOVP1,A//moves the patternfor"2"intoport117ACALL DELAY//calls the 1ms delay18CLR P3.1//deactivates the 2nd display19SJMP MAIN//jumps back to mainandcycle is repeated20 21DELAY:MOVR3,#02H22DEL1:MOVR2,#0FAH23DEL2:DJNZ R2,DEL224DJNZ R3,DEL125RET26DISPLAY:MOVC A,@ADPTR//adds thebyteinA to the addressinDPTRandloads A withdatapresentinthe resultant address27RET28 29LABEL1:DB3FH30DB06H31DB5BH32DB4FH33DB66H34DB6DH35DB7DH36DB07H37DB7FH38DB6FH39 40END`