1 of 37

UNIT-V: �Applications and Interfacing of 8051

2 of 37

Introduction:

  • In this chapter we learn about some real world applications of 8051.
  • LCD
  • Keyboard
  • ADC,
  • DAC and
  • External memory interfacing to 8051.

3 of 37

LCD interfacing to 8051.��

  • In recent years LCDs replaces the Seven segment displays becoz of
  • The ability to display the numbers, characters and graphics(limited in LEDs)
  • Ease of programming for characters and graphics
  • The LEDs must be refreshed by CPU.

4 of 37

Contd..

5 of 37

LCD commands�

6 of 37

Programming LCD16x2

  • Initialize LCD16x2 
  • It is very easy to initialize LCD
  • Power ON LCD
  • Wait for 15ms, Power-on initialization time for LCD16x2
  • Send 0x38 command (initialize. 2 line, 5x8 matrix, 8-bit mode)
  • MOV A,#38h
  • Send any Display ON command (0x0E, 0x0C)
  • MOV A,#0Eh---- Display On , Cursor ON but Cursor blinking OFF.
  • Send 0x06 command (increment cursor)
  • MOV A,#06h----the cursor position automatically increments by one & moves to right
  • MOV A,#01h----Clearing the LCD

7 of 37

Command write function

  • Send command to the data port
  • Make RS pin low, RS=0 (command reg.)
  • Make RW pin low, RW=0 (write operation)
  • Give High to Low pulse at Enable (E) minimum of 450ns.
  • When we give Enable pulse, LCD latch the data present at D0 to D7 & execute a command as RS is command reg.

8 of 37

Data write function

  • Send command to the data port
  • Make RS pin low, RS=1 (data reg.)
  • Make RW pin low, RW=0 (write operation)
  • Give High to Low pulse at Enable (E) minimum of 450 ns
  • When we give Enable pulse, LCD latch the data present at D0 to D7 & display it on the 5x8 matrix as RS is data reg.

9 of 37

Note

  • LCD Power on delay: after power on, we can’t send commands immediately, LCD16x2 needs a self-initialization time of 15ms.
  • While programming we need to take care of providing sufficient power on delay> 15ms, and then send a command to LCD.
  • After providing commands to execute, LCD16x2 takes time in microseconds but for 0x01 command (Clear display), it takes 1.64ms to execute.
  • So after giving this command, we need to give sufficient delay> 1.63milliseconds.

10 of 37

Rolling Display:

  • To roll the string or character on the LCD, 
  • Display string on the LCD
  • Roll it to the right using the ‘0x1C’ command
  • Roll it to the left using the ‘0x18’ command

11 of 37

Let us write a program that will display 'DNA TECHNOLOGY' on LCD.

  • ORG 0000h
  • CALL lcd_initialize
  • CALL lcd_clr
  • MOV A,#80h              //Location from where Data is to be displayed
  • CALL lcd_command
  • MOV A,#'D'
  • CALL lcd_datadisplay
  • MOV A,#'N'
  • CALL lcd_datadisplay
  • MOV A,#'A'
  • CALL lcd_datadisplay
  • MOV A,#20h                //Hex value for blank space to be displayed
  • CALL lcd_datadisplay
  • MOV A,#'T'
  • CALL lcd_datadisplay

12 of 37

  • MOV A,#'E'
  • CALL lcd_datadisplay
  • MOV A,#'C'
  • CALL lcd_datadisplay
  • MOV A,#'H'
  • CALL lcd_datadisplay
  • MOV A,#'N'
  • CALL lcd_datadisplay
  • MOV A,#'O'
  • CALL lcd_datadisplay
  • MOV A,#'L'
  • CALL lcd_datadisplay
  • MOV A,#'O'
  • CALL lcd_datadisplay
  • MOV A,#'G'

13 of 37

  • CALL lcd_datadisplay
  • MOV A,#'Y'
  • CALL lcd_datadisplay
  • stop:
  • sjmp stop

14 of 37

15 of 37

16 of 37

    • The rows are connected to an output port(Port 1) and the columns are connected to an input port. (Port 2)
    • If no key has been pressed, reading the input port will yield 1s for all columns since they are all connected to high (Vcc).
    • If all the rows are 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

17 of 37

KEY SCAN

  • To find out the key pressed ,
  • the controller grounds a row by sending a ‘0’ on the corresponding line of the output port.
  • It then reads the data at the columns using the input port.
  • If data from columns is D3-D0=1111, then no key is pressed.
  • If any bit of the column is ‘0’, it indicates that a key is pressed in that column.

18 of 37

  • In this example, the column is identified by the following values:
  • 1110 – key pressed in column 0
  • 1101 – key pressed in column 1
  • 1011 – key pressed in column 2
  • 0111– key pressed in column 3

19 of 37

STEPS TO FIND OUT KEY PRESSED

  • Beginning with the row 0, the microcontroller grounds it by providing a low to row D0 only.
  • It then reads the columns (port2). If the data read is all 1s, then no key in that row is activated and the process is moved to the next row.
  • It then grounds the next row, reads the columns, and checks for any zero. This process continues until a row with a zero is identified.
  • After identification of the row in which the key has been pressed, the column to which the pressed key belongs is identified as discussed above - by looking for a zero in the input values read.

20 of 37

Example:

  • D3 – D0 = 1101 for the row, D3 – D0 = 1011 for the column, indicate row 1 and column 3 are selected. This indicates that key 6 is pressed.
  • D3 – D0 = 1011 for the row, D3 – D0 = 0111 for the column, indicate row 2 and column 3 are selected. Then key ‘B’ is pressed.

21 of 37

PROGRAM:

  • The program used for detection and identification of the key activated goes through the following stages:
  • 1. To make sure that the preceding key has been released, 0s are output to all rows at once, and 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.

