0% found this document useful (0 votes)
37 views55 pages

MP MC Module-5

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

MP MC Module-5

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

BECE204L – MICROPROCESSORS AND

MICROCONTROLLERS

MODULE-5
I/O interfacing with
Microcontroller 8051

MODULE-5 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 1


MODULE-5

• LCD
• LED
I/O • Keypad
interfaci • Analog-to-Digital Convertors,
ng with • Digital-to-Analog Convertors
Microco •
ntroller Sensor with Signal Conditioning
8051 Interface.

MODULE-5 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 2


LCD

MODULE-5 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 3


LIQUID CRYSTAL DISPLAY (LCD)
 Display units are the most important output devices in many
electronics products and LCD is one of the most used display unit in
many applications.

 LCD is composed of liquid crystal particles which do not emit light on


their own instead they are illuminated by a backlight hence they need
an external light source to work.

 When light from a backlight source is emitted and allowed to fall on the
vertical polarizer. Then the unpolarized light by the source gets
vertically polarized.

 When initially no external potential is provided between the two


electrodes, the molecules of the liquid crystal remain twisted.
MODULE-5 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 4
LIQUID CRYSTAL DISPLAY (LCD)

 When an electric current is


applied to them, they tend to
untwist and causes a change in
the light angle passing through
them.

 Further this causes a change in


the angle of the top polarizing
filter with respect to it.

 So little light is allowed to pass


through that particular area of
LCD.

 Thus that area becomes darker


MODULE-5 comparing to others.
BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 5
LIQUID CRYSTAL DISPLAY (LCD)

16x2
LCD
MODULE-5 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 6
LIQUID CRYSTAL DISPLAY (LCD)
 In 16x2 LCD, 2 represents number of lines and 16 represents number of
characters displayed in each line. It supports all the ASCII characters and
provides the provision to display the custom characters by creating the
pattern. Each character in LCD is displayed in a matrix of 5x7 pixels.

MODULE-5 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 7


LIQUID CRYSTAL DISPLAY (LCD)
 The 16X2 LCD has two built in registers namely
data register and command register.

 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 the screen, setting the
cursor position, controlling display etc.

 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.

 For programming LCD follow these steps:


STEP1: Initialization of LCD.
STEP2: Sending command to LCD.
MODULE-5 STEP3: Writing the data– MICROPROCESSORS
BECE204L to LCD. AND MICROCONTROLLERS 8
LIQUID CRYSTAL DISPLAY (LCD)
Step1: LCD initialization (common for almost all applications)
1. Send 38H to the 8 bit data line for initialization
2. Send 0FH for making LCD ON, cursor ON and cursor blinking ON.
3. Send 06H for incrementing cursor position.
4. Send 01H for clearing the display and return the cursor.
Step2: Sending command to LCD
5. Send the command data to command register
6. Make R/W low.
7. Make RS=0 if data byte is a command
8. Pulse E from high to low with some delay.
9. Repeat above steps for sending another command.
Step3: Writing the data to LCD
10. Place data byte on the data register.
11. Make R/W low.
12. make RS=1 if the data byte is a data to be displayed.
13. Pulse E from high to low with some delay.
14. Repeat above steps for sending another data.
MODULE-5 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 9
LIQUID CRYSTAL DISPLAY (LCD)
EXAMPLE-1
Write an 8051 assembly language program to display the message “VIT” on LCD
display. Assume following,
• calls a time delay before sending next data/command
• P1.0-P1.7 are connected to LCD data pins D0-D7
• P2.0 is connected to RS pin of LCD
• P2.1 is connected to R/W pin of LCD
• P2.2 is connected to E pin of LCD

