0% found this document useful (0 votes)
105 views60 pages

ES - 8051 LED Interfacing

Uploaded by

alpha.gamer.661
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
105 views60 pages

ES - 8051 LED Interfacing

Uploaded by

alpha.gamer.661
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 60

Interfacing

Interfacing is a method, that provides communication between


Microcontroller and the interface device. An interface is either Input
device, or output device, or a storage device, or processing device.

Input Interface Devices: Push button switch, Keypad, Infrared


sensor, Temperature sensor, gas Sensor etc. These devices provide
some information to the Microcontroller, and this is called as input
data.

Output Interface Devices: LED, LCD, Buzzer, Relay driver, DC


Motor Driver, 7-Segment Display etc.

Storage Interface Devices: Used to store/ retain the data, example,


SD card, EEPROM, DataFlash, Real Time Clock, etc.
Interfacing of an LED with 8051
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 or dim. This program is called as the
driver/firmware. The driver software can be developed using
any programming language like Assembly, C etc.
8051 Microcontroller
The 8051 Microcontroller was invented in 1980’s by Intel. Its
foundation is based on Harvard architecture and this Microcontroller
was developed principally for bringing it to be used in Embedded
Systems. It is a 40 Pin PDIP (Plastic Dual Inline Package).

8051 has an on-chip oscillator, but it


requires an external clock to run it.
A quartz crystal is connected in
between the XTAL pins of the MC.
This crystal needs two same value
capacitors (33pF) for generating a
clock signal of the desired frequency.
LED (Light Emitting Diode)
LED is a semiconductor device that emits the light when the current
flows through it. It used in many electronic devices, 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.
The only way to know polarity is either to test it with a multimeter
or by carefully observing inside the LED. The larger end inside the
led is -ve (cathode) and the shorter one is +ve (anode), that is how
we find out the polarity of the LED. Another way to recognize the
polarity is, connecting leads, POSITIVE terminal has more length
than NEGATIVE terminal.
The circuit consists of an LED, a voltage supply and a resistor to
regulate the current and voltage.
LED Interfacing to 8051
There are two ways which we can interface LED to the
Microcontroller 8051. But the connections and programming
techniques will be different. LEDs typically require 15 to 20mA. Here
LED interfacing with 8051 and LED blinking code for AT89C52/
AT89C51 Microcontroller is given.
Observe carefully the interface LED 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 interface 1 connections.

The resistor is important in LED interfacing to limit the flowing


current and avoid damaging the LED and/or MCU.

Interface 1 will glow LED, only if the PIN value of the MC is HIGH
as current flows towards the ground.

Interface 2 will glow LED, only if the PIN value of the MC is LOW
as current flows towards PIN due to its lower potential.
How to Control LEDs?
Light Emitting Diodes are the semi conductor light sources.
Commonly used LEDs will have a cut-off voltage of 1.7V and
current of 10mA. When an LED is applied with its required voltage
and current it glows with full intensity.
The Light Emitting Diode is similar to the normal PN diode but it
emits energy in the form of light. The color of light depends on the
band gap of the semiconductor. The following figure shows “how an
LED glows?”
Thus, LED is connected to the AT89C51 microcontroller with the
help of a current limiting resistor. The value of this resistor is
calculated using the following formula.

R= (V-1.7)/10mA,

where V is the input voltage.


Generally, microcontrollers output a maximum voltage of 5V.
Thus, the value of resistor calculated for this is 330 Ohms. This
resistor can be connected to either the cathode or the anode of the
LED.
Seven Segment Display
Seven-segment displays are commonly used to display numbers and
characters in various electronic devices. It is a display device
consisting of seven LEDs arranged in a specific pattern to display
numerals and some letters or symbols. Each of the seven LEDs can
be controlled independently to display different combinations of
characters. By turning on or off the appropriate LEDs, it is possible
to display all the numbers from 0 to 9 and some letters like A, B, C,
D, E, F.
They are easy to read, low-cost, and consume very little power.
Interfacing a seven-segment display with a microcontroller like the
8051 is a simple task that can be accomplished with minimal
hardware and software requirements.
The 7-segment display, also written as “seven segment display”,
consists of seven LEDs (hence its name) arranged in a rectangular
fashion as shown. Each of the seven LEDs is called a segment
because when illuminated the segment forms part of a numerical digit
(both Decimal and Hex) to be displayed.
An additional 8th LED is sometimes used within the same package
thus allowing the indication of a decimal point, (DP) when two or
more 7-segment displays are connected together to display numbers
greater than ten.
Each one of the seven LEDs in the display is given a positional
segment with one of its connection pins being brought straight out of
the rectangular plastic package. These individually LED pins are
labelled from a through to g representing each individual LED. The
other LED pins are connected together and wired to form a common
pin.
The seven segments are labeled as A, B, C, D, E, F, and G. Each
segment is connected to a different pin of the display. In addition,
there is an eighth pin that controls the decimal point.
Interfacing a Seven-Segment Display with 8051: To interface a seven-
segment display with an 8051 microcontroller, we need to connect the
segments of the display to the output pins of the microcontroller. We
also need to connect the common cathode or anode of the display to a
suitable voltage source, depending on the type of display.
Seven-segment LED circuit configuration
LED display devices have two kinds of circuit: common anode and
common cathode.
Common anode: when the common pin is positive
Common cathode: when the common pin is negative
For a common cathode seven-segment display, we need to connect
the common cathode pin to ground and the segment pins to the
output pins of the 8051.