22 of 37

  • 2. To see if any key is pressed, the columns are scanned over and over in an infinite loop until one of them has a 0 on it.
    • Remember that the output latch is connected to rows, still have their initial zeros (in stage 1), making them grounded.
    • After the key press detection, it waits for 20-ms for the bounce and then scans the columns again.
  • It ensures that the first key press detection was not an erroneous one due to spike noise.
  •  
  • After the 20-ms delay, if the key is still pressed, then it goes to the loop (step 3) to detect the actual key pressed.

23 of 37

3. To detect which row the key pressed belongs to, it 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 does not belong to that row. Therefore, it grounds the next row and continues until it finds the row, that the key pressed belongs to.
  • Upon finding the row that the key pressed belongs to, it sets up the starting address for the lookup table holding the scan codes for that row.

24 of 37

4. To identify the key pressed, it rotates the column bits, 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.

25 of 37

PROGRAM:

  • keyboard subroutine. This program sends the ASCII code for pressed key to P0.1;P1.0-P1.3 connected to rows, P2.0-P2.3 to column
  • MOV P2,#0FFH ;make P2 an input port
  • K1: MOV P1,#0 ;ground all rows at once
  • MOV A,P2 ;read all col;(ensure keys open)
  • ANL A,00001111B ;masked unused bits
  • CJNE A,#00001111B,K1 ;till all keys release

26 of 37

  • K2: ACALL DELAY ;call 20 msec delay
  • MOV A,P2 ;see if any key is pressed
  • ANL A,00001111B ;mask unused bits
  • CJNE A,#00001111B,OVER ;key pressed, find row
  • SJMP K2 ;check till key pressed
  • OVER: ACALL DELAY ;wait 20 msec debounce time
  • MOV A,P2 ;check key closure
  • ANL A,00001111B ;mask unused bits

27 of 37

  • CJNE A,#00001111B,OVER1 ;key pressed, find row
  • SJMP K2 ;if none, keep polling
  • OVER1: MOV P1, #11111110B ;ground row 0
  • MOV A,P2 ;read all columns
  • ANL A,#00001111B ;mask unused bits
  • CJNE A,#00001111B,ROW_0 ;key row 0, find col.
  • MOV P1,#11111101B ;ground row 1
  • MOV A,P2 ;read all columns
  • ANL A,#00001111B ;mask unused bits

28 of 37

  • �CJNE A,#00001111B,ROW_1 ;key row 1, find col. MOV P1,#11111011B ;ground row 2
  • MOV A,P2 ;read all columns
  • ANL A,#00001111B ;mask unused bits CJNE A,#00001111B,ROW_2 ;key row 2, find col.
  • MOV P1,#11110111B ;ground row 3
  • MOV A,P2 ;read all columns
  • ANL A,#00001111B ;mask unused bits

29 of 37

  • CJNE A,#00001111B,ROW_3 ;key row 3, find col.
  • LJMP K2 ;if none, false input,
  • ROW_0: MOV DPTR,#KCODE0 ;set DPTR=start of row 0
  • SJMP FIND ;find col. Key belongs to
  • ROW_1: MOV DPTR,#KCODE1 ;set DPTR=start of row
  • SJMP FIND ;find col. Key belongs to

30 of 37

ADC INTERFACING TO 8051

  • ADC (Analog to digital converter) forms a very essential part in many embedded projects.
  • ADC 0804.
  • ADC0804 is an 8 bit successive approximation analogue to digital converter from National semiconductors.
  • features of ADC0804  
  • It has differential analogue voltage inputs,
  • 0-5V input voltage range,
  • no zero adjustment,
  • built in clock generator,
  • reference voltage can be externally adjusted to convert smaller analogue voltage span to 8 bit resolution etc.

31 of 37

Steps for converting the analogue input  and reading the output from ADC0804.

  • Make CS=0 and send a low to high pulse to WR pin to start the conversion.
  • Now keep checking the INTR pin.  INTR will be 1 if conversion is not finished and INTR will be 0 if conversion is finished.
  • If conversion is not finished (INTR=1) , poll until it is finished.
  • If conversion is finished (INTR=0), go to the next step.
  • Make CS=0 and send a high to low pulse to RD pin to read the data from the ADC.

32 of 37

Circuit diagram.

  • The circuit initiates the ADC to convert a given analogue input , then accepts the corresponding digital data and displays it on the LED array connected at P0.

33 of 37

example

  • if the analogue input voltage Vin is 5V then all LEDs will glow indicating 11111111 in binary which is the equivalent of 255 in decimal.
  • Dout=Din/step size
  • Dout– digital data output(decimal)
  • Din– analog input voltage
  • Step size smallest change  is the voltage difference between one digital level (i.e. 0001) and the next one
  • (2*vref/2)/256
  • (resolution-- the smallest incremental voltage that can be recognized and thus causes a change in the digital output.)

34 of 37

Resolution Vs Stepsize:

35 of 37

Program.

  • ORG 00H
  • MOV P1,#11111111B // initiates P1 as the input port
  • MAIN: CLR P3.7 // makes CS=0
  • SETB P3.6 // makes RD high
  • CLR P3.5 // makes WR low
  • SETB P3.5 // low to high pulse to WR for starting conversion
  • WAIT: JB P3.4,WAIT // polls until INTR=0
  • CLR P3.7 // ensures CS=0
  • CLR P3.6 // high to low pulse to RD for reading the data from ADC
  • MOV A,P1 // moves the digital data to accumulator
  • CPL A // complements the digital data (*see the notes)
  • MOV P0,A // outputs the data to P0 for the LEDs
  • SJMP MAIN // jumps back to the MAIN program
  • END

36 of 37

TIMING DIAGRAM

37 of 37

DAC INTERFACING WITH 8051: