#Microcontrollers Keypad and LCD interfacing with 8051

 /*------------------Keypad--------------------*/
/*
=============================================================================
Interfacing Pins :     Keypad------------------------------------
                    P1.0 ----> Row 1    P1.4 ----> Col 1
                    P1.1 ----> Row 2 P1.5 ----> Col 2
                    P1.2 ----> Row 3 P1.6 ----> Col 3
                    P1.3 ----> Row 4 P1.7 ----> Col 4
LCD---------------------------------------
P0.0 thru P0.7 interfaced to D0 thru D7.
P3.2 ----> LCD_RS (Register Select).
P3.3 ----> LCD_EN (Enable).
=============================================================================
*/

org 0x00
ljmp start ;jump to start on reset

;lookup table
LUT: DB '0','4','8','C','1','5','9','D','2','6','A','E','3','7','B','F'

/*-------------------------------LCD Routines----------------------------------*/
;Interface declaration
RS  EQU P3^2
EN  EQU P3^3

DelaymS:
mov r4,#04 ;  (4*255) ~ 1000uS = 1mS
mov r5,#0
loop:
djnz r5,loop
djnz r4,loop
ret

;Subroutine to write DATA to LCD :
;Input Value: passed in A accumulator:

write_lcd_data:
mov P0,A ;Write the value on port P0
mov r6,#05h ;Call delay for 5mS
continue1:
lcall DelaymS
djnz r6, continue1
SETB RS
SETB EN
mov r6,#01h ;Call delay for 1mS
continue2:
lcall DelaymS
djnz r6, continue2
MOV P3,#00 ; clear the control signals
ret

;Subroutine to send command to LCD

write_lcd_cmd:
mov P0,A ;Write the value on port P0
mov r6,#0ah ;Call delay for 5mS
continue3:
lcall DelaymS
djnz r6, continue3
SETB EN
mov r6,#01h ;Call delay for 1mS
continue4:
lcall DelaymS
djnz r6, continue4
MOV P3,#00 ; clear the control signals
ret


;Subroutine to blink display
blink_display:
mov a,#08
lcall write_lcd_cmd
mov a, #0fh
lcall write_lcd_cmd
ret

lcd_init:
mov a,#0x38 ; //function set
lcall write_lcd_cmd
mov a,#0x08 ; //display off
lcall write_lcd_cmd
mov a,#0x01 ; //display clear
lcall write_lcd_cmd
mov a,#0x06 ; //entry mode set
lcall write_lcd_cmd
mov a,#0x0f ; //display on
lcall write_lcd_cmd
mov a,#0x80 ; //set address counter value
lcall write_lcd_cmd
ret

disp_key:
mov a,r0
mov dptr,#LUT
movc a,@a+dptr
lcall write_lcd_data
ret


/*--------------------KEYPAD Routines------------------------------*/
keypad_init:
mov P1,#0xF0 ; set rows (P1.0-P1.3) as inputs and Cols(P1.4-P1.7) as output
ret

get_row:
mov a , r0
mov r2,#0x3
mov r3,#0x00
it_1:
jnb acc.0, get_row_ret
rr a
inc r3
djnz r2, it_1
get_row_ret:
mov a,r3
add a,r1
mov r0,a
ret

get_key:
mov r1,#0x00 ;r1 contains col identifier
mov P1,#0xEF ;set col 0 =0
mov a , P1 ;ckeck if key is pressed
mov r0,a ;r0 contains row number
anl a ,#0x0F ;check if any row is LOW
cjne a,#0x0F,get_row
k1:
mov r1,#0x04 ;r1 contains col identifier
mov P1,#0xDF ;set col 1 =0
mov a , P1 ;ckeck if key is pressed
mov r0,a ;r0 contains row number
anl a ,#0x0F ;check if any row is LOW
cjne a,#0x0F, get_row
k2:
mov r1,#0x08 ;r1 contains col identifier
mov P1,#0xBF ;set col 2 =0
mov a , P1 ;ckeck if key is pressed
mov r0,a ;r0 contains row number
anl a ,#0x0F ;check if any row is LOW
cjne a,#0x0F,get_row
k3:
mov r1,#0x0C ;r1 contains col identifier
mov P1,#0x7F ;set col 3 =0
mov a , P1 ;ckeck if key is pressed
mov r0,a ;r0 contains row number
anl a ,#0x0F ;check if any row is LOW
cjne a,#0x0F,get_row
ajmp get_key

delay:
mov r4,#0xFF
i2: mov r5,#0xFF
i1: djnz r5, i1
djnz r4, i2
ret


/*---------------------------MAIN-----------------------------------*/
start:   ;main program
lcall lcd_init
lcall keypad_init


key_loop:
lcall get_key
lcall disp_key
lcall delay
jmp key_loop
end

Comments