Wednesday, April 13, 2016

4x4 Matrix Keypad interface with 89C51

This tutorial demonstrates the use of a matrix keypad with 89C51 microcontroller.

The demonstration software is divided in two parts, a first part that contains the keyboard interrupt drivers, and a second part that demonstrates how to use these drivers. All the software is written in assembly.

As soon as number of key is greater than 4, a matrix keypad is more economical in term of I/O used. For example a 16–key pad arranged as a 4 x 4 matrix can be implemented with only eight port pins. To minimize the number of pins required, the keys should be arranged in as a square matrix as shown in Figure 2. Row lines are connected to microcontroller inputs whereas column lines are connected to microcontroller outputs.

Circuit Diagram:



Assembly Language Program for 4x4 Matrix keypad interface with 89C51:
Program is written in ProgStudio, You can easy coustomize it for other assemblers 

For C code Click Here

INCLUDE 89C51.MC
;---------------------4x4 keyboard-------------------------
X1  EQU P1.0  ;return line (Row)
X2  EQU P1.1
X3  EQU P1.2
X4  EQU P1.3

Y1  EQU P1.4  ; scan lines (Column)
Y2  EQU P1.5
Y3  EQU P1.6
Y4  EQU P1.7
;----------------------------------------------------------
; Register definitions.

rBUFFER EQU 00h  ; read data register
wBUFFER EQU 01h  ; send data register
;----------------------------------------------------------
(0000H):
 JMP on_reset
;-----------------------Serial Interrupt-------------------
(0023H): 
 JNB RI,ser_down  
 MOV rBUFFER,SBUF  ; read data
 CLR RI
 JMP int_end
ser_down:
 MOV SBUF,wBUFFER  ; send data
 CLR TI
int_end: 
RETI
;----------------------------------------------------------
(00FFH):
on_reset:
;Initialize Timer
;Baud rate table

;Fosc = 11.0592 MHz
;Baud rate TH1 TH1(hex) SMOD(PCON.7)
;300  160 A0   0
;1200  232 E8   0
;2400  244 F4   0
;4800  250 FA   0
;9600  253 FD   0
;19200 253 FD   1
;28800 255 FF   0


  MOV TH1,#FDH ; baud rate 9600
  MOV TMOD,#20H ; timer-1 in 8-bit auto reload
  MOV SCON,#50H ; receive enable
  CLR PCON.7  ; SMOD = 0

  CALL KEYBOARD
;  MOV IE,#90H  ; Serial Interrupt 10010000b
ENDLESS:
  
  CALL get_byte
  MOV A,rBUFFER
  MOV wBUFFER,A
  CALL send_byte

  JMP ENDLESS 
;----------------------------------------------------------
;Without interrupt
;----------------------------------------------------------
send_byte:
  MOV SBUF,wBUFFER  ; send data
  SETB TR1
RS232BACK:  JNB SCON.1,RS232BACK ; wait for TI
  CLR TF1
  CLR SCON.1
  RET
;---------------------------------------------------------- 
get_byte:
  SETB TR1
RS232B:   JNB SCON.0,RS232B  ; wait for RI
  MOV rBUFFER,SBUF  ; get data
  CLR TF1
  CLR SCON.0
  RET
;-----------------------------------------------------------
;    KEYBOARD SCANNER
;-----------------------------------------------------------
KEYBOARD:
 DOWN1: 
  CLR Y1
  SETB Y2
  SETB Y3
  SETB Y4
  
  MOV C,X4
  MOV A,#01H
  JC DOWN2
  
  CALL DELAY
 XXX1: MOV C,X4
  JNC XXX1
  
  CALL KEYCHECK
 DOWN2:  

  SETB Y1
  CLR Y2
  SETB Y3
  SETB Y4
 
  MOV C,X4
  MOV A,#02H
  JC DOWN3

  CALL DELAY
 XXX2: MOV C,X4
  JNC XXX2
  
  CALL KEYCHECK
 DOWN3:  
  CLR Y3
  SETB Y2
  SETB Y1
  SETB Y4
  
  MOV C,X4
  MOV A,#03H
  JC DOWN4

  CALL DELAY
 XXX3: MOV C,X4
  JNC XXX3
  
  CALL KEYCHECK
 DOWN4:
  CLR Y1
  SETB Y2
  SETB Y3
  SETB Y4
  
  MOV C,X3
  MOV A,#04H
  JC DOWN5

  CALL DELAY
 XXX4: MOV C,X3
  JNC XXX4
  
  CALL KEYCHECK
 DOWN5:
  CLR Y2
  SETB Y1
  SETB Y3
  SETB Y4
  
  MOV C,X3
  MOV A,#05H
  JC DOWN6
  
  CALL DELAY
 XXX5: MOV C,X3
  JNC XXX5
  
  CALL KEYCHECK
 DOWN6:  
  CLR Y3
  SETB Y2
  SETB Y1
  SETB Y4
  
  MOV C,X3
  MOV A,#06H
  JC DOWN7

  CALL DELAY
 XXX6: MOV C,X3
  JNC XXX6
  
  CALL KEYCHECK
 DOWN7:  
  CLR Y1
  SETB Y2
  SETB Y3
  SETB Y4
  
  MOV C,X2
  MOV A,#07H
  JC DOWN8
  
  CALL DELAY
 XXX7: MOV C,X2
  JNC XXX7
  
  CALL KEYCHECK
 DOWN8:  
  CLR Y2
  SETB Y1
  SETB Y3
  SETB Y4
  
  MOV C,X2
  MOV A,#08H
  JC DOWN9

  CALL DELAY
 XXX8: MOV C,X2
  JNC XXX8
  
  CALL KEYCHECK
 DOWN9:  
  CLR Y3
  SETB Y2
  SETB Y1
  SETB Y4
  
  MOV C,X2
  MOV A,#09H
  JC DOWN0

  CALL DELAY
 XXX9: MOV C,X2
  JNC XXX9
  
  CALL KEYCHECK
 DOWN0:  
  CLR Y2
  SETB Y1
  SETB Y3
  SETB Y4
  
  MOV C,X1
  MOV A,#00H
  JC DOWNA

  CALL DELAY
 XXX0: MOV C,X1
  JNC XXX0
  
  CALL KEYCHECK
