Escolar Documentos
Profissional Documentos
Cultura Documentos
on
8051 Microcontroller and Applications
September 1st- 2nd, 2008
DAY 2 SESSION 2
LCD Interfacing
Display Data on 2x16 character LCD
SYM
Vss
Vcc
Vee
RS
R/W
DESCRIPTION
Ground
Supply , +5v
Supply to control contrast
RS=0 to select Command REG
RS=1 to select DATA REG
R/W = 1 to Read from LCD
R/W = 0 to Write to LCD
6
EN
Enable Pin
7-14 D0-D7 8 bit DATA Bus
LCD Module
D0
Vcc
Vee
P2.7
D7
10 K pot
Vss
RS RW EN
P3.5
P3.6
P3.7
Code
01h
02h
06h
0Ch
0Fh
C0h
38h
Command
Clear Display screen
Return Home
Increment Cursor after write
Display on Cursor Off
Display on Cursor blinking
Force cursor to beginning of 2nd line
2 lines 5X7 font (initialization code)
RS EQU P3.5
RW EQU P3.6
EN EQU P3.7
PDATA EQU P2
WRTCMD:MOV PDATA,A
CLR RS
CLR RW
SETB EN
CLR EN ;Hi to Lo pulse
RET
The command to be sent is in A reg
RS EQU P3.5
RW EQU P3.6
EN EQU P3.7
PDATA EQU P2
WRTDAT:
MOV PDATA,A
SETB RS
CLR RW
SETB EN
CLR EN ;Hi to Lo pulse
RET
Bit
Bit7=0
7=0
YES
P3.5
P3.6
P3.7
P2
EQU RS
EQU RW
EQU EN
EQU PDATA
LCD
LCDINITIALIZATION
INITIALIZATION::
To
Toinitialize
initializethe
theLCD
LCDthe
thefollowing
followingcommands
commandshave
haveto
to
Be
Beissued,once
issued,oncethis
thisdone
doneyou
youready
readyto
tosend
senddata
data
start
start
Configure LCD for
8 bit data , 2 lines
5X7 font
Code =38h
LCD
LCDOn
On, ,Cursor
Cursor
Blinking
Blinkingcode=0Fh
code=0Fh
Clear
ClearLCD
LCDcode=01h
code=01h
Auto
Autoincrement
incrementcursor
cursor
code=06h
code=06h
81
82
C0
C1 C2
8E
8F
CE CF
KEYPAD INTERFACING
Structure of 4X4 Matrix keyboard
R1
R2
R3
R4
C1
C2
C3
C4
KEYPAD INTERFACING
Keypad Connector Structure ( LAB Keypad)
8 pin connector at the bottom of the Key Pad
C1 C2 C3 C4
R4 R3 R2 R1
C1 C2 C3 C4
P1.0
R4 R3 R2 R1
P1.4
P1.1
P1.2
P1.3
P1.5
P1.6
P1.7
KEYPAD INTERFACING
Equivalent Interfacing Diagram:
P1.7(R1)
P1.6(R2)
P1.5(R3)
P1.4(R4)
P1.0
(C1)
P1.1
(C2)
P1.2 P1.3
(C3) (C4)
KEYPAD INTERFACING
Important Steps in Reading Keypad
Step1: See if any key is pressed , so keep scanning the key pad
till a key is presses
Step2: Once we have detected a key press we have to decode
the key i.e we have to identify which key is pressed
Step3: We have to wait till the pressed key is released before
We go on to process the next key
KEYPAD INTERFACING
Step1: Detecting Key Press
P1.7(R1)
P1.6(R2)
P1.5(R3)
P1.4(R4)
P1.0
(C1)
P1.1
(C2)
P1.2 P1.3
(C3) (C4)
Configure
P1.0 P1.3(Columns) : Outputs and P1.4 P1.7(Rows) : Inputs
Vcc
TB2
Vcc
Write to latch
Clk
P1.X
pin
P1.X
Q
M1
TB1
Read pin
8051 IC
output 1
Vcc
TB2
ground
Write to latch
Clk
P1.X
pin
P1.X
Q
M1
TB1
Read pin
8051 IC
output 0
TB2
2. MOV A,P1
Vcc
external pin=High
Load(L1)
P1.X
Write to latch
Clk
M1
TB1
Read pin
3. Read pin=1 Read latch=0
Write to latch=1
8051 IC
P1.X pin
Vcc
2. MOV A,P1
TB2
Load(L1)
external pin=Low
MOV P1,#0FFH
Internal CPU bus
P1.X
Write to latch
Clk
M1
TB1
Read pin
3. Read pin=1 Read latch=0
Write to latch=1
8051 IC
P1.X pin
KEYPAD INTERFACING
Keypad when connected to the port
P1.7(R1)
P1.6(R2)
P1.5(R3)
P1.4(R4)
Vcc
P1.0
(C1)
P1.2
(C2)
P1.3
(C3)
P1.4
(C4)
KEYPAD INTERFACING
KEYPAD INTERFACING
C1
START
START
Make
Makeall
allcolumns
columns
Low
Low
NO
Key
KeyPress
Press
YES
Continue
Continue
EQU P1.0
R1
EQU P1.7
C2
EQU P1.1
R2
EQU P1.6
C3
EQU P1.2
R3
EQU P1.5
C4
EQU P1.3
R4
EQU P1.4
PORT
EQU P1
ANL A,#0F0H
If there is no key press the higher nibble will be all ones (F)
KEYPAD INTERFACING
KEY DEBOUNCE
Some times key press detected in the previous step could be
due to noise or transients so in order to be sure that it is key
press we need to do key debounce
Key debounce involves giving a small delay of 20 ms and then
checking again for a key press, If we find a key press the
second time also, we can be assured that we have a valid key
press, else we have to again go back to key scan mode
KEYPAD INTERFACING
Key Check Routine with debounce
KEYCHK: MOV PORT,#0F0H
MOV A,PORT
ANL A,#0F0H
HERE:
ACALL DELAY
; debounce
MOV A,PORT
ANL A,#0F0H
CJNE A,#0F0H,KEYFIND ; If valid key, jump to decode the key
SJMP KEYCHK
KEYPAD INTERFACING
Step2: Key Decode , to decode the key we will use a look up
Table approach so let us look at generation of look up table
1
10
P1.7(R1)
P1.6(R2)
16
P1.5(R3)
P1.4(R4)
P1.0
(C1)
P1.1
(C2)
P1.2 P1.3
(C3) (C4)
So if we send the code FE h on port 1 and on reading it back and if we get 70 h we can
conclude that key 1 is pressed, you can assign any code to key 1 like A , decimal 1 ,
Carriage return (CR)
KEYPAD INTERFACING
1
Pin5:
Input : 1111 1110 Output:1011 0000
P1.7(R1)
P1.6(R2)
10
Pin2,6,10:
P1.5(R3) Input : 1111 1101 Output: 0111 0000
So input FD h , output 70h , key= 2
P1.4(R4)
P1.0
(C1)
P1.2
(C2)
P1.3 P1.4
(C3) (C4)
KEYPAD INTERFACING
Decoding Algorithm :
KEYPAD INTERFACING
The code for the above is as follows
KEYFND: MOV
DPTR, #KEYCODE
; initialize acc to 0
NOP
NOP
MOV A,PORT
ANL A,#0F0H
MOV R1,A
MOV A,#00H
MOVC A,@A+DPTR ; LOAD THE EXPECTED NO. IN A
CJNE A,01,NEXT
INC DPTR
CLR A
MOVC A,@A+DPTR
MOV R3,A
SJMP KEYREL
KEY RELEASE
MOV A,PORT
ANL A,#0F0H
SERIAL COMMUNICATION
9
DB 9 Connector
PIN
1
2
3
4
5
6
7
8
9
Description
Data carrier detect
Receive Data (RxD)
Transmit Data (TxD)
Data terminal ready
Signal Ground (GND)
Data set ready
Request to send
Clear to send
Ring Indicator
CLASSIFICATION
DTE: Data terminal equipment ,like computers that send and
receive data
DCE: Data communication equipment like modems that transfer
data
To connect two DTE equipment we use what is called as the null
modem connection , we will be using this scheme to connect our
controller to PC
DTE
DTE
TxD
TxD
RxD
GND
RxD
GND
SERIAL COMMUNICATION
8051 and RS 232
MAX 232
11
T1IN
T1out
12
R1out
R1IN
T2IN
T2out
R2out
R2IN
10
9
TTL SIDE
14
13
7
8
RS 232 SIDE
SERIAL COMMUNICATION
8051 uses two pins of port 3 for serial communication
P3.0 = Rxd
P3.1 = TxD
As they are TTL compatible they need to be connected to a line
driver
8051
MAX 232
P3.1(TxD)
11
11
P3.0(RxD)
10
12
DB 9
14
13
SERIAL COMMUNICATION
SERIAL COMMUNICATION
/12
/12
28800 Hz
/32
/32
To timer 1
By
UART
By UART to set Baud Rate
SERIAL COMMUNICATION
Timer Mode 2:
XTAL
XTAL
/12
/12
C/T=0
TL
TF
Reload
TRx
TH
SERIAL COMMUNICATION
Count Values : If we compute the count values to be loaded into
TH to generate the standard baud rates we would get the following
TH1(DEC)
9600
FD
-3
4800
FA
-6
2400
F4
-12
SBUF register : 8 bit Used for serial comm of 8051 You place a byte in sbuff prior
to sending Byte is placed in sbuff when received from out side
MOV
MOV
MOV
SBUFF , #D
sbuff,A
A,SBUFF
SCON
SCONregister
register: :88bit
bitUsed
Usedfor
forprogramming
programmingthe
thestart
startand
and
Stop
Stopbit
bitand
anddata
databits
bitsofofdata
dataframing
framing
SM0
SM0
SM1
SM1
SM2
SM2
REN
REN
TB8
TB8
RB8
RB8
TITI
RI
RI
SM0
SM0
SM1
SM1
SM2
SM2
REN
REN
TB8
TB8
RB8
RB8
TiTi
RiRi
SCON.7
SCON.7 Serial
Serialmode
modespecifier
specifier
SCON.6
SCON.6 Serial
Serialport
portmode
modespecifier
specifier
SCON.5
SCON.5 Used
Usedfor
formultiprocessor
multiprocessorcommunication
communication
SCON.4
SCON.4 Set/Cleared
Set/Clearedby
bysoft
software
waretotoenable/disable
enable/disablereception
reception
SCON.3
SCON.3 not
notwidely
widelyused
used
SCON.2
SCON.2 not
notwidely
widelyused
used
SCON.1
SCON.1 Transmit
Transmitinterrupt
interruptFlag.
Flag.Set
Setby
byhardware
hardwareatatthe
the
beginning
beginningofofthe
thestop
stopbit
bitininmode
mode11Must
Mustbe
becleared
cleared
by
bysoftware
software
SCON.0
SCON.0 Receive
Receiveinterrupt
interruptflag
flagset
setby
byhardware
hardwarehalf
halfway
way
thru
thruthe
thestop
stopbit
bittime
timeininmode
mode11
THANK YOU
END OF SESSION 2