The key board here we are interfacing is a matrix keyboard. This key board is designed with a particular rows and columns. These rows and columns are connected to the microcontroller through its ports of the micro controller 8051. We normally use 8*8 matrix key board. So only two ports of 8051 can be easily connected to the rows and columns of the key board.
Keyboard is organized in a matrix of rows and columns as shown in the figure. The microcontroller accesses both rows and columns through the port.
Thanks for reading: Interfacing the Keyboard to microcontroller 8051
![]() |
| Keyboard to microcontroller 8051 |
When ever a key is pressed, a row and a column gets shorted through that pressed key and all the other keys are left open. When a key is pressed only a bit in the port goes high. Which indicates microcontroller that the key is pressed. By this high on the bit key in the corresponding column is identified.
Once we are sure that one of key in the key board is pressed next our aim is to identify that key. To do this we firstly check for particular row and then we check the corresponding column the key board.
To check the row of the pressed key in the keyboard, one of the row is made high by making one of bit in the output port of 8051 high . This is done until the row is found out. Once we get the row next out job is to find out the column of the pressed key. The column is detected by contents in the input ports with the help of a counter. The content of the input port is rotated with carry until the carry bit is set.
The contents of the counter is then compared and displayed in the display. This display is designed using a seven segment display and a BCD to seven segment decoder IC 7447.
The BCD equivalent number of counter is sent through output part of 8051 displays the number of pressed key.
| Circuit diagram of INTERFACING KEY BOARD TO 8051 |
The programming algorithm, program and the circuit diagram is as follows. Here program is explained with comments.
![]() |
| Circuit diagram of INTERFACING KEY BOARD TO 8051 |
- The 8051 has 4 I/O ports P0 to P3 each with 8 I/O pins, P0.0 to P0.7,P1.0 to P1.7, P2.0 to P2.7, P3.0 to P3.7. The one of the port P1 (it understood that P1 means P1.0 to P1.7) as an I/P port for microcontroller 8051, port P0 as an O/P port of microcontroller 8051 and port P2 is used for displaying the number of pressed key.
- Make all rows of port P0 high so that it gives high signal when key is pressed.
- See if any key is pressed by scanning the port P1 by checking all columns for non zero condition.
- If any key is pressed, to identify which key is pressed make one row high at a time.
- Initiate a counter to hold the count so that each key is counted.
- Check port P1 for nonzero condition. If any nonzero number is there in [accumulator], start column scanning by following step 9.
- Otherwise make next row high in port P1.
- Add a count of 08h to the counter to move to the next row by repeating steps from step 6.
- If any key pressed is found, the [accumulator] content is rotated right through the carry until carry bit sets, while doing this increment the count in the counter till carry is found.
- Move the content in the counter to display in data field or to memory location
- To repeat the procedures go to step 2.
dispclr equ 00000001b
funcset equ 00111000b
entrmod equ 00000110b
dispon equ 00001100b
KbdData bit P3.3
KbdClock bit INT0;P3.2
DataKeyboard equ 30h
;
org 0h
nop
call init_LCD
;
Forever:
JB KbdClock,$
call GetDataKeyboard
jnc Forever
acall LCD_Out
clr C
sjmp Forever
;
;==================================
;Subroutine Get Data From Keyboard
;==================================
GetDataKeyboard:
call Get_scancode
cjne a,#0F0h,kybd_pressed
JB KbdClock,$
call Get_Scancode
Setb C
call Convert_Scancode
ret
;
Kybd_pressed:
Clr C
ret
;
;===============================
;Subroutine Convert Scan Data
;Using Look Up Table
;===============================
Convert_ScanCode:
Mov DPTR,#TableKeyboard
movc A,@A+DPTR
mov DataKeyboard,A
ret
;
LCD_Out:
mov R7, #80h
acall write_inst
mov R7,DataKeyboard
acall write_data
ret
;
init_lcd:
mov R7,#dispclr
acall write_inst
mov R7,#funcset
acall write_inst
mov R7,#dispon
acall write_inst
mov R7,#entrmod
acall write_inst
ret
;
Get_scanCode:
clr A
Jnb KbdClock,$
mov R0,#08h
Get_ScanCode2:
Jb KbdClock,$
Mov C,KbdData
Rr A
mov acc.7,C
Jnb KbdClock,$
Djnz R0,Get_ScanCode2
Jb KbdClock,$
Jnb KbdClock,$
Jb KbdClock,$
Jnb KbdClock,$
ret
;
Write_inst:
Clr P3.6 ; RS = 0,
Mov P0,R7 ; D7 s/d D0 = P0 = R1
Setb P3.7 ; EN = 1
call delay; call delay time
Clr P3.7 ; EN = 0
ret
;
Write_data:
Setb P3.6 ; RS = 1,
Mov P0,R7 ; D7 s/d D0 = P0 = R1
Setb P3.7 ; EN = 1
call delay; call delay time
Clr p3.7 ; EN = 0
ret
;
delay: mov R5,#0
delay1:mov R4,#02fh
djnz R4,$
djnz R5,delay1
ret
;
TableKeyboard:
DB 00
DB 0F9H
DB 00
DB 0F5H,0F3H,0F1H,0F2H,0FCH
DB 00H
DB 0FAH,0F8H,0F6H,0F4H
DB 0C0H
DB '~'
DB 00H
DB 00H
DB 0C1H
DB 0C2H
DB 00H
DB 0C3H
DB 'Q1'
DB 00H,00H,00H
DB 'ZSAW2'
DB 00H,00H
DB 'CXDE43'
DB 00H,00H
DB ' VFTR5'
DB 00H,00H
DB 'NBHGY6'
DB 00H,00H,00H
DB 'MJU78'
DB 00H,00H
DB ',KIO09'
DB 00H,00H
DB './L;P-'
DB 00H,00H,00H
DB 27H
DB 00H
DB '[='
DB 00H,00H
DB 0C5H
DB 0C6H
DB 0AH
DB ']'
DB 00H
DB '\'
DB 00H,00H,00H,00H,00H,00H,00H,00H
DB 08H
DB 00H,00H,00H,00H,00H,00H,00H,00H,00H,00H,00H,00H,00H
DB 00H,00H
DB 0DH
DB 00H
DB 0FBH
DB 00H,00H,00H,00H,00H,00H,00H,00H,00H,00H
DB 0F7H
end