For a common anode display, we need to connect the common anode


pin to Vcc and the segment pins to the output pins of the 8051.

Once the display is connected to the microcontroller, we can write a


program to control the display. We can use the P1 port of the 8051 to
connect to the segment pins of the display. To display a number, we
need to send the appropriate combination of signals to the segment
pins. For example, to display the number 0, we need to turn on all the
segments except G. Similarly, to display the number 1, we need to
turn on only segments B and C.
1. The Common Cathode (CC) – In the common cathode display,
all the cathode connections of the LED segments are joined together
to logic “0” or ground. All LEDs negative terminals are connected to
common and the common terminal is connected to ground. That
means all the negative terminals of LEDs are connected to ground
and positive terminals are open. When “1” is sent on the
corresponding pin that LED will glow. The individual segments are
illuminated by application of a “HIGH”, or logic “1” signal via a
current limiting resistor to forward bias the individual Anode
terminals (a-g).

Common Cathode
Configuration
2. The Common Anode (CA) – In the common anode display, all the
anode connections of the LED segments are joined together to logic
“1”. That means all the positive ends of the LEDs are connected
together to common and that common is connected to +5v and the
LEDs are in forward biased and hence it turns on the LED. The
negative terminals are open. In this case “0” on the pin will glow the
LED. The individual segments are illuminated by applying a ground,
logic “0” or “LOW” signal via a suitable current limiting resistor to the
Cathode of the particular segment (a-g).

Common Anode
Configuration
How the seven segment display shows the
digits?
Table: Display numbers on a seven-segment display in common
cathode configuration

Number hgfedcba Hex Code


0 00111111 3F
1 00000110 06
2 01011011 5B
3 01001111 4F
4 01100110 66
5 01101101 6D
6 01111101 7D
7 00000111 07
8 01111111 7F
9 01001111 4F
Table: Display numbers on a seven-segment display in common
anode configuration

Number hgfedcba Hex code


0 11000000 C0
1 11111001 F9
2 10100100 A4
3 10110000 B0
4 10011001 99
5 10010010 92
6 10000010 82
7 11111000 F8
8 10000000 80
9 10010000 90
Interfacing Seven Segment To 8051
Write an ALP program for letting glow connected at port
pinP1.0, When P1.0 clears.
Blinking of LED
LED OFF = P1.0 is at ‘1’; SETBP1.0;
LED ON = P1.0 is at ‘0’; CLRB P1.0;
ORG 0000H
REP :CLR P1.0 //LED ON
LCALL DELAY
SETB P1.0 //LED OFF
LCALL DELAY
SJMP REP
(delay subroutine)
ORG 0100H
DELAY :MOV R1, #250
B1 :DJNZ R1, B1
RET
#include<reg51.h>
sbit LED= P1^0; // pin0 of port1 is named as LED
Void main(void)
{
While (1)
{
LED = 1;
delay(1000);
LED = 0;
Delay(1000);
}Return 0;
}
Void delay(unsigned int time)
{
Unsigned int i,j;
For(i=0;i<time;i++)
For (j=0;j<1275;j++);
}
Assembly Language Program for common cathod
seven segment display

ORG 0000H ORG 0050H


START: MOV R2, #0AH CODE :DB 3F, 06, 5B,
MOV DPTR, #CODE 4F, 66,
BACK:MOV A, #00H 6D,7D, 07, 7F, 6F
MOVC A, @A+DPTR ORG 0070H
MOV P1,A DELAY :MOV R1, 02H
LCALL DELAY B2 :MOV R0, #250D
INC DPTR B1 :DJNZ R0, B1
DJNZ R2, BACK DJNZ R1, B2
SJMP START RET
C program to display the numbers 0 to 9 on a common anode seven-
segment display connected to the 8051 microcontroller:
#include <reg51.h>
Void main(void)
{
Unsigned char seg[10] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82,
0xf8, 0x80, 0x90};
Unsigned char x;
Unsigned int i;
P1= 0x00; //Output configuration
While (1)
{
For (x=0;x<10;x++)
{
P1=seg[x];
For (i=0;i<60000;i++);
}
}
Multiplexing of 7 segment LED
If two or more seven segment displays have to be used and the
microcontroller unit does not have enough I/O ports to
accommodate all the input pins of the seven segment display units.
The best method to be adopted in such cases would be to use a
multiplexer.

