Você está na página 1de 56

Training Program

on
8051 Microcontroller and Applications
September 1st- 2nd, 2008
DAY 2 SESSION 2

INTERFACING 8051 WITH PHERIPHERALS


1. LED
2. LCD
3. KEYBOARD
4. 7 SEGMENT
5. SERIAL PORT
6. ADC, STEPPER MOTOR,DC MOTOR etc..

Interfacing LED with 8051


Light Emitting Diodes (LED) are the most commonly used components,
usually for displaying Pins Digital State.

Interfacing 7 segment display

They are composed of 8 LEDs, 7 segments are arranged as a rectangle


for symbol displaying and there is additional segment for decimal point
displaying.

Problem - valuable I/O pins which they occupy.


Ex: Two 6-digit numbers - 96 output pins are needed
The solution on this problem is called MULTIPLEXING

A microcontrollers port is connected to display in a way


that bit 0 activates segment a, bit 1 activates segment b,
bit 2 segment c etc., the table below shows mask for
each digit

LCD Interfacing
Display Data on 2x16 character LCD

LCD INTERFACING Contd


LCDs are typically used for displaying outputs from a
Micro controller or for debugging purposes.
LCDs come in varying sizes some of the typical ones
are
16 X 1= one line 16 characters per line
16 X 2= Two lines 16 characters per line
20 X 4= Four lines 20 characters per line
To display on LCD characters should be sent in
ASCII format

LCD INTERFACING Contd


Pin
1
2
3
4

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 INTERFACING Contd


LCD power supply

LCD INTERFACING Contd


There are three important control pins for the LCD
1. RS , Register Select : There are two registers inside LCD one
is the command register and the second is Data register.
2. RS is used to select between the two
RS =0 Command Register
RS =1 Data Register
Typical Commands include Clear Display, Set cursor etc.These
commands should be sent to command regs. The Data which
is to displayed should be sent to the data reg

LCD INTERFACING Contd

2. R/W Pin: R/W =1 Read information from LCD


R/W =0 Write information to LCD
3. EN : Enable pin, used by LCD to latch information
presented on data pins. When data/command is supplied to
the LCD a HI to LO pulse must pulse must be applied to this
pin in order for the LCD to latch the data/command present
on the pins
IMP: The pulse must be a minimum 450 nsec wide

LCD INTERFACING Contd


Connection Diagram
8051
P2.0

LCD Module
D0

Vcc
Vee

P2.7

D7

10 K pot

Vss
RS RW EN

P3.5
P3.6
P3.7

In addition to the above pins there is a back light pin which if


Connected to + 5v will provide back light

LCD INTERFACING Contd


Sending Commands to LCD
The typical commands sent to the LCD are as follows

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)

LCD INTERFACING Contd


Algorithm
start
start
Load
LoadData
DatatotoPort
Port
Connected
Connected
totoLCD
LCDDATA
DATAbus
bus
Clear
ClearRS
RS; ;command
command
Clear
ClearRW
RW; ;Write
Write
Hi
HiTo
ToLo
Lopulse
pulseon
onEN
EN
stop
stop

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

LCD INTERFACING Contd


Sending DATA to LCD
start
start
Load
LoadData
DatatotoPort
Port
Connected
Connected
totoLCD
LCDDATA
DATAbus
bus
SET
SETRS
RS; ;DATA
DATA
Clear
ClearRW
RW; ;Write
Write
Hi
HiTo
ToLo
Lopulse
pulseon
onEN
EN
stop
stop

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

The data to be sent is in A reg

LCD INTERFACING Contd


Busy Waiting
The LCD takes some time to execute the commands and to
display.
The data sent to it and while it is doing execution we are not
Suppose to send new commands or data to it
To send data there are two Ways
1. Giving some predefined delay say 30msec between two
commands or data, not a good method as different LCD
modules take different times to execute
2. Busy Wait Flag : Bit 7 of the command reg is the busy wait
flag. We can use it to check if the LCD is busy
D7 =1 ; Busy
D7 =0 ; Free to process next instruction

LCD INTERFACING Contd


start
start
Make
Makebit
bit77ofofport
port
Input
Inputpin
pin
Clear
ClearRS
RS; ;Command
Command
Set
SetRW
RW ; ; Read
Read
Hi
HitotoLo
Lo pulse
pulseon
onEn
En
NO

Bit
Bit7=0
7=0

YES

P3.5
P3.6
P3.7
P2

EQU RS
EQU RW
EQU EN
EQU PDATA

BSYWT: SETB PDATA.7


CLR RS
SET RW
LOOP: SETB EN
CLR EN
JB pdata.7, LOOP
RET
stop
stop

LCD INTERFACING Contd


So before issuing any command or data to the LCD, we have
to check if LCD is free or not. This can be done using the BSYWT
Routine as shown in the previous slide, so for correct operations
Our WRTCMD and WRTDAT routines should be modified as
Follows
WRTCMD:LCALL BSYWT
MOV PDATA,A
CLR RS
CLR RW
SETB EN
CLR EN
RET

WRTDAT: LCALL BSYWT


MOV PDATA,A
SETB RS
CLR RW
SETB EN
CLR EN
RET

The command/data to be sent is in A reg

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

LCDINIT: MOV A,#38H


LCALL WRTCMD
MOV A,#0FH
LCALL WRTCMD
MOV A,#01H
LCALL WRTCMD
MOV A,#06H
LCALL WRTCMD
RET
stop
stop

LCD INTERFACING Contd


EX1: Write an ALP to Print HELLO on the LCD
Start: LCALL LCDINIT
MOV A,#H
LCALL WRTDAT
MOV A,#E
LACLL WRTDAT
MOV A,#L
LCALL WRTDAT
MOV A,#L
LCALL WRTDAT
MOV A,#O
LCALL WRTDAT
HERE: SJMP HERE

LCD INTERFACING Contd


Putting data at any Location
Scope is there to put data at any location of our choice, to
Accomplish this we need to place the cursor at the desired
Location
Address locations in a 16X2 LCD
80h

81

82

C0

C1 C2

8E

8F

CE CF

So to place the cursor at any location we need to issue a


command with address as the parameter
Ex: To place the the cursor at line 2 at pos 4
MOV A,#0C3H
LCALL WRTCMD

KEYPAD INTERFACING
Structure of 4X4 Matrix keyboard

R1
R2
R3
R4
C1

C2

C3

C4

Contains 4 Rows and 4 Columns : whenever a key is pressed


The corresponding rows and columns are shorted

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

Interfacing Keypad to 8051:We will use one port P1 for interfacing

C1 C2 C3 C4

P1.0

R4 R3 R2 R1

P1.4

P1.1
P1.2
P1.3

P1.5
P1.6
P1.7

The lower nible


Of Port1 i.e. P1.0
To P1.3 are
Connected to the
4 columns
The upper nible
Is connected to the
4 Rows note that
Row 1 is connected
To 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

Writing 1 to Output Pin P1.X


Read latch

Vcc

TB2

Load(L1) 2. output pin is

Vcc

1. write a 1 to the pin


Internal CPU
bus

Write to latch

Clk

P1.X
pin

P1.X
Q

M1

TB1
Read pin

8051 IC

output 1

Writing 0 to Output Pin P1.X


Read latch

Vcc

TB2

Load(L1) 2. output pin is

ground

1. write a 0 to the pin


Internal CPU
bus

Write to latch

Clk

P1.X
pin

P1.X
Q

M1

TB1
Read pin

8051 IC

output 0

Reading High at Input Pin


Read latch
1.

TB2

write a 1 to the pin MOV


P1,#0FFH
Internal CPU bus

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

Reading Low at Input Pin


Read latch
1.

Vcc

2. MOV A,P1

TB2

write a 1 to the pin

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

1. Ground all the Columns and read the rows


2. If no key is pressed you will read all rows as high(??)
Remember the port structure P1 has internal pull up
resistors
3. If some key is pressed then one of the rows will
indicate a zero

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

KEYCHK: MOV PORT,#0F0H ;make all column low


MOV A,PORT

; read from the ports

ANL A,#0F0H

; mask the lower nibble

CJNE A,#0F0H,HERE ; check if there is key press


SJMP KEYCHK

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

; make all the columns LOW

MOV A,PORT

; read from the ports

ANL A,#0F0H

; mask the lower nibble

CJNE A,#0F0H,HERE ; check if there is key press


SJMP KEYCHK

HERE:

ACALL DELAY

; debounce

MOV A,PORT

; read port again to make sure it was not noise

ANL A,#0F0H
CJNE A,#0F0H,KEYFIND ; If valid key, jump to decode the key

SJMP KEYCHK

; If invalid go back to key scan mode

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)

Suppose we send the code


1111 1110 on port 1
And suppose key 1 is pressed
Then if you read back P1 and
Mask the lower bits what will you
Get

P1.4(R4)

P1.0
(C1)

P1.1
(C2)

P1.2 P1.3
(C3) (C4)

P1= 0111 0000 = 70h

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

So input FE h , output B0h , key= 5

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)

Input : 1111 1101 Output:1011 000


So input FD h , Output: B0h key= 6
Input : 1111 1101 Output:1101 000
So input FD h , Output: D0h key= 10

So in our look up table we will store the code , followed by


