Module 5
I/O interfacing with Microcontroller 8051
LED INTERFACING
• Interfacing comprises of hardware (Interface device) and Software
(source code to communicate, also called as the Driver).
• Simply, to use an LED as the output device, LED should be connected to
Microcontroller port and the MC has to be programmed inside make
LED ON or OFF or blink.
• Light Emitting Diodes or LEDs are the mostly commonly used
components in many applications, mostly used for signal transmission
/power indication purposes.
• It is very cheaply and easily available in a variety of shape, color, and
size.
• The LEDs are also used for design message display boards and traffic
control signal lights etc.
• It has two terminals positive and negative as shown in the figure.
• Observe carefully the interface LED interface 2 is in forward biased
because the input voltage of 5v connected to the positive terminal of the
LED, So here the Microcontroller pin should be at LOW level and vice
versa with the LED interface 1 connections.
LED INTERFACING WITH 8051
PROGRAM FOR BLINKING OF LEDS
ORG 00H ; Assembly Starts from 0000H.
START: MOV P0, #0FFH ; Move 11111111 to PORT0.
ACALL DELAY ; Call DELAY
MOV A, P0 ; Move P0 value to ACC
CPL A ; Complement ACC
MOV P0, A ; Move ACC value to P0
ACALL DELAY ; Call DELAY
SJMP START ; Jump to START
DELAY: MOV R2, #255 ; Load Register R2 with 10 (0x0A)
WAIT1: DJNZ R2, WAIT1 ; Decrement R2 till it is 0. Jump to WAIT1 if not 0.
RET ; Return to Main Program
END ; End Assembly
SEVEN SEGMENT DISPLAY
• 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.
• 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‘.
• In common anode type, the anode of all LEDs are tied together
as a single terminal and cathodes are left alone as individual pins.
SEVEN SEGMENT DISPLAY INTERFACING
• Since these are basically LEDs arranged as a group they can
either have the anode in common or cathode thus they are
named as Common-Anode/Common-Cathode displays.
• Common Cathode: In this type of segments all the cathode
terminals are made common and tied to GND. Thus the
segments a to g needs a logic High signal(5v) in order to
glow.
• Common Anode: In this type of segments all the anodes
terminals are made common and tied to VCC(5v). Thus the
segments a to g needs a logic LOW signal(GND) in order to
glow.
Assembly language program to interface seven segment display (0-9) when it
connected to port1
ORG 0000H
MAIN: MOV DPTR, #400H
REPEAT: CLR A
MOVC A, @A+DPTR ; Copy data from external location to accumulator
MOV P1, A ; Move the pattern of the digit into port P1
ACALL DELAY ; Call a delay to so that the transition is visible
INC DPTR ; Point to the next pattern
CJNE A, 0, REPEAT ; Repeat till 0 (Stop bit) is received
SJMP MAIN ; Run this forever till externally stopped
DELAY:
MOV R0, #08H
LP2: MOV R1, #0FFH
LP1: MOV R2, #0FFH
LP3: DJNZ R2, LP3
DJNZ R1, LP1
DJNZ R0, LP2
RET
ORG 400H
DB 3FH, 06H, 5BH, 4FH, 66H, 6DH, 7DH, 07H, 7FH, 6FH, ; Lookup table for digits 0 to 9
END
Write a program to display
“HELLO” by using seven
segment display while it is
connected to port 0 of 8051.
ORG 0000H
MAIN: MOV DPTR, #200H
MOV R0,#05H
REPEAT: CLR A
MOVC A, @A+DPTR ; Copy data from external location to accumulator
MOV P0, A ; Move the pattern of the digit into port 0
ACALL DELAY ; Call a delay to so that the transition is visible
INC DPTR ; Point to the next pattern
DJNZ R0, REPEAT ; Repeat till R0 = 0
SJMP MAIN ; Run this forever till externally stopped
DELAY:
MOV R0, #08H
LP2: MOV R1, #0FFH
LP1: MOV R2, #0FFH
LP3: DJNZ R2, LP3
DJNZ R1, LP1
DJNZ R0, LP2
RET
ORG 200H
DB 76H, 79H, 38H, 38H, 3FH ; Lookup table for alphabets
Practice Problem
• Write a program to display “SCOPE” on seven segment display
connected to port 1 by calling the values from lookup table at code
memory 250H.
• Write a program to display “SCOPE” on seven segment display
connected to port 1 by calling the values from lookup table at code
memory 250H with a delay of 50μs in each alphabet.