Interfacing with 8051
By
Dr. Supratim Gupta
Part-1: Interfacing Keys
Keyboard Configuration
Lead-per-Key
Matrix Keypad
Coded Keyboard
Keyboard Configuration: Lead-per-Key
Keyboard Configuration: Matrix
Keyboard Configuration: Coded Keyboard
Robust Keyboard Operations: Human Factors
To guard against bouncing of push button or accidental key press
To guard against multiple & simultaneous key press and release in arbitrary
sequence
To guard against key press and held for longer duration
To guard against rapid key press
Robust Keyboard Operations: Bouncing
Robust Keyboard Operations: Hardware Solution
Starting with the switch open
• The capacitor C1 will charge via R1 and D1
• In time, C1 will charge and Vb will reach within 0.7V
of Vcc.
• Therefore the output of the inverting Schmitt trigger
will be a logic 0.
Now close the switch
• The capacitor will discharge via R2.
• In time, C1 will discharge and Vb will reach 0V.
• Therefore the output of the inverting Schmitt trigger
will be a logic 1.
Robust Keyboard Operations: Hardware Solution
Schmitt Trigger
Output Voltage Vs. Input Voltage
Robust Keyboard Operations: Software Solution
De-bounce/Accidental Key hit: To wait until a time interval –longer than the
manufacturer’s specification –lapses during both key press & release.
Multiple Key press: To use only a valid key patterns –all other patterns will be
ignored; the first valid key patterns will be accepted
Key Hold: To identify valid key press if the same valid pattern exists after the
de-bounce delay
Rapid Key hit: Keys are scanned at a rate faster than human reaction time
The Algorithm for keyboard Interface
Step1: Check if any key is pressed else return to main routine
Step 2: Provide a time delay for de-bouncing (generally 20ms)
Step 3: Check if same switch is remained pressed else return to main routine
Step 4: Check if valid key pattern is pressed else return to main routine
Step 5: Act on the key pressed and return to main routine
Robust Keyboard Operations: Software Solution
Key scanning via polling: May sluggish, but usable for small keypad & less
number of multi-task environment
Key scanning via interrupt: CPU will scan keys when interrupted; Optimal for
large key board & larger number of multi-tasking environment
Robust Keyboard Operations: Embedded Solution
Circuit connection for Lead-per-key scan via interrupt
Robust Keyboard Operations: Embedded Solution
Circuit connection for Coded key scan via interrupt
Robust Keyboard Operations: Embedded Solution
Circuit connection for Coded key scan via timer interrupt
Part-2: ADC & DAC
ADC Interfacing: ADC0809
18
ADC Interfacing: Bipolar Signal
19
To ADC Channel
Pre-conditioning Circuit
DAC Interfacing: DAC0808
20
= + + …+ = + + …+
2 4 256 2 4 256
DAC Interfacing: DAC0808
21
= + + …+
2 4 256
= + + …+
2 4 256
= + + …+
2 4 256
DAC Interfacing: Bipolar output signal
22
I0
DAC0809
Post-conditioning of DAC output
Programming
23
Programming
24
Part-4: Interfacing Displays
Display Units
7-Segment LED Display
LCD Display
7-Segment Display
7-Segment Display: Single Unit
Common Anode/Cathode
Controlling the contrast
7-Segment Display: Multiple Units
The Algorithm for 7-Segment LED Display
Step1: Get the content to be displayed in Hex
Step 2: Convert the HEX number to BCD
Step 3: Use index addressing –Base address + index (the nibbles of BCD no.) –
to a look up table for fetching code for 7-Segment LEDs
Step 4: Send the code to Port where the unit(s) is (are) connected
LCD Interface with 8051
LCD Interface with Microcontroller
Basic Interface in Block Diagram
Basics on LCD Driver HD44780U
Multiplexed Instruction and Data paths
o 4-bit or nibble length
o 8-bit byte length
Memory and registers
o Two 8-bit register: Instruction Register (IR) and Data Register (DR)
o Character Generator RAM (CGRAM) of 64x8 bit
o Character Generator ROM (CGROM) 9920-bit for 240 character
o Display Data RAM (DDRAM) of 80x8bit
3 Signals flow control lines
o Select Register (RS): Selects instruction or data register
o Read/Write(R/W): 1 implies reading and 0 implies writing
o Enable (E): Provides clock to data flow
Power supply
o VDD: Overall power supply (5V)
o VSS: Ground connection (0V)
o VEE: Used to supply to LCD driver (VDD – VEE provides power to it)
Basics on LCD Driver HD44780U
Data/Instruction Flow Lines
No. of
Signal I/O Function
Lines
Selects registers.
0: Instruction register (for write) Busy flag:
RS 1 I
address counter (for read)
1: Data register (for write and read)
R/W 1 I Selects read or write. 0: Write 1: Read
E 1 I Starts data read/write.
Four high order bidirectional tristate data bus
pins. Used for data transfer and receive between
DB4 to DB7 4 I/O
the MPU and the HD44780U. DB7 can be used
as a busy flag.
Four low order bidirectional tristate data bus
pins. Used for data transfer and receive between
DB0 to DB3 4 I/O
the MPU and the HD44780U. These pins are not
used during 4-bit operation.
Register Selection
RS R/W Operation
0 0 IR write as an internal operation (display clear, etc.)
0 1 Read busy flag (DB7) and address counter (DB0 to DB6)
1 0 DR write as an internal operation (DR to DDRAM or CGRAM)
1 1 DR read as an internal operation (DDRAM or CGRAM to DR)
Instructions for HD44780U
Designate HD44780U functions, such as display format,
data length, etc.
Set internal RAM addresses
Perform data transfer with internal RAM
Perform miscellaneous functions
Instructions for LCD Driver
fOSC is
Instruction RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0 Description 250
kHz)
Clears entire display and sets
Clear
0 0 0 0 0 0 0 0 0 1 DDRAM address 0 in address
display
counter.
Sets DDRAM address 0 in
address counter. Also returns
Return
0 0 0 0 0 0 0 0 1 — display from being shifted to 1.52 ms
home
original position. DDRAM
contents remain unchanged.
Sets cursor move direction and
Entry mode specifies display shift. These
0 0 0 0 0 0 0 1 I/D S 37 m s
set operations are performed during
data write and read.
Display Sets entire display (D) on/off,
on/off 0 0 0 0 0 0 1 D C B cursor on/off (C), and blinking of 37 m s
control cursor position character (B).
Moves cursor and shifts display
Cursor or
0 0 0 0 0 1 S/C R/L — — without changing DDRAM 37 m s
display shift
contents.
Instructions for LCD Driver
fOSC is
Instruction RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0 Description 270
kHz)
Sets interface data length (DL),
Function set 0 0 0 0 1 DL N F — — number of display lines (N), and 37 m s
character font (F).
Set Sets CGRAM address. CGRAM
CGRAM 0 0 0 1 ACG ACG ACG ACG ACG ACG data is sent and received after 37 m s
address this setting.
Set Sets DDRAM address. DDRAM
DDRAM 0 0 1 ADD ADD ADD ADD ADD ADD ADD data is sent and received after 37 m s
address this setting.
Reads busy flag (BF) indicating
Read busy
internal operation is being
flag & 0 1 BF AC AC AC AC AC AC AC 0ms
performed and reads address
address
counter contents.
Instructions for LCD Driver
fOSC is
Instruction RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0 Description 270
kHz)
Write data 37 m s
Writes data into DDRAM or
to CG or 1 0 Write data tADD =
CGRAM.
DDRAM 4 m s*
Read data 37 m s
Reads data from DDRAM or
from CG or 1 1 Read data tADD =
CGRAM.
DDRAM 4 m s*
Instructions for LCD Driver
I/D = 1 Increment DDRAM: Display data RAM
= 0 Decrement CGRAM: Character generator RAM
S = 1 Accompanies display shift ACG: CGRAM address
ADD: DDRAM address
= 1 Display shift (corresponds to cursor address)
S/C Execution time changes
AC: Address counter used for both when frequency changes
=0 Cursor move DD and CGRAM addresses Example: When fcp or fosc
=1 Shift to the right DDRAM: Display data RAM is 250 kHz,
R/L 37 × 270
=0 Shift to the left CGRAM: Character generator RAM = 40
250
DL =1 8 bits, DL = 0: 4 bits ACG: CGRAM address
N =1 2 lines, N = 0: 1 line
F = 1 5 ´ 10 dots, F = 0: 5 ´ 8 dots
= 1 Internally operating
BF
= 0 Instructions acceptable
Character Generator ROM Access
The Algorithm for LCD Display
Step1: Send an instruction to configure different display features
Step 2: Poll the DB7 bit if it goes low from high
Step 3: Send next instruction or data and repeat the process for other data
Alternatively DB7 can be connected to external interrupt pin and Step 1 & three
can be placed in the interrupt service routine
Example of LCD Display
Statement:
Initialize LCD display with following features
1. 8-bit data path, 2 line display, and 5x8 Font
2. Display on, cursor on, and blinking on
3. Cursor move direction with increment mode, set display shift
off
After initialization send “Hi” in the display
The Algorithm for LCD Display
;*****************************************************************************
; This program demonstrates LM016L LCD interfacing with 8051 microcontroller
; Hardware description: Port 1 => DB0-DB7 of LCD
; P2.0 => RS of LCD
; P2.1 => Enable of LCD
; P2.2 => R/W of LCD
;*****************************************************************************
.org 0000h
sjmp main ;Jump to main program
;______________________________________________________________________________________
RS: .reg P2.0
Enb: .reg P2.1
RW: .reg P2.2
;______________________________________________________________________________________
main: ;Initialize part
;Run part
acall lcd_disp ;call function for LCD display
stop: sjmp stop ;Run in infinite loop
The Algorithm for LCD Display
lcd_disp: clr Enb
;__First instruction to set datapath length, display style and font_______
clr RS ;clear RS to send instruction
clr RW ;Clear RW for writing
mov P1,#38h ;send instruction (0 0 1 DL N F - -) for 8-bit datapath,
;2 line display, and 5x8 Font
setb Enb
clr Enb
lcall wait_lcd
;__Second instruction to set display on/off, cursor on/off, and blinkin of cursor on/off_______
clr RS ;clear RS to send instruction
clr RW ;Clear RW for writing
mov P1,#0Fh ;send instruction (0 0 0 0 1 D C B) for display on,
;cursor on, and blinking on
setb Enb
clr Enb
lcall wait_lcd
The Algorithm for LCD Display
;__Third instruction to set cursor move direction, set displayshift on/off_______
clr RS ;clear RS to send instruction
clr RW ;Clear RW for writing
mov P1,#06h ;send entrymode instruction (0 0 0 0 0 1 I/D S) for display on,
;cursor on, and blinking on
setb Enb
clr Enb
lcall wait_lcd
;__First data_____________________________________________________________________
setb RS ;set RS to send data
clr RW ;Clear RW for writing
mov P1,#'H' ;send ascii value of H,
setb Enb
clr Enb
lcall wait_lcd
;__Second data____________________________________________________________________
setb RS ;set RS to send data
clr RW ;Clear RW for writing
mov P1,#'i' ;send ascii value of i,
setb Enb
clr Enb
lcall wait_lcd
The Algorithm for LCD Display
;Special Character from CGRAM
;__Third data____________________________________________________________________
setb RS ;set RS to send data
clr RW ;Clear RW for writing
mov P1,#3Bh ;send ascii value of semicolon obtained from character code and character pattern table of datasheet,
setb Enb
clr Enb
lcall wait_lcd
ret
wait_lcd: push ACC
wait_lcdin: clr Enb
clr RS ;clear RS to send instruction
setb RW ;set RW for reading
setb Enb ;Clock out command from
mov A,P1
jb ACC.7,wait_lcdin ;if bit 7 high, LCD still busy
clr Enb ;finish the command
clr RW ;Turn-off RW for future commands
pop ACC
ret
Part-5: Device Communication
Device Communication Modes
Parallel communication implies sending a whole byte (or more) of data over
multiple parallel wires
Serial communication implies sending data bit by bit over a single wire
Asynchronous: Universal asynchronous receiver/transmitter (UART Standard)
Synchronous: Serial peripheral interface (SPI Standard)
Asynchronous Serial Communication
With asynchronous communication, the transmitter and receiver do not
share a common clock
Add: Start, Stop, Parity Bits Remove: Start, Stop, Parity Bits
Transmitter + – Receiver
Data
1 byte-wide Data 1 byte-wide Data
The Transmitter The Receiver
Shifts the parallel data onto the Extracts the data using its own clock
serial line using its own clock
Converts the serial data back to the
Also adds the start, stop and parallel form after stripping off the
parity check bits start, stop and parity bits
Asynchronous Serial Communication
Start Bit Parity Bit 1 or 2 Stop Bits
D0 D1 D2 D3 D4 D5 D6 D7
1 Asynchronous Byte
Baud rate—the bit rate of the serial port
Throughput—actual data transmitted per sec (total bits transmitted—overhead)
Example: 115200 baud = 115200 bits/sec
If using 8-bit data, 1 start, 1 stop, and no parity bits, the effective
throughput is: 115200 * 8 / 10 = 92160 bits/sec
Serial Communication: Configuring 8051
Serial Communication: Configuring 8051
Serial Communication: Configuring 8051
Serial Communication: Configuring 8051
Timer 2 in Baud Rate Generation Mode
Serial Communication: Configuring 8051
Serial Communication: Programming 8051
Serial Communication between 8051 & PC
Serial Communication: Max232
Example of LCD Display
Statement:
Send any data from PC to LCD display using serial
communication
Example of LCD Display
Example of LCD Display
Example of LCD Display
Example of LCD Display
Example of LCD Display
Example of LCD Display
Example of LCD Display
Example of LCD Display with Temperature sensor