ORG 0000H
MOV A, #38H ; INITIALIZE 2x16 LCD
ACALL COMNWRT ; call command
subroutine
ACALL DELAY ; give LCD some time
MOV A, #0EH ; display on, cursor on
ACALL COMNWRT ; call command
subroutine
MODULE-5 ACALL DELAY ; give LCD some time
BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 10
LIQUID CRYSTAL DISPLAY (LCD)
MOV A, #01 ; clear LCD
ACALL COMNWRT ; call command
subroutine
ACALL DELAY ; give LCD some time
MOV A, #06H ; shift cursor right
ACALL COMNWRT ; call command
subroutine
ACALL DELAY ; give LCD some time
MOV A, #84H ; cursor at line 1, pos. 4
ACALL COMNWRT ; call command
subroutine
ACALL DELAY ; give LCD some time
MOV A, #’V’ ; display letter N
ACALL DATAWRT ; call display
subroutine
ACALL DELAY ; give LCD some time
MOV A, #’I’ ; display letter O
ACALL DATAWRT ; call display
subroutine
ACALL DELAY ; give LCD some time
MODULE-5 MOV A, –#’T’
BECE204L MICROPROCESSORS ;AND
display letter O
MICROCONTROLLERS 11
LIQUID CRYSTAL DISPLAY (LCD)
COMNWRT: MOV P1, A ; send command to LCD by
coping reg A to port 1
CLR P2.0 ; RS=0 for command
CLR P2.1 ; R/W=0 for write
SETB P2.2 ; E=1 for high pulse
ACALL DELAY ; give LCD some time
CLR P2.2 ; E=0 for H-to-L pulse
RET
DATAWRT: MOV P1, A ; write data to LCD by coping reg
A to port 1
SETB P2.0 ; RS=1 for data
CLR P2.1 ; R/W=0 for write
SETB P2.2 ; E=1 for high pulse
ACALL DELAY ; give LCD some time
CLR P2.2 ; E=0 for H-to-L pulse
RET
DELAY: MOV R3, #50 ; 50 or higher for fast CPUs
HERE2: MOV R4, #255 ; R4 = 255
HERE: DJNZ R4, HERE ; stay until R4 becomes 0
DJNZ R3, HERE2 ; stay until R3 becomes 0
MODULE-5 RETBECE204L – MICROPROCESSORS AND MICROCONTROLLERS 12
LIQUID CRYSTAL DISPLAY (LCD)
EXAMPLE-2
Write an 8051 assembly language program to display the message “HELLO” on
LCD display using DPTR. Assume ; P1.0-P1.7=D0-D7, P2.0=RS, P2.1=R/W,
P2.2=E.
ORG 0000H
MOV DPTR, #MYCOM
C1: CLR A
MOVC A,@A+DPTR
ACALL COMNWRT
ACALL DELAY
INC DPTR
JZ SEND_DAT
SJMP C1

Exercise: Write an 8051 assembly language program to display “Your Reg. No” on first line of
the LCD and “Your name” on the second line of the LCD using DPTR.

MODULE-5 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 13


LIQUID CRYSTAL DISPLAY (LCD)
SEND_DAT: MOV DPTR, #MYDATA
D1: CLR A
MOVC A,@A+DPTR
ACALL DATAWRT
ACALL DELAY
INC DPTR
JZ AGAIN
SJMP D1
AGAIN: SJMP AGAIN
COMNWRT: MOV P2, A ; send command to LCD by coping A
to P1
CLR P3.7 ; RS=0 for command
CLR P3.6 ; R/W=0 for write
SETB P3.5 ; E=1 for high pulse
ACALL DELAY ; give LCD some time
CLR P2.2 ; E=0 for H-to-L pulse
RET
MODULE-5 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 14
LIQUID CRYSTAL DISPLAY (LCD)
DATAWRT: MOV P2, A ; write data to LCD by
coping A into P1
SETB P3.7 ; RS=1 for data
CLR P3.6 ; R/W=0 for write
SETB P3.5 ; E=1 for high pulse
ACALL DELAY ; give LCD some time
CLR P3.5 ; E=0 for H-to-L pulse
RET
DELAY: MOV R3, #250 ; 50 or higher for fast CPUs
HERE2: MOV R4, #255 ; R4 = 255
HERE: DJNZ R4, HERE ; stay until R4 becomes 0
DJNZ R3, HERE2
RET
ORG 300H
MYCOM: DB 38H, 0EH, 01, 06, 84H, 0 ; commands and
null
MYDATA: DB “HELLO”, 0
MODULE-5 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 15
KEYPAD

16

MODULE-5 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 16


KEYPAD
 Keyboards are organized in a matrix of rows and columns
 A 4x4 matrix connected to two ports - rows are connected to an
output port and the columns are connected to an input port
 When a key is pressed, a row and a column make a contact

