BECE201L
Microprocessors and Microcontrollers
Module-5
I/O interfacing with Microcontroller 8051
Part-1
Module:5 I/O interfacing with Microcontroller 8051
LCD, LED, Keypad, Analog-to-Digital Convertors, Digital-to-
Analog Convertors, Sensor with Signal Conditioning Interface.
7 hours
Introduction to LED
• Simplest display element.
• Available in wide varieties of shapes and colors.
• Used to indicate the status of the device like powered on, running,
waiting, error, etc.
• Commonly used as indicator in majority of the electronic appliances
like televisions, audio/video systems, printers, washing machines, disk
drives, control panels, etc.
• The color of an LED is determined by the semiconductor material used
for it.
Introduction to LED
• The most common colors are red, orange, yellow, green, blue and
white.
Introduction to LED
• The LED can be interfaced with 8051 using
two different configurations.
• Method 1:
• The port pin will source the current
when the port pin is high for
configuration.
• A current limiting resistor should be
connected as shown to protect the
LED as well as port circuits.
Introduction to LED
• The LED can be interfaced with 8051 using
two different configurations.
• Method 2:
• The LED will glow when the pin is
made low, where it sinks the current.
• A current limiting resistor should be
connected as shown to protect the
LED as well as port circuits.
Introduction to LED
• The pin P1.0 is connected to a LED through
buffer (inverter).
• When P1.0 is high: The output of inverter is low
and causes the current to flow. LED glows.
• When P1.0 is low: The output of inverter is
high, so no current flow. LED not glows.
• The inverter acts as a current buffer and
prevents the port pins from current loading, the
current flows between the inverter and the 5 V
supply.
• The resistor limits the current through the LED
and protects it from damage.
Example Problem-1
Write an ALP to blink a LED which is connected with P1.0.
Example Problem-1
Write an ALP to blink a LED which is connected with P1.0.
AGAIN: SETB P1.0 ; Set Pin P1.0 to 1
ACALL DELAY
CLR P1.0 ; Clear P1.0 to 0
ACALL DELAY
SJMP AGAIN
Example Problem-1
Delay calculation for 0.5 s delay
For the crystal frequency of 12MHz
• Machine Cycle frequency is 12MHz/12 = 1MHz
• Time period for 1 Machine cycle = 1/1MHz = 1µs
• DJNZ will take 2 Machine cycles
• So, for 0.5s delay
MC×TP×N( No of loops) = 2×1µs×N = 0.5
N = 250000= 250× 250× 4 (Three Nested Loops)
Example Problem-1
Write an ALP to blink a LED which is connected with P1.0.
DELAY: MOV R5, #4 ; For generating 0.5sec delay for
H3: MOV R4, #250 ; 12MHz crystal frequency
H2: MOV R3, #250
H1: DINZ R3, H1
DINZ R4, H2
DJNZ R5, H3
RET
END
Example Problem-2
Write an ALP to blink LED connected on Port 2.
Example Problem-2
Write an ALP to blink LED connected on Port 2.
MOV A, #00H
MOV P2, A ; Make P2 as output port
MOV A, #00H ; Clear Accumulator
BACK: MOV P2, A ; Copy Accumulator to P2
ACALL DELAY
CPL A ; Complement Accumulator
SJMP BACK
Example Problem-2
Delay calculation for 0.5 s delay
For the crystal frequency of 12MHz
• Machine Cycle frequency is 12MHz/12 = 1MHz
• Time period for 1 Machine cycle = 1/1MHz = 1µs
• DJNZ will take 2 Machine cycles
• So for 0.5s delay
MC×TP×N( No of loops) = 2×1µs×N = 0.5
N = 250000= 250× 250× 4 (Three Nested Loops)
Example Problem-2
Write an ALP to blink LED connected on Port 2.
DELAY: MOV R5, #4 ; For generating 0.5sec delay for
H3: MOV R4, #250 ; 12MHz crystal frequency
H2: MOV R3, #250
H1: DINZ R3, H1
DINZ R4, H2
DJNZ R5, H3
RET
END
Example Problem-3
Write an ALP to read the status of a switch connected on P0.0 and turn the
LED connected on P2.0 ON/ OFF as per switch position.
Example Problem-3
Write an ALP to read the status of a switch connected on P0.0 and turn the
LED connected on P2.0 ON/ OFF as per switch position.
ORG 0000H
SETB P0.0 ; make P0.0 an input
CLR P2.0 ; make P2.0 an output
AGAIN: JNB P0.0, NEXT ; If SW is OFF go to NEXT
SETB P2.0 ;Turn LED ON
SJMP AGAIN
NEXT: CLR P2.0 ; Turn the LED OFF
SJMP AGAIN
END
Example Problem-4
Interface eight LEDs and eight pushbutton switches to Port 3 and Port 2 of
89C51 respectively. Write a program to monitor the status of all switches
and display it on the corresponding LEDs, i.e. LED 0 should glow when
switch 0 is pressed and so on.
Example Problem-4
Interface eight LEDs and eight pushbutton switches to Port 3 and Port 2 of
89C51 respectively. Write a program to monitor the status of all switches
and display it on the corresponding LEDs, i.e. LED 0 should glow when
switch 0 is pressed and so on.
ORG 0000H
AGAIN: MOV A, P2 ; get position of SW in Accumulator
MOV P3, A ; Glow LEDs as per SW position
SJMP AGAIN
END
Example Problem-5
write a program to glow LEDS one by one in a sequence continuously.
ORG 0000H
MOV A, #01H ; Only one bit is High. One LED glows
REPEAT: MOV P3, A ; Send value to the port
ACALL DELAY ; Wait for delay
RL A ; Rotate to glow the next LED
SJMP REPEAT ; Repeat process forever
Example Problem-5
write a program to glow LEDS one by one in a sequence continuously.
DELAY: MOV R0, #10 ; Delay for approx. 1s for 12MHz Clock
L2: MOV R1, #200
L1: MOV R2, # 250
HERE: DJNZ R2, HERE
DJNZ R1, L1
DJNZ R0, L2
RET
END
Seven segment display
• Seven (8th LED may be used for Decimal point) LEDs are placed in the
form of figure 8.
• Digits 0-9 (Even Hex numbers) can be displayed.
• The pin out scheme of a typical 7 segment LED display is below.
Seven segment display
• Seven Segment Display can be used in two different configurations.
• Common Anode:
• The cathode of all LEDs are tied together to a single terminal which is
usually labeled as ‘com’.
• The anode of all LEDs are left alone as individual pins labeled as a, b,
c, d, e, f, g & h (or dot) .
Seven segment display
• Seven Segment Display can be used in two different configurations.
• Common Cathode:
• 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
• Digit drive pattern is simply the different logic combinations
of its terminals ‘a’ to ‘h’ in order to display different digits and
characters. Digit h g f e d c b a Hex
0 1 1 0 0 0 0 0 0 C0
1 1 1 1 1 1 0 0 1 F9
2 1 0 1 0 0 1 0 0 A4
3 1 0 1 1 0 0 0 0 B0
4 1 0 0 1 1 0 0 1 99
5 1 0 0 1 0 0 1 0 92
6 1 0 0 0 0 0 1 0 82
7 1 1 1 1 1 0 0 0 F8
8 1 0 0 0 0 0 0 0 80
9 1 0 0 1 0 0 0 0 90
Seven segment display
• Digit drive pattern is simply the different logic combinations
of its terminals ‘a’ to ‘h’ in order to display different digits and
characters. Digit h g f e d c b a Hex
Common Cathode 0 0 0 1 1 1 1 1 1 3F
1 0 0 0 0 0 1 1 0 06
• Complement of 2 0 1 0 1 1 0 1 1 5B
Common Anode 3 0 1 0 0 1 1 1 1 4F
4 0 1 1 0 0 1 1 0 66
5 0 1 1 0 1 1 0 1 6D
6 0 1 1 1 1 1 0 1 7D
7 0 0 0 0 0 1 1 1 07
8 0 1 1 1 1 1 1 1 7F
9 0 1 1 0 1 1 1 1 6F
Seven segment display
Interface a Seven Segment Display with 8051 using P2.
Seven segment display
• In 8051 ALP’s, a look up table is used to configure 7 segment display.
Digit Address Hex
0 0400 3F
1 0401 06
2 0402 5B
3 0403 4F
4 0404 66
5 0405 6D
MOV DPTR, #0400H
6 0406 7D MOV A, #01H
7 0407 07 MOVC A, @A+DPTR
8 0408 7F
MOV P2, A
9 0409 6F
Seven segment display
Display ‘2025’ using Seven segment display interfacing with 8051.
• To interface 4 Seven segment displays with the 8051 microcontroller,
4 driver Transistors are used to select one SSD at a time (Through
P2).
• Send the hex codes one by one through P0.
• Provide atleast 1ms delay in between hex codes.
Seven segment display
Display ‘2025’ using Seven segment display interfacing with 8051.
Seven segment display
1/3
Display ‘2025’ using Seven segment display interfacing with 8051.
MAIN: MOV DPTR, #0400H ; Look up table Address
MOV A, #02H ; To load Hex code of digit ‘2’
MOVC A, @A+DPTR
MOV P2, #01H ; Select 1st LED
MOV P0, A ; Send hex code of ‘2’ to P0
ACALL DELAY
MOV A, #00H ; To load Hex code of digit ‘0’
MOVC A, @A+DPTR
MOV P2, #02H ; Select 2nd LED
MOV P0, A ; Send hex code of ‘0’ to P0
ACALL DELAY
Seven segment display
2/3
Display ‘2025’ using Seven segment display interfacing with 8051.
MOV A, #02H ; To load Hex code of digit ‘2’
MOVC A, @A+DPTR
MOV P2, #04H ; Select 3rd LED
MOV P0, A ; Send hex code of ‘2’ to P0
ACALL DELAY
MOV A, #05H ; To load Hex code of digit ‘5’
MOVC A, @A+DPTR
MOV P2, #08H ; Select 4th LED
MOV P0, A ; Send hex code of ‘5’ to P0
ACALL DELAY
SJMP MAIN ; Repeat
Seven segment display
3/3
Display ‘2025’ using Seven segment display interfacing with 8051.
DELAY: MOV R3,#02H ; Creates ‘1ms’ delay
LOOP1: MOV R2,#0FAH
LOOP2: DJNZ R2, LOOP2
DJNZ R3, LOOP1
RET
Liquid Crystal Display (LCD)
• An LCD is electronically modulated optical device shaped into a thin, flat
panel.
• Made up of No. of color or monochrome pixels filled with liquid crystals.
• LCD is finding widespread use and replaces LEDs in many applications.
• The declining prices of LCD.
• The ability to display numbers, characters, and graphics.
• Incorporation of a refreshing controller with the LCD relieves the
CPU of the task of refreshing the LCD.
• Ease of programming for characters and graphics.
Liquid Crystal Display (LCD)
• Depending on No. of characters and No. of Rows wide variety of LCD
panels are available.
• The size is specified as No. of Characters in Each Row* No. of Rows.
16*1 LCD
16*4 LCD 20*4 LCD
16*2 LCD
Interfacing of LCD with 8051
• The 1602 is a type of LCD that can show alphanumeric characters.
• 16 characters in 02 rows.
Some key features are
1. Display Size:
• Displays up to 32 characters (16 characters on two rows).
2. Character Set:
• Supports a 5*7 dot matrix per character, allowing for the display of
basic letters, numbers, punctuation marks, and some special
characters.
Interfacing of LCD with 8051
Some key features are
3. Backlight:
• Many models come with an optional LED backlight, making the
display readable in low-light conditions. (Blue/ Green)
4. Contrast Adjustment:
• The contrast of the characters on the LCD can be adjusted using a
potentiometer connected to the VCC pin.
5. 4-bit or 8-bit Communication:
• It can operate in either 4-bit or 8-bit data mode, meaning it can
communicate with a microcontroller using 4 or 8 data lines.
Interfacing of LCD with 8051
LCD 1602 is a 16-pin device with
• 8 data pins
• (D0-D7 for Data/ Command issue.
• 3 control pins (RS, RW, EN)
• To configure the LCD in command mode
or data mode.
• To configure read mode or write mode
• The remaining 5 pins are for the supply and
the backlight of the LCD.
Interfacing of LCD with 8051
16*2 LCD Pin Details
No. Pin Name Function
1 VSS Ground (0V)
2 VCC Power Supply (+5V)
3 VEE Contrast Control (0V-High)
4 RS Register Select
0: Command / 1: Data Reg.
5 RW Read/ Write
0: Write / 1: Read
6 E Enable (H-L pulse)
7-14 D0-D7 Data Pins (D7-Busy Flag)
15 LED+ Back light Anode (+5 V)
16 LED- Backlight Cathode (0V)
Interfacing of LCD with 8051
The Pins are
• 𝑽𝑪𝑪 , 𝑽𝑬𝑬 𝒂𝒏𝒅 𝑽𝑺𝑺 :
• 𝑉𝐶𝐶 𝑎𝑛𝑑 𝑉𝑆𝑆 provide +5V and GND to the LCD respectively.
• RS (Register Select):
• There are two most important register in LCD.
• If RS = 0, command code register is selected, allowing the user to send a
command such as clear display, Cursor at home, etc.
• If RS = 1, the data register is selected, allowing the user to Send data to
be displayed on the LCD.
Interfacing of LCD with 8051
The Pins are
• R/W (Read/ Write):
• R/W input allows the user to write information to the LCD or read from
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 pin.
• A high-to-low pulse must be applied to this pin in order for the LCD to
latch in the data present at the data pins.
• This pulse must be a minimum of 450 ns wide.
Interfacing of LCD with 8051
The Pins are
• Data pins (D0-D7):
• The 8-bit data pins, D0 - D7, are used to send information to the LCD or
read the contents of the LCD's internal registers.
• To display letters and numbers, send ASCII codes for the letters A - Z, a - z,
and numbers 0 - 9 to these pins while making RS = 1.
Interfacing of LCD with 8051
• This LCD has two registers, namely, Command and Data.
• The command register stores the command instructions given to the LCD.
• A command is an instruction given to LCD to do a predefined task like
initializing , clearing screen, setting the cursor position, controlling the
display, etc.
• The data register stores the data to be displayed on the LCD.
• The data is the ASCII value of the character to be displayed on the LCD.
• In order to use certain commands to the LCD must be given in command
mode.
• Once the LCD is configured, then data to be displayed can be given using
Data Mode.
Interfacing of LCD with 8051
• There are also instruction command codes that can be sent to the LCD to
clear the display or force the cursor to the home position or blink the cursor...
• Some of the instruction commands are
Code Command to LCD Execution Time
(HEX)
0x01 Clear the display screen 1.64ms
0x06 Shift the cursor right (e.g. data gets written in an 40 us
incrementing order, left to right)
0x0C Display on, cursor off 40 us
0x0E Display on, cursor blinking 40 us
0x80 Force the cursor to the beginning of the 1st line 40 us
0xC0 Force the cursor to the beginning of the 2nd line 40 us
0x10 Shift cursor position to the left 40 us
0x14 Shift cursor position to the right 40 us
Interfacing of LCD with 8051
• Some of the instruction commands are
Code Command to LCD Execution Time
(HEX)
0x18 Shift entire display to the left 40 us
0x1C Shift entire display to the right 40 us
0x38 2 lines, 5x8 matrix, 8-bit mode 40 us
0x28 2 lines, 5x8 matrix,4-bit mode 40 us
0x30 1 line, 8-bit mode 40 us
0x20 1 line, 4-bit mode 40 us
Interfacing of LCD with 8051
Interfacing of LCD with 8051
• LCD 1602 in 8-bit mode:
• 8-bit mode can be enabled by giving 38H to LCD.
• Uses eight pins to issue data/ command.
• Data pins (D0-D7) must be connected with eight I/O line of 8051
E.g.: Port 1 (P1.0 to P1.7) is connected with D0-D7
• LCD 1602 in 4-bit mode:
• 4-bit mode can be enable by giving 28H to LCD.
• Only four pins are used for data/ command.
• data/command is sent in a 4-bit (nibble) format.
• Higher nibble followed by lower nibble of data/command is given to
LCD.
Interfacing of LCD with 8051
• The LCD internals takes some time to latch and make the necessary
adjustments as per the command word.
• Delay subroutines can be used to allow LCD to finish the commands.
(More delay required and waste of computation power).
• A much better way is to monitor the busy flag before issuing a command
or data to the LCD.
• The busy flag (D7) can be read when R/W= 1 and RS = 0.
• If D7 =1, the LCD is busy taking care of internal operations and will not
accept any new information.
• If D7 = 0, the LCD is ready to receive new information.
• The E line is negative-edge triggered for the write while it is positive-
edge triggered for the read.
Interfacing of LCD with 8051
Interfacing of LCD with 8051
To interface 1602 LCD with 8051, follow these mandatory steps:
1. Initialization:
• Set the LCD to 8-bit mode by sending initialization commands.
2. Set RS and RW Pins:
• RS: Set to 0 for commands, 1 for data.
• RW: Set to 0 for writing, 1 for reading (not often used in basic setups).
3. Send Commands/Data:
• Place the command or data in the data register.
4. Enable Pulse:
• Toggle the Enable Pin to send the command/data.
5. Delay:
• Wait briefly after each command/data transfer to allow the LCD to
process it.
Interfacing of LCD with 8051
;Calls a time delay before sending next data/ command to LCD
;P1.0 to P1.7 are connected to LCD data pins D0-D7.
1/6
;P2.0 connected to RS pin
;P2.1 connected to R/W pin
;P2.2 connected to E pin
ORG 000H
MOV A, #38H ; Initialize LCD for 2 lines, 5x7 matrix
LCALL COMNWRT ; Call command write subroutine
LCALL DELAY ; Give LCD some time
MOV A, #0EH ; Display ON, Cursor ON
LCALL COMNWRT ; Call command write subroutine
LCALL DELAY ; Give LCD some time
Interfacing of LCD with 8051
MOV A, #01H ; Clear LCD display
LCALL COMNWRT ; Call command write subroutine 2/6
LCALL DELAY ; Give LCD some time
MOV A, #06H ; Shift cursor to the right
LCALL COMNWRT ; Call command write subroutine
LCALL DELAY ; Give LCD some time
MOV A, #85H ; Set cursor at line 1, position 1
LCALL COMNWRT ; Call command write subroutine
LCALL DELAY ; Give LCD some time
Interfacing of LCD with 8051
MOV A, #’V' ; Display letter 'E'
LCALL DATAWRT ; Call data write subroutine 3/6
LCALL DELAY ; Give LCD some time
MOV A, #’I' ; Display letter ‘I'
LCALL DATAWRT ; Call data write subroutine
LCALL DELAY ; Give LCD some time
MOV A, #’T' ; Display letter ‘T'
LCALL DATAWRT ; Call data write subroutine
LCALL DELAY ; Give LCD some time
AGAIN: SJMP AGAIN ; stay here
Interfacing of LCD with 8051
; Command Write Subroutine
COMNWRT: 4/6
MOV P1, A ; Copy content of register A to port 1
CLR P2.0 ; Set RS = 0 for command mode
CLR P2.1 ; Set R/W = 0 for write mode
SETB P2.2 ; Set E high for pulse
ACALL DELAY ; Give LCD some time
CLR P2.2 ; E=0 for H-to-L pulse
RET
Interfacing of LCD with 8051
; Data Write Subroutine
DATAWRT: 5/6
MOV P1, A ; Copy content of register A to port 1
SETB P2.0 ; Set RS = 1 for data mode
CLR P2.1 ; Set R/W = 0 for write mode
SETB P2.2 ; Set E high for pulse
ACALL DELAY ; Give LCD some time
CLR P2.2 ; E=0 for H-to-L pulse
RET
Interfacing of LCD with 8051
; Delay Subroutine
DELAY: 6/6
MOV R3, #50 ;Set outer loop count (> 50 for fast CPUs)
HERE2: MOV R4, #255 ; Set inner loop count (255)
HERE: DJNZ R4, HERE ; Decrement R4 and repeat until it reaches 0
DJNZ R3, HERE2 ; Decrement R3 and repeat until it reaches 0
RET
END ; End of program
Interfacing of LCD with 8051
;Check busy flag before sending data, command to LCD
;P1.0 to P1.7 are connected to LCD data pins D0-D7.
1/5
;P2.0 connected to RS pin
;P2.1 connected to R/W pin
;P2.2 connected to E pin
ORG 000H
MOV A, #38H ; Initialize LCD for 2 lines, 5x7 matrix
LCALL COMMAND ; Issue command
MOV A, #0EH ; Display ON, Cursor ON
LCALL COMMAND ; Issue command
MOV A, #01H ; Clear LCD display
LCALL COMMAND ; Issue command
Interfacing of LCD with 8051
MOV A, #06H ; Shift cursor to the right
LCALL COMMAND ; Issue command 2/5
MOV A, #85H ; Set cursor at line 1, position 1
LCALL COMMAND ; Issue command
MOV A, #’V' ; Display letter 'E'
ACALL DATA_DISPLAY
MOV A, #’I' ; Display letter ‘I'
ACALL DATA_DISPLAY
MOV A, #’T' ; Display letter ‘T'
ACALL DATA_DISPLAY
AGAIN: SJMP AGAIN ; stay here
Interfacing of LCD with 8051
; Command Write Subroutine
COMMAND: 3/5
ACALL READY ; is LCD ready?
MOV P1, A ; Copy content of register A to port 1
CLR P2.0 ; Set RS = 0 for command mode
CLR P2.1 ; Set R/W = 0 for write mode
SETB P2.2 ; Set E high for pulse
CLR P2.2 ; E=0 for H-to-L pulse, latch in
RET
Interfacing of LCD with 8051
; Data Write Subroutine
DATA_DISPLAY: 4/5
ACALL READY ; is LCD ready?
MOV P1, A ; Copy content of register A to port 1
SETB P2.0 ; Set RS = 1 for data mode
CLR P2.1 ; Set R/W = 0 for write mode
SETB P2.2 ; Set E high for pulse
CLR P2.2 ; E=0 for H-to-L pulse, latch in
RET
Interfacing of LCD with 8051
; Busy flag Check Subroutine
READY:
5/5
SETB P1.7 ; Set P1.7 input port
CLR P2.0 ; RS=0 access command register
SETB P2.1 ; R/W = 1 read command register
;read command register and check busy flag
BACK: CLR P2.2 ; E=0 for L-to-H pulse
ACALL DELAY ; give LCD some time
SETB P2.2 ; E=1 L-to-H pulse
JB P1.7, BACK ; Stay until busy flag=0
RET
END
Example Problem-1
Write an ALP to display “CSE@VIT VELLORE” on LCD which is connected to
port 1.
1/5
;calls a time delay before sending next data command
;P1.0-P1.7 used for D0-D7, P2.0 is RS, P2.1 is R/W, and P2.2 is E pins
ORG 0H
MOV DPTR, #MYCOM
C1: CLR A
MOVC A, @A+DPTR
ACALL COMNWRT ; call command subroutine
ACALL DELAY ; give LCD some time
JZ SEND_DAT
INC DPTR
SJMP C1
Example Problem-1
SEND_DAT: MOV DPTR, #MYDATA
Dl: CLR A
2/5
MOVC A, @A+DPTR
ACALL DATAWRT ;call data write subroutine
ACALL DELAY ; give LCD some time
INC DPTR
JZ AGAIN
SJMP D1
AGAIN: SJMP AGAIN
Interfacing of LCD with 8051
; Command Write Subroutine
COMNWRT: 3/5
LCALL READY ; Prepare for sending command to LCD
MOV P1, A ; Copy content of register A to port 1
CLR P2.0 ; Set RS = 0 for command mode
CLR P2.1 ; Set R/W = 0 for write mode
SETB P2.2 ; Set E high for pulse
ACALL DELAY ; Give LCD some time
CLR P2.2 ; E=0 for H-to-L pulse
RET
Interfacing of LCD with 8051
; Data Write Subroutine
DATAWRT: 4/5
MOV P1, A ; Copy content of register A to port 1
SETB P2.0 ; Set RS = 1 for data mode
CLR P2.1 ; Set R/W = 0 for write mode
SETB P2.2 ; Set E high for pulse
ACALL DELAY ; Give LCD some time
CLR P2.2 ; E=0 for H-to-L pulse
RET
Interfacing of LCD with 8051
; Delay Subroutine
DELAY: 5/5
MOV R3, #50 ;Set outer loop count (> 50 for fast CPUs)
HERE2: MOV R4, #255 ; Set inner loop count (255)
HERE: DJNZ R4, HERE ; Decrement R4 and repeat until it reaches 0
DJNZ R3, HERE2 ; Decrement R3 and repeat until it reaches 0
RET
END ; End of program
ORG 300H
MYCOM: DB 38H, 0EH, 01H, 06H, 85H, 0 ; command and null
MYDATA: DB “CSE@VIT VELLORE”, 0 ; data and null
Keypad interfacing
• Keyboards and LCDs are the most widely used input/output devices.
• The Keyboard is a major input device used with computers and other
electronic devices.
• Takes input from the user in the form of text, numbers, and several
other commands.
• Many different keyboard technologies have been developed for
consumer demands and optimized for industrial applications.
• The standard full-size (100%) computer alphanumeric keyboard typically
uses 101 to 105 keys.
Keypad interfacing
• Many different keyboard technologies have been developed for
consumer demands and optimized for industrial applications.
• Mechanical keyboard
• Spring switch
• Scissor-switch
• Interlocking plastic pieces resembling scissors
• Reed
• Small magnet used to open/ close
• Capacitive
Keypad interfacing
• Many different keyboard technologies have been developed for
consumer demands and optimized for industrial applications.
• Mechanical keyboard
• Membrane keyboard (2 or 3 layers of membrane used)
• Optical keyboard (optical signal deflection upon key press)
• Hall effect keyboard (magnet used to produce hall effect)
Keypad interfacing
• Virtual keyboards, which are mostly accessed via a touchscreen interface,
have no physical switches and provide artificial audio and haptic feedback
instead.
• This variety of keyboard can prove useful, as it is not limited by the rigid
nature of physical computer keyboards.
• Mechanical keyboards are popular among gamers and typists seeking
enhanced accuracy and responsiveness.
• Contact bounce problem- switch contacts do not come to rest immediately.
• Human cannot type more than 50 keys per second.
Keypad interfacing
• Consider a simple push switch interface.
• Easy to interface.
• Simple to use.
• One I/O pin per switch can be used for
interfacing.
• For ‘n’ switches ‘n’ I/O lines are required.
• The standard full-size (100%) computer
alphanumeric keyboard typically uses 101 to
105 keys. (Needs 105 I/O lines!→
impossible).
• Matrix concept is used to overcome above
issue.
Keypad interfacing
• At the lowest level, keyboards arc 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 (64 keys) matrix of keys can be connected to a
microprocessor.
• When a key is pressed,
• A row and a column make a contact.
• Otherwise, there is no connection between rows and columns.
4X4 matrix Keypad interfacing
• There are 16 keys in the 4X4 matrix
Keypad.
• 4 Rows and 4 columns are used to
interface keypad with microcontrollers.
4X4 matrix Keypad interfacing
Scanning and identifying the key:
• The rows are connected to an output port and
the columns arc connected to an input port.
• If no key is pressed, reading of the input port
gives 1’s for all columns since they are all
connected to high (𝑉𝑐𝑐 ).
• If all the rows arc grounded and a key is
pressed, one of the columns will have 0 since
the key pressed provides the path to ground.
• It is the function of the microcontroller to scan
the keyboard continuously to detect and
identify the key pressed.
4X4 matrix Keypad interfacing
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 (𝑫𝟎 − 𝑫𝟑 = 𝟏𝟏𝟏𝟏), no key has been
pressed and the process continues until a key press is detected,
• However, if one of the column bits has a zero, this means that a key press has
occurred.
• For example, if (𝑫𝟎 − 𝑫𝟑 = 𝟏𝟏𝟎𝟏), this means that a key in the 𝐷1 column has
been pressed.
• After a key press is detected, the microcontroller will go through the process of
identifying the key.
4X4 matrix Keypad interfacing
Grounding rows and reading the columns:
• Starting with the top row, the microcontroller grounds it by providing a low to
row 𝐷0 only; then it reads the columns.
• If the data read is all 1’s, no key in that row is activated and the process is moved
to the next row.
• It grounds the next row, reads the columns, and checks for any zero.
• This process continues until the row is identified.
• After identification of the row in which the key has been pressed.
• The next task is to find out which column the key belongs to.
• This should be easy since the microcontroller knows at any time which row and
column are being accessed.
ALP for 4X4 Keypad interfacing with 8051
• To interface 4x4 keypad with 8051
configure
• Ports P1 as output and
• Port P2 as input.
• Unused pins of above ports are
grounded. (not used for interfacing)
• Port 0 can be used to display the key
pressed using Seven segment Display/
LCD display.
ALP for 4X4 Keypad interfacing with 8051
Steps to interface 4x4 keypad with 8051: (Ports P1 and P2 are initialized as output
and input ports).
1. To ensure debouncing:
• 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.
ALP for 4X4 Keypad interfacing with 8051
2. To see whether a key is pressed:
• The columns arc scanned over and over in an infinite loop until one of them
has a 0 on it.
• After the key press detection, the microcontroller waits 𝟐𝟎 𝒎𝒔 for the bounce
and then scans the columns again.
• It ensures that the first key press detection was not an erroneous one due
to a spike noise, and
• The 𝟐𝟎 𝒎𝒔 delay prevents the same key press from being interpreted as a
multiple key press.
• If after the 𝟐𝟎 𝒎𝒔 delay the key is still pressed, it goes to the next stage to
detect which row it belongs to; otherwise, it goes back into the loop to detect
a real key press.
ALP for 4X4 Keypad interfacing with 8051
3. To detect which key is pressed:
• 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 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 press belongs to.
• Upon finding the row that the key press belongs to, it sets up the starting
address for the LUT holding the scan codes (or the ASCII value) for that
row and goes to the next stage.
ALP for 4X4 Keypad interfacing with 8051
3. To detect which key is pressed:
ALP for 4X4 Keypad interfacing with 8051
4. To identify exact key:
• To identify the key press, the microcontroller rotates the column bit, one bit
at a time, into the carry flag and checks to see if it is low.
• Upon finding the zero, 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.
ALP for 4X4 Keypad interfacing with 8051
4. To identify exact key:
ALP for 4X4 Keypad interfacing with 8051
• There are IC chips such as National Semiconductor's MM74C923 that
incorporate keyboard scanning and decoding all in one chip.
• Such chips use counters and logic gates (not microcontrollers) to implement the
all 4 steps of previous procedure.
Flow
chart
for 4X4
Keypad
interfacing
with 8051
Example Problem
Identify the row and column of the pressed key for each of the following,
a) D3 – D0 = 1110 for the row, D3 – D0 = 1011 for the column
b) D3 – D0 = 1101 for the row, D3 – D0 = 0111 for the column.
From 4x4 key diagram, the row and column can be used to identify the key.
a) The row belongs to D0 and the column belongs to D2, therefore key
number 2 was pressed.
b) The row belongs to D1 and t column belongs to D3, therefore key
number 7 was pressed.
ALP for 4X4 Keypad interfacing with 8051
; Keyboard subroutine. 1/6
; This program sends the ASCII code ;for pressed key to P0.1
; P1.0-P1.3 connected to rows;
; P2.0-P2.3 connected to columns
MOV P2, #0FFH ;make P2 an input port
K1: MOV P1, #00H ;ground all rows at once
MOV A, P2 ;read all col. ensure all keys open
ANL A, #00001111B ;masked unused bits
CJNE A, #00001111B, K1 ;check till all keys released
ALP for 4X4 Keypad interfacing with 8051
K2: ACALL DELAY ;call 20 ms delay 2/6
MOV A, P2 ;see if any key is pressed
ANL A, #00001111B ;mask unused bits
CJNE A, #0000111lB, OVER ; key pressed, await closure
SJMP K2 ;check if key is pressed
OVER: ACALL DELAY ;wait 20 ms debounce time
MOV A, P2 ; Check key closure
ANL A, #00001111B ; mask unused bits
CJNE A, #00001111B, OVER1 ; key pressed, find row
SJMP K2 ; if none, keep polling
ALP for 4X4 Keypad interfacing with 8051
3/6
OVER1: MOV Pl, #11111110B ;ground row 0
MOV A, P2 ;read all columns
ANL A, #00001111B ;mask unused bits
CJNE A, #00001111B, ROW_0 ;key in row 0, find the col.
MOV P1, #11111101B ;ground row 1
MOV A, P2 ;read all columns
ANL A, #00001111B ;mask unused bits
CJNE A, #00001111B, ROW_1 ;key in row 1, find the col.
ALP for 4X4 Keypad interfacing with 8051
4/6
MOV Pl, #11111011B ;ground row 2
MOV A, P2 ;read all columns
ANL A, #00001111B ;mask unused bits
CJNE A, #00001111B, ROW_2 ;key in row 2, find the col.
MOV Pl, #11110111B ;ground row 3
MOV A, P2 ;read all columns
ANL A, #00001111B ;mask unused bits
CJNE A, #00001111B, ROW_3 ;key in row 3, find the col.
LJMP K2 ;if none, false input, repeat
ALP for 4X4 Keypad interfacing with 8051
ROW_0 MOV DPTR, #KCODE0 ;set DPTR=start of row 0 5/6
SJMP FIND ;find col. key belongs to
ROW_1 MOV DPTR, #KCODE1 ;set DPTR=start of row 1
SJMP FIND ;find col. key belongs to
ROW_2 MOV DPTR, #KCODE2 ;set DPTR=start of row 2
SJMP FIND ;find col. key belongs to
ROW_3 MOV DPTR, #KCODE3 ;set DPTR=start of row 3
FIND: RRC A ;see if any CY bit is low
JNC MATCH ;if zero, get the ASCII code
INC DPTR ;point to next col. Address
SJMP FIND ;keep searching
ALP for 4X4 Keypad interfacing with 8051
MATCH: CLR A ; set A=0 (match is found) 6/6
MOVC A, @A+DPTR ; get ASCII code from table
MOV P0, A ; display pressed key
LJMP K1
;ASCII LOOK-UP TABLE FOR EACH ROW
ORG 300H
KCODE0: DB ‘0’, ‘1’, ‘2’, ‘3’ ; ROW 0
KCODE1: DB ‘4’, ‘5’, ‘6’, ‘7’ ; ROW 1
KCODE2: DB ‘8’, ‘9’, ‘A’, ‘B’ ; ROW 2
KCODE3: DB ‘C’, ‘D’, ‘E’, ‘F’ ; ROW 3
END
Contact Bounce Problem
• Upon the mechanical switch changing its state, contacts would not be working
smoothly because of bouncing.
• Bouncing means the repeated contact and separation that occurs between
electrical contacts when a mechanical switch is turned ON or OFF.
Contact Bounce Problem
• This quick bouncing creates multiple on/off signals, which can confuse digital
circuits by treating them as several inputs instead of one signal.
• Switch debouncing is the process of removing electrical noise generated by a
mechanical switch while it pressed or released.
• Debounce circuits or software fix this bouncing by filtering out extra signals.
• Debouncing methods:
• Software Based
• Hardware Based
• With Flip-Flops
• With RC filters
• With Debouncing IC’s
Contact Bounce Problem
Software Switch Debouncing:
• In this method, the switch’s bouncing state effect is eliminated using various
algorithms and filters.
• The programmer can design an algorithm with use of shift register and
counters such that it will register the switch’s state after a delay.
• Another method is to use filter algorithms on the sampled input from the switch
and determine the state of switch based on the output of such digital filter.
• All this can make the software slightly inefficient, adding to delay in
performance if not implemented correctly.
Contact Bounce Problem
Hardware Debouncing:
Contact Bounce Problem
Hardware Debouncing:
• IC’s like MAX6816, MAX6817, MAX6818, MC 14490, Logi switch LS118
can be used for debouncing.
Summary
• I/O Interfacing of 8051
• LED
• Seven Segment Display
• LCD
• With Delay subroutine
• With monitoring of Busy Flag
• Keypad
• 4x4 keypad interfacing
• Debouncing problem