;------------------------------------------------
 DOWNA:  
  CLR Y4
  SETB Y1
  SETB Y3
  SETB Y2
  
  MOV C,X4
  MOV A,#0AH
  JC DOWNB

  CALL DELAY
 XXXA: MOV C,X4
  JNC XXXA
  
  CALL KEYCHECK

 DOWNB:  
  CLR Y4
  SETB Y1
  SETB Y3
  SETB Y2
  
  MOV C,X3
  MOV A,#0BH
  JC DOWNC

  CALL DELAY
 XXXB: MOV C,X3
  JNC XXXB
  
  CALL KEYCHECK

 DOWNC:  
  CLR Y4
  SETB Y1
  SETB Y3
  SETB Y2
  
  MOV C,X2
  MOV A,#0CH
  JC DOWND

  CALL DELAY
 XXXC: MOV C,X2
  JNC XXXC
  
  CALL KEYCHECK

 DOWND:  
  CLR Y4
  SETB Y1
  SETB Y3
  SETB Y2
  
  MOV C,X1
  MOV A,#0DH
  JC DOWNS

  CALL DELAY
 XXXD: MOV C,X1
  JNC XXXD
  
  CALL KEYCHECK

 DOWNS:  
  CLR Y1
  SETB Y4
  SETB Y3
  SETB Y2
  
  MOV C,X1
  MOV A,#22H  ; * key
  JC DOWNH

  CALL DELAY
 XXXS: MOV C,X1
  JNC XXXS
  
  CALL KEYCHECK

 DOWNH:  
  CLR Y3
  SETB Y4
  SETB Y1
  SETB Y2
  
  MOV C,X1
  MOV A,#33H  ; # key
  JC DOWNEND

  CALL DELAY
 XXXH: MOV C,X1
  JNC XXXH
  
  CALL KEYCHECK

 DOWNEND:  
  JMP DOWN1
RET
;-----------------------------------------------------------
;    KEYBOARD DECODER
;-----------------------------------------------------------
KEYCHECK:
  IF A = #00H THEN
   MOV wBUFFER,#30H
   CALL send_byte
  ENDIF

  IF A = #01H THEN
   MOV wBUFFER,#31H
   CALL send_byte
  ENDIF
  
  IF A = #02H THEN
   MOV wBUFFER,#32H
   CALL send_byte
  ENDIF

  IF A = #03H THEN
   MOV wBUFFER,#33H
   CALL send_byte
  ENDIF

  IF A = #04H THEN
   MOV wBUFFER,#34H
   CALL send_byte
  ENDIF
  
  IF A = #05H THEN
   MOV wBUFFER,#35H
   CALL send_byte
  ENDIF

  IF A = #06H THEN
   MOV wBUFFER,#36H
   CALL send_byte
  ENDIF

  IF A = #07H THEN
   MOV wBUFFER,#37H
   CALL send_byte
  ENDIF

  IF A = #08H THEN
   MOV wBUFFER,#38H
   CALL send_byte
  ENDIF
  
  IF A = #09H THEN
   MOV wBUFFER,#39H
   CALL send_byte
  ENDIF
 
  IF A = #0AH THEN
   MOV wBUFFER,#41H
   CALL send_byte
  ENDIF

  IF A = #0BH THEN
   MOV wBUFFER,#42H
   CALL send_byte
  ENDIF

  IF A = #0CH THEN
   MOV wBUFFER,#43H
   CALL send_byte
  ENDIF

  IF A = #0DH THEN
   MOV wBUFFER,#44H
   CALL send_byte
  ENDIF

  IF A = #22H THEN
   MOV wBUFFER,#2AH
   CALL send_byte
  ENDIF

  IF A = #33H THEN
   MOV wBUFFER,#23H
   CALL send_byte
  ENDIF
 
RET
;----------------------------------------------------------
DELAY:
  MOV R7,#FFH
dloop: NOP
  NOP
  NOP
  DJNZ R7,dloop
RET
;----------------------------------------------------------

For C code Click Here
You can find more tutorial from top menu

No comments:

Post a Comment