By multiplexing, the number of seven segment units multiplexed


would be using only the seven output ports and enable pins equal to
the number of display units to display the output.

Now, how it is made possible to use only seven output ports to


display output of the number of seven segment display units at a
single time? The principle behind this is the persistence of vision.
Only one unit is working at a single time and the switching between
the seven segment display units are made faster so that the onlooker
is not able to recognize the switching.
A transistor is a three-terminal semiconductor that regulates the
current or voltage flow and acts or operates as a switch or gate for
signals.

If the base is forward biased, then it is ON.


If the base is not forward biased, then it is OFF.

P2.0 P2.1
Q1 Q2 State of Q1 State of Q2 Selection of LEDs
0 0 OFF OFF Not Q1, Q2
0 1 OFF ON LED Q2
1 0 ON OFF LED Q1
1 1 ON ON BOTH LEDs
Q1, Q2
Write a C program for common cathode to
display 7 and 8.
Steps: Select LED2 – Set P2.0
Provide CC code to display 7, P1 = 07H (07)
Select LED1 – Clear P2.0 and Set P2.1
Provide CC code to display 8, P1 = 08H (7F)

MOV P2, #00H


BACK :SETB P2.0
MOV P1, #07H
CLRB P2.0
SETB P2.1
MOV P1, #7FH
CLR P2.0
SJMP BACK
END
LCD Interface
An LCD is electronically modulated optical device shaped into thin,
flat panel made up of number of color/monochrome pixels filled
with liquid crystals.
It is an output device used in an Embedded systems.
LCD (Liquid Crystal Display)
In the recent years the LCD is finding the widespread use replacing
the LEDs.
This is due to the following reasons:
1.It is often used in battery powered electronic device because it
requires very small amounts of electronic power.
2.The declining prices of LCDs.
3.The ability to display characters, numbers and graphics.
4.Ease of programming for characters and graphics.
LCD types
Some of the formats that are available in the alphanumeric LCDs are
1. 16 x 1 : Displaying 16 characters in one row as shown in the
figure.

2. 16 x 2 : It is a 2 line display where each line displays 16


characters.

3. 20 x 4 : It is a 4 line display where each line displays 20 characters


in each row.
Basic LCD of 16 x 2
D0 – D7 = Digital Data Pins
RS = Register Select
RD / WR = Read / Write
EN = Enable
Vcc = + 5 V
Vee = Variable between 0 to +5V to vary the brightness
Vss = Ground
LCD Interfacing

LCD Connections
Pin Description

Vcc, Vss and Vee


While Vcc and Vss provide +5 V and ground, respectively, Vee is
used for controlling LCD contrast.

RS, Register Select


Two important registers inside the LCD. The RS pin is used for their
selection.
RS = 0, the instruction command code register is selected, allowing
the used to send a commands.
RS = 1, the data register is selected, allowing the user to send the
data to be displayed on the LCD.
R/W, Read / Write
R/W input allows the used to write information to the LCD or read
information from it.
R/W = 1 when reading;
R/W = 0 when writing.

E, enable
The enable pin is used by the LCD to latch information presented to
its data pins.
When the data is supplied to the data pins, a high - to - low pulse
must be applied to this pin in order for LCD to latch in the data
present at data pins.
D0 – D7
The 8 bit data pins, D0 – D7, are used to send the information to the
LCD or to read the contents of the LCD’s internal registers.
To display letters A-Z, a-z, and the numbers 0-9 to these pins while
making RS = 1.
There are also instruction command codes that can be sent to the LCD
to clear the display or to force the cursor to the home position or blink
the cursor.
We also use RS=0 to check the busy flag bit to see if the LCD is
ready to receive the information. The busy flag is D7 and can be read
when R/W=1 and RS=0, as follows:
If R/W=1, RS=0
When D7=1 (busy flag=1), the LCD is busy taking care of internal
operations and will not accept any new information.
When D7=0, the LCD is ready to receive new information.
It is recommended to check the busy flag before writing any data to
the LCD.
LCD Command Codes
Steps to place the command and data
to the LCD interface

Steps to give command Steps to give data