MODULE-5 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 17


KEYPAD
 It is the function of the microcontroller to scan the keyboard
continuously to detect and identify the key pressed
 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 columns is D3 –D0 = 1111, no key has been
pressed and the process continues till key press is detected
 If one of the column bits has a zero, this means that a key press has
occurred
 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 it find
out which column the pressed key belongs to.
MODULE-5 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 18
KEYPAD
 Identify the row and column of the pressed key for
(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
Answer : (a). 2 (b). 7

MODULE-5 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 19


KEYPAD
STEPS FOR KEY PRESS IDENTIFICATION
 Initially all switches are assumed to be
released. So there is no connection between
the rows and columns.

 When any one of the switches are pressed, the


corresponding row and column are connected
(short circuited). This will drive that column pin
(initially high) low.

 Using this logic, the button press can be


detected. The colors red and black is for logic
high and low respectively.

 Step 1: The first step involved in interfacing the


MODULE-5matrix keypad is to write
BECE204L all logicAND0’s
– MICROPROCESSORS to the
MICROCONTROLLERS 20
KEYPAD
STEPS FOR KEY PRESS IDENTIFICATION

 Step 2: Now the program has to scan the


pins connected to columns of the keypad. If
it detects a logic 0 in any one of the
columns, then a key press was made in that
column. This is because the event of the
switch press shorts the C2 line with R2.
Hence C2 is driven low.

 Step 3: Once the column corresponding to


the key pressed is located, start writing logic
0’s to the rows sequentially (one after the
other) and check if C2 becomes low.
MODULE-5 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 21
KEYPAD
STEPS FOR KEY PRESS IDENTIFICATION

 Step 4: The procedure is followed till C2 goes


low when logic low is written to a row. In this
case, a logic low to the second row will be
reflected in the second column.

 We already know that the key press


happened at column 2. Now we have
detected that the key is in row 2. So, the
position of the key in the matrix is (2,2).

 Once this is detected, its up to us to name it


or provide it with a task on the event of the
key press.
MODULE-5 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 22
KEYPAD
STEPS FOR KEY PRESS IDENTIFICATION

MODULE-5 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 23


KEYPAD
PROGRAM
Write an assembly language program for the 8051 to interface the 4x4 matrix
keypad and LCD. Any key pressed on the Keypad must be display in LCD.

MODULE-5 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 24


KEYPAD
ORG 000H
SJMP START

ORG 0030H
START: MOV P0,#0FFH ;MAKE P0 AN
INPUT PORT
ACALL LCD_INITIALIZE

K1: MOV P1,#0


;GROUND ALL ROWS AT ONCE
MOV A,P0 ;READ
ALL COL.
ANL A,#00001111B
;MASKED UNUSED BIT
CJNE A,#00001111B,K1 ;CHECK ALL
KEYS RELEASED

K2: ACALL DELAY ;CALL


20 MS DELAY
MODULE-5 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 25
KEYPAD
OVER: ACALL DELAY ;WAIT 20 ms
Key DEBOUNCE TIME
MOV A,P0 ;CHECK
KEY CLOSURE
ANL A,#00001111B
;MASKED UNUSED BIT
CJNE A,#00001111B,OVER1 ;KEY PRESSED,
FIND ROW
SJMP K2
;IF NONE, KEEP POLLING

OVER1: MOV P1,#1111110B ;GROUND ROW


0
MOV A,P0
;READ ALL COLUMNS
ANL A,#00001111B
;MASKED UNUSED BIT
MODULE-5 CJNE A,#00001111B,ROW_0
BECE204L ;ROW0, FIND
– MICROPROCESSORS AND MICROCONTROLLERS 26
KEYPAD
MOV P1,#11111011B
;GROUND ROW 2
MOV A,P0
;READ ALL COL.
ANL A,#00001111B ;
MASKED UNUSED BIT
CJNE A,#00001111B,ROW_2 ;ROW 2, FIND
COL
MOV P1,#11110111B
;GROUND ROW 3
MOV A,P0
;READ ALL COL.
ANL A,#00001111B
;MASKED UNUSED BIT
CJNE A,#00001111B,ROW_3 ;ROW 3, FIND
COL
LJMP K2
;IF NONE, FALSE INPUT, REPEAT
MODULE-5 ROW_0: MOV DPTR,– MICROPROCESSORS
BECE204L #KCODE0AND MICROCONTROLLERS;SET 27
KEYPAD
FIND: RRC A
;SEE IF ANY CY BIT LOW
JNC MATCH
;IF ZERO GET ASCII CODE
INC DPTR
;POINT TO NEXT COLUMN
SJMP FIND
;KEEP SEARCHING

MATCH: CLR A
MOVC A,@A+DPTR
ACALL LCD_DATA
;GET CODE FROM LOOK-UP TABLE
LJMP K1
;LOOP

DELAY: MOV R4,#40


MODULE-5 REPEAT: MOV R5,#230
BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 28
KEYPAD
ORG 300H
KCODE3: DB 'C','D','E','F‘
;ROW 3
KCODE2: DB '8','9','A','B‘
;ROW 2
KCODE1: DB '4','5','6','7‘
;ROW 1
KCODE0: DB '0','1','2','3‘
;ROW 0
END

LCD_INITIALIZE: MOV A,#38H


ACALL
LCD_COMMAND
MOV A,#0EH
ACALL
LCD_COMMAND
MOV A,#01H
ACALL
LCD_COMMAND
MODULE-5 MOV A,#06H
BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 29
KEYPAD
LCD_COMMAND: MOV P2,A
CLR
P3.7
CLR P3.6
SETB P3.5
ACALL DELAY
CLR P3.5
RET

LCD_DATA: MOV P2,A


SETB
P3.7
CLR
P3.6
SETB
P3.5
ACALL
MODULE-5 DELAY
BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 30
ANALOG TO DIGITAL
CONVERTER (ADC)
31

MODULE-5 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 31


ANALOG TO DIGITAL CONVERTER (ADC)
 ADCs (analog-to-digital converters) are among
the most widely used devices for data
acquisition

 A physical quantity, like temperature, pressure,


humidity, and velocity, etc., is converted to
electrical (voltage, current) signals using a
device called a transducer, or sensor

 Analog-to-digital converter needed to translate


the analog signals to digital numbers, so
microcontroller can read them

 Types of ADC:
 Parallel - 8 or more pins for the binary data. Ex:
MODULE-5
0804, 0808 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 32
ANALOG TO DIGITAL CONVERTER (ADC)
 The higher resolution ADC provides high accuracy by having a smaller
step size. Step size is smallest change that can be detected by an
ADC.

 Conversion time: Time taken by ADC to convert the analog input to a


digital (binary) number.

 ADC0804 IC is A 8-bit parallel an analog-to-digital converter


 Successive approximation ADC
 It works with +5 volts and has a resolution of 8 bits
MODULE-5 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 33
ANALOG TO DIGITAL CONVERTER (ADC)

ADC 0804 PIN DIAGRAM

MODULE-5 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 34


ANALOG TO DIGITAL CONVERTER (ADC)
 CLK IN and CLK R:
 CLK IN is an input pin connected to an external clock source
 To use the internal clock generator (also called self-clocking), CLK IN and CLK
R pins are connected to a capacitor and a resistor, and the clock frequency is
determined by

 Typical values are R = 10K ohms and C = 150 Pf.


 We get f= 606 kHz and the conversion time is 110 µs

 Vref/2:
 It is used for the reference voltage
 If this pin is open (not connected), the analog input voltage is in the range of
0 to 5 volts (the same as the Vcc pin)
 If the analog input range needs to be 0 to 4 volts, Vref/2 is connected to 2
volt
MODULE-5 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 35
ANALOG TO DIGITAL CONVERTER (ADC)
 D0-D7:
 The digital data output pins and these are tri-state buffered
 The converted data is accessed only when CS =0 and RD is forced low
 To calculate the output voltage, use the following formula

Where, Dout= digital data output (in dec.), Vin= analog voltage, and step size
(resolution) is the smallest change

 The following steps must be followed for data conversion by the


ADC0804 chip
 Make CS = 0 and send a low-to-high pulse to pin WR to start conversion
 Keep monitoring the INTR pin
 If INTR is low, the conversion is finished
 If the INTR is high, keep polling until it goes low
 If INTR has become low, make CS = 0 and send a high-to-low pulse to RD pin
MODULE-5
to get the dataBECE204L
out of –the ADC0804
MICROPROCESSORS AND MICROCONTROLLERS 36
ANALOG TO DIGITAL CONVERTER (ADC)

For 8-bit ADC

MODULE-5 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 37


ANALOG TO DIGITAL CONVERTER (ADC)
 Examine the ADC804 connection to the 8051 in Figure. Write a
program to monitor the INTR pin and bring an analog input into
register A. Do this continuously.

MODULE-5 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 38


ANALOG TO DIGITAL CONVERTER (ADC)

MODULE-5 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 39


ANALOG TO DIGITAL CONVERTER (ADC)
INTERFACING LM35 WITH 8051

 The ADC804 has 8-bit resolution with a maximum of 256 steps and
the LM35 (or LM34) produces 10 mV for every degree of temperature
Change

 we can condition Vin of the ADC804 to produce a Vout of 2560 mV full-


scale output. Therefore, in order to produce the fullscale Vout of 2.56
V for the ADC804

 We need to set Vref/2 = 1.28. This makes Vout of the ADC0804


correspond directly to the temperature as monitored by the LM35.

MODULE-5 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 40


ANALOG TO DIGITAL CONVERTER (ADC)

MODULE-5 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 41


DIGITAL TO ANALOG
CONVERTER (DAC)
42

MODULE-5 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 42


DIGITAL TO ANALOG CONVERTER (DAC)
 μC generates output in digital form but the controlling system requires
analog signal

 The digital-to-analog converter (DAC) is a device used to converts


digital data into equivalent analog voltage/current.

 Most commonly used DAC is R/2R method due to high precision. DAC
resolutions (in bits): 8, 10, and 12 bits

 The 8-bit DAC0808 converts digital data into equivalent analog


Current hence we require an I to V converter to convert this current
into equivalent voltage.

 The total current provided by the Iout pin is as follows:

MODULE-5 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 43


DIGITAL TO ANALOG CONVERTER (DAC)

MODULE-5 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 44


DIGITAL TO ANALOG CONVERTER (DAC)
PROGRAM
 Write a program to send data to the DAC to generate a Sawtooth,
triangle and staircase waveforms.

ORG 0000H
SAWTOOTH: MOV A, #00H
BACK: MOV P1,A
INC A
CJNE A,#255, BACK
MOV A,#00
SJMP SAWTOOTH
RET

MODULE-5 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 45


DIGITAL TO ANALOG CONVERTER (DAC)
TRIANGLE: MOV A,#00
INCR: MOV P1,A
INC A
CJNE A,#255, INCR
DECR: MOV P1,A
DEC A
CJNE A,#00, DECR
SJMP TRIANGLE

STAIRCASE: MOV A,#00


MOV P1,A
RPT: ADD A,#51
MOV P1,A
CJNE A,#255, RPT
SJMP STAIRCASE
MODULE-5 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 46
SENSOR WITH SIGNAL
CONDITIONING INTERFACE

47

MODULE-5 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 47


SENSOR WITH SIGNAL CONDITIONING
INTERFACE
 Signal conditioning circuits are used to process the output signal from
sensors to be suitable for the next stage of operation

 The function of the signal conditioning circuits include


 Signal amplification (op-amp)
 Filtering (op-amp)
 Protection (Zener & photo isolation)
 Linearization
 Current – voltage change circuits
 Resistance change circuits (Wheatstone bridge)
 Error compensation

 Operational amplifiers are the basic element of many signal


conditioning modules
MODULE-5 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 48
SENSOR WITH SIGNAL CONDITIONING
INTERFACE

MODULE-5 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 49


SENSOR WITH SIGNAL CONDITIONING
INTERFACE

MODULE-5 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 50


SENSOR WITH SIGNAL CONDITIONING
INTERFACE

MODULE-5 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 51


SENSOR WITH SIGNAL CONDITIONING
INTERFACE

MODULE-5 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 52


SENSOR WITH SIGNAL CONDITIONING
INTERFACE

MODULE-5 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 53


SENSOR WITH SIGNAL CONDITIONING
INTERFACE

MODULE-5 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 54


THANK YOU

You might also like