Expected output ,followed by key code , suppose we want to
Create a look up table with key codes being ASCII 0 to 9 with
Key 1 being 1 and Key 10 being 0 , it would appear as below
KEYCODE: DB 0FEH,70H,'1'
DB 0FEH,0B0H,'5'
DB 0FEH,0D0H,'9'
DB 0FDH,70H,'2'
DB 0FDH,0B0H,'6'
DB 0FDH,0D0H,'0'
DB 0FBH,70H,'3'
DB 0FBH,0B0H,'7'
DB 0F7H,70H,'4'
DB 0F7H,0B0H,'8'

After creating the look up table


Store it in On chip Rom .

KEYPAD INTERFACING
Decoding Algorithm :

Step1: Initialize pointer to Look Up Table


Step2: Output code ,in pointer and read input
Step3: Input == Expected Input Key found exit loop
Step4: Inc pointer to point to next code location and go to
Step 2

KEYPAD INTERFACING
The code for the above is as follows

KEYFND: MOV

DPTR, #KEYCODE

keyfnd1: MOV A,#00H

; initialize acc to 0

MOVC A,@A+DPTR ; get the first code into acc


INC DPTR
MOV PORT,A

; inc DPTR to point to expected output


; PUT KEYCODE INTO PORT1

NOP
NOP
MOV A,PORT

; READ FROM THE PORT

ANL A,#0F0H

; MASK THE LOWER NIBBLE

MOV R1,A

; STORE ACC. IN REG. R1

MOV A,#00H
MOVC A,@A+DPTR ; LOAD THE EXPECTED NO. IN A

CJNE A,01,NEXT
INC DPTR

;IF KEY FOUND POINT TO THE CODE

CLR A
MOVC A,@A+DPTR
MOV R3,A

;STORE RESULT IN REG R3

SJMP KEYREL

NEXT: INC DPTR


INC DPTR
SJMP KEYFND 1

KEY RELEASE

KEYREL: MOV PORT,#0F0H

; make all the columns LOW

MOV A,PORT

; read from the ports

ANL A,#0F0H

; mask the lower nibble

CJNE A,#0F0H,KEYREL ; check if there is key press


MOV A,R3
LCALL WRTDAT
SJMP KEYCHK

SERIAL COMMUNICATION

9
DB 9 Connector

For most of our applications


only PIN 2,3 and 5 are
required

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

NULL MODEM CONNECTION

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

8051 is a TTL based chip


where as the RS 232 standard
Is not TTL compatible so we
Require line drivers like the
MAX 232 chips which convert
TTL logic to RS 232
compatible signals and also
vice versa. Each MAX 232 has
two sets of line drivers as
shown in the Fig

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

Programming the Serial Interface:


Programming involves three Registers
1. TMOD Register : Timer 1 in mode 2 i.e. auto reload mode
is used to generate the baud rate for serial data transfer
2. SBUF Register : This the register where data ,to be sent ,
received is stored
3. SCON Register : Serial Control register used to configure
the various modes also has flags to test whether transfer
is compete etc.

SERIAL COMMUNICATION

BAUD RATE : The BAUD rate for serial communication is


programmable . Basic clock for BAUD rate is derived as follows
11.0592 MHz
XTAL
XTAL
Oscillator
Oscillator

/12
/12

28800 Hz
/32
/32
To timer 1
By
UART
By UART to set Baud Rate

Typical BAUD Rates can be derived from the above as shown


28 800/24 = 1200
28 800/12 = 2400
28 800/6 = 4800
28 800/3 = 9600

SERIAL COMMUNICATION
Timer Mode 2:

XTAL
XTAL

/12
/12
C/T=0

TL

TF
Reload

TRx

TH

1. 8 bit timer so can count from 00 to FF h only


2. Count is loaded in TH and when timer is started count value Is loaded into TL
automatically.
3. When TL rolls over from FF to 00 the TF flag is set and also the count stored in TH
is reloaded into TL, so just by clearing The TF flag the whole process can be
repeated again, no need To reload as in mode 1.

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

BAUD RATE TH1(HEX)

TH1(DEC)

9600

FD

-3

4800

FA

-6

2400

F4

-12

So to set a 9600 Baud rate the following statement would be reqd


MOV TMOD ,# 20h ; Timer 1 mode 2
MOV TH1,#FDh or MOV TH1,# -3

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

; Load sbuff=44h , ascii for D


;copy acc into sbuf
;copy SBUF into accum

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

START: MOV TMOD,#20h


MOV TH1, #-3
MOV SCON,#50h
SETB TR1
AGAIN: MOV SBUF,#A
HERE: JNB TI,HERE
CLR TI
SJMP AGAIN
MOV TMOD ,#20h
MOV TH1,#-3
MOV SCON ,#50h
SETB TR1
HERE: JNB Ri, HERE
MOV A, SBUF
MOV P2,A
CLR RI
SJMP HERE

THANK YOU
END OF SESSION 2

Você também pode gostar