1. Place the command on port 1.1. Place the data on port 1
2. Make RS = 0 (command) 2. Make RS = 1 (data)
3. Make R/W=0 (write) 3. Make R/W=0 (write)
4. Make Write Enable 1 and 4. Make Write Enable 1 and
then 0 then 0.
5. Call delay (10ms) 5. Call delay (10ms)
Write an assembly language program to display
‘HELLO MREC’
DPTR 0500 ‘H’
0501 ‘E’
0502 ‘L’
0503 ‘L’
0504 ‘0’
0505 ‘ ’
0506 ‘M’
0507 ‘R’
0508 ‘E’
0509 ‘C’
MOV DPTR, #0500H
INT: MOV A, #38H ;2 lines & 5x7 matrix
ACALL CMD
MOV A, #0FH ;display on, cursor blinking
ACALL CMD
MOV A, #01H ;clear display screen
ACALL CMD
MOV A, #06H ;Increment cursor(shift cursor to right
ACALL CMD
MOV A, #80H ;force cursor to beginning of 1st line
ACALL CMD
MOV R7, #0AH ;move 10 characters
MOV R0, #00H
AGAIN:MOV A, R0
MOVC A, @A+DPTR
ACALL DATA
INC R0
DJNZ R7, AGAIN
CMD: MOV P1, A
CLR P2.0
CLR P2.1
SETB P2.2
ACALL DELAY
CLRB P2.2
RET

DATA :MOV P1, A


SETB P2.0
CLR P2.1
SETB P2.2
ACALL DELAY
CLRB P2.2
RET

STRING : ORG 0500H


DB ‘HELLO MREC’
Keyboard Interfacing
Keyboards and LCDs are most widely used input/output devices of the
8051, and a basic understanding of them is essential.
Ex: Calculator, mobile phones, swiping machine, all has key pad.

Interfacing the keyboard to the 8051


At the lowest level, keyboards are organized in a matrix of rows and
columns. The CPU accesses both rows and columns through ports;
therefore, with two 8 bit ports, an 8 x 8 matrix of keys can be
connected to a microprocessor.
When a key is pressed, a row and column make a contact; otherwise,
there is no connection between rows and columns.
In IBM PC single microcontroller takes care of hardware and software
interfacing of the keyboard.
In such systems, it is the function of the programs stored in the
EPROM of the microcontroller to scan the keys continuously, identify
which key has been activated and present it to the motherboard.
4 x 4 matrix keypad
Rows and columns are connected to the different ports. Whenever the
key is pressed the terminals to which the key is connected will get
shorted.
Ex: whenever the key 1 is pressed the row R1 and C3 gets shorted.
Interfacing keyboard to 8051
Scanning and identifying the key
In the above figure 4 x 4 matrix connected to two ports.
The rows are connected to an output port and the columns are
connected to an input port.

If no key is pressed, reading the input port will yield 1s for all
columns since they are all connected to high (Vcc).

If all the rows are grounded and a key is pressed, one of the columns
will have 0 since the key pressed provides the path to the ground.

It is the function of the microcontroller to scan the keyboard


continuously to detect and identify the key pressed.
Grounding Rows And Reading The Columns
To detect a pressed key, the microcontroller grounds all rows by
providing 0 to the output latch, then it reads the columns.

If the data read from the columns is D3 – D0 = 1111, no key has been
pressed and the process continues until a key press is detected.

However, if one of the column bits has a 0, this means that a key press
has occurred.
Ex: If D3 – D0 = 1101

This means that a key in the D1 column has been pressed. After a key
press is detected, the microcontroller will go through the process of
identifying the key.
Starting with the top row, the microcontroller grounds it by providing
a low to row D0 only; then it reads the columns.

If the data read is all 1s, no key in that row is activated and the
process is moved to next row.

After the identification of the row in which the key has been pressed,
the next task is to find out which column the pressed key belongs to.
This should be easy since the microcontroller knows at any time
which row and column are being accessed.
The above 8051 assembly language program is used for detection and
identification of the key activation. In this program, it is assumed that
P1 and P2 are initialized as output and input, respectively. It goes
through the following four major stages:

To make sure that the preceding key has been released, 0s are output
to all rows at once, and the columns are read and checked repeatedly
until all the columns are high. When all columns are found to be high,
the program waits for a short amount of time before it goes to the next
stage of waiting for a key to be pressed.

To see if any key is pressed, the columns are scanned over and over in
an infinite loop until one of them has a 0 on it. Remember that the
output latches connected to rows still have their initial zeros (provided
in stage 1), making them grounded. After the key press is detected,
microcontroller waits for 20ms for the bounce and then scans the
column again.
To detect which row the key press belongs to, the microcontroller
grounds one row at a time, reading the columns each time. If it finds
that all the columns are high, this means that the key press cannot
belong to that row; therefore, it grounds the next row and continues
until it finds the row the key pressed belongs to. Upon finding the
row that the key press belongs to, it sets up the starting address for
the look up table holding the scan codes for that row and goes to the
next stage to identify the key.

To identify the key press, the microcontroller rotates the column


bits, one bit at a time, into the carry flag and checks to see if it is
low. Upon finding the zeros, it pulls out the ASCII code for that key
from the look up table; otherwise it increments the pointer to point
to the next element of the look up table.
Flow chart for the program

You might also like