Back to PCAM+ page.
Back to Chapter 9 4×4 Keypad and LCD Interface with 89S52 MicrocontrollerThe 4-by-4 keypad is a commonly used interface circuit in embedded applications. This keypad has eight lines with 16 switches arranged in a matrix format.In this application, we will have the LCD of the PCAM+ display a number, 1 through 16, depending on which key is pressed on the keypad. We will need to use 19 of the 20 connecting wires in the kit: - Connect P3.7, P3.6, P3.5 of the 89S52 to the LCD section's RS, RW, EN pins respectively.
- Connect pins 0-7 of the LCD to the respective P0 port pins of the 89S52.
- Connect pins 0-7 of the Keypad to the respective P2 port pins of the 89S52.
Write the following code in 8051IDE: ;For LCD RS EQU P3.7 ;change as per your connections RW EQU P3.6 ;change as per your connections EN EQU P3.5 ;change as per your connections
PDATA EQU P0 ;change as per your connections
LCALL INIT_LCD LCALL CLEAR_LCD
START: MOV P2,#0FFh CLR P2.7 JNB P2.0,Key_1 JNB P2.1,Key_2 JNB P2.2,Key_3 JNB P2.3,Key_4 MOV P2,#0FFh CLR P2.6 JNB P2.0,Key_5 JNB P2.1,Key_6 JNB P2.2,Key_7 JNB P2.3,Key_8 MOV P2,#0FFh CLR P2.5 JNB P2.0,Key_9 JNB P2.1,Key_10 JNB P2.2,Key_11 JNB P2.3,Key_12 MOV P2,#0FFh CLR P2.4 JNB P2.0,Key_13 JNB P2.1,Key_14 JNB P2.2,Key_15 JNB P2.3,Key_16 SJMP START
Key_1: LJMP K_1
Key_2: LJMP K_2
Key_3: LJMP K_3
Key_4: LJMP K_4
Key_5: LJMP K_5
Key_6: LJMP K_6
Key_7: LJMP K_7
Key_8: LJMP K_8
Key_9: LJMP K_9
Key_10: LJMP K_10
Key_11: LJMP K_11
Key_12: LJMP K_12
Key_13: LJMP K_13
Key_14: LJMP K_14
Key_15: LJMP K_15
Key_16: LJMP K_16
K_1: LCALL CLEAR_LCD MOV A,#'1' LCALL WRITE_TEXT JNB P2.0,$ LJMP START
K_2: LCALL CLEAR_LCD MOV A,#'2' LCALL WRITE_TEXT JNB P2.1,$ LJMP START
K_3: LCALL CLEAR_LCD MOV A,#'3' LCALL WRITE_TEXT JNB P2.2,$ LJMP START
K_4: LCALL CLEAR_LCD MOV A,#'4' LCALL WRITE_TEXT JNB P2.3,$ LJMP START
K_5: LCALL CLEAR_LCD MOV A,#'5' LCALL WRITE_TEXT JNB P2.0,$ LJMP START
K_6: LCALL CLEAR_LCD MOV A,#'6' LCALL WRITE_TEXT JNB P2.1,$ LJMP START
K_7: LCALL CLEAR_LCD MOV A,#'7' LCALL WRITE_TEXT JNB P2.2,$ LJMP START
K_8: LCALL CLEAR_LCD MOV A,#'8' LCALL WRITE_TEXT JNB P2.3,$ LJMP START
K_9: LCALL CLEAR_LCD MOV A,#'9' LCALL WRITE_TEXT JNB P2.0,$ LJMP START
K_10: LCALL CLEAR_LCD MOV A,#'1' LCALL WRITE_TEXT MOV A,#'0' LCALL WRITE_TEXT JNB P2.1,$ LJMP START
K_11: LCALL CLEAR_LCD MOV A,#'1' LCALL WRITE_TEXT MOV A,#'1' LCALL WRITE_TEXT JNB P2.2,$ LJMP START
K_12: LCALL CLEAR_LCD MOV A,#'1' LCALL WRITE_TEXT MOV A,#'2' LCALL WRITE_TEXT JNB P2.3,$ LJMP START
K_13: LCALL CLEAR_LCD MOV A,#'1' LCALL WRITE_TEXT MOV A,#'3' LCALL WRITE_TEXT JNB P2.0,$ LJMP START
K_14: LCALL CLEAR_LCD MOV A,#'1' LCALL WRITE_TEXT MOV A,#'4' LCALL WRITE_TEXT JNB P2.1,$ LJMP START
K_15: LCALL CLEAR_LCD MOV A,#'1' LCALL WRITE_TEXT MOV A,#'5' LCALL WRITE_TEXT JNB P2.2,$ LJMP START
K_16: LCALL CLEAR_LCD MOV A,#'1' LCALL WRITE_TEXT MOV A,#'6' LCALL WRITE_TEXT JNB P2.3,$ LJMP START
INIT_LCD: CLR RS MOV PDATA,#00110100b SETB EN CLR EN LCALL WAIT_LCD
CLR RS MOV PDATA,#00111100b SETB EN CLR EN LCALL WAIT_LCD
CLR RS MOV PDATA,#0Ch SETB EN CLR EN LCALL WAIT_LCD
CLR RS MOV PDATA,#06h SETB EN CLR EN LCALL WAIT_LCD RET
;NOTE: Copy character to be displayed in A WRITE_TEXT: SETB RS MOV PDATA,A SETB EN CLR EN LCALL WAIT_LCD RET
CLEAR_LCD: CLR RS MOV PDATA,#01h SETB EN CLR EN LCALL WAIT_LCD RET
WAIT_LCD: CLR EN CLR RS SETB RW MOV PDATA,#0FFh SETB EN MOV A,PDATA JB ACC.7,WAIT_LCD CLR EN CLR RW RET Assemble the code and program it to the 89S52 on PCAM+ using the HandyProgUSB programmer. Power up the PCAM+ and try pressing buttons on the 4×4 keypad. You should see a number displayed for each different button pressed. End of Tutorial Now you can experiment with what you have learned. For example, can you adapt this keypad code to display the numbers on the 7-segment LEDs? to turn a motor back and forth? to display and then scroll text on the LCD? to display the text on your PC via RS232? How about doing these things from the IR remote control?
Back to PCAM+ page. |