OBJECTIVE:
- Students understand the 4x4 keypad interface circuit with a microcontroller
- Students understand assembly language for keypad data retrieval
- Students understand assembly language for taking keypad data and outputting it to the LED.
- Students understand assembly language for keypad data retrieval and output to 7 segments.
- Students understand assembly language for retrieving keypad data and outputting it to the Character LCD.
Figure 7.1 4x4 keypad interface circuit
Keypads are often used as input on some microprocessor or microcontroller-based devices. The keypad actually consists of a number of switches, which are connected as rows and columns with an arrangement as shown in Figure 7.2. In order for the microcontroller to scan the keypad, the port outputs one of the 4 bits connected to the column with a low logic "0" and then reads the 4 bits on the row to test if a button is pressed on that column. As a consequence, as long as no button is pressed, the microcontroller will see it as a high logic "1" on each pin connected to the row.
Figure 7.3. Basic 4x4 keypad circuit
7.1. Experiment to scan 1x4 keypad data, COR-MEN-UpArrow-DnArrow
In this experiment, we will scan the COR-MEN-UpArr.- DnArr. keypad data and output the data to the LED. To do this experiment, do the following steps:
- Open the jumper on DAC_EN, if it is installed.
- Connect the jumper on LED_EN.
- Connect the Microcontroller Trainer module to the +5V power supply.
- Connect the Microcontroller Trainer module to the programmer circuit
Figure 7.4. LED Display as Keypad Data Output - Open the M-IDE Studio for MCS-51 program, as a program editor and compiler.
- Type the following program
download file prog71a.asm).
col4 bi P2.0 col3 bit P2.1 col2 bit P2.2 col1 bit P2.3 row1 bit P2.4 row2 bit P2.5 row3 bit P2.6 row4 bit P2.7 ; keydata equ 70h keybounc equ 71h keyport equ P2 ; org 0h mov P2,#11111111b start: call keypad4x4 ;calling subroutine keypad4x4 Mov A,keydata ;A = keydata Cjne A,#0FFh,send ; sjmp start ;LOOPING FOREVER send: CPL A ;A = NOT A Mov P0,A ;P0 = A Sjmp start ;LOOPING FOREVER PART 2 ; ;========================================== ; subroutine scan keypad 1 column x 4 row ;========================================== Keypad4x4: mov keybounc,#50 ;keybounc = 50 mov keyport,#0FFh ;keyport=P2= FF clr col4 ;col1= P3.0 = 0 ; keyCOR: jb row4,keyMEN ; Key COR djnz keybounc,KeyCOR mov keydata,#0Ah ;Data Output = 0Ah ret ; keyMEN: jb row3,keyUpA ; Key MEN djnz keybounc,keyMEN mov keydata,#0bh ;Data Output = 0bh ret ; keyUpA: jb row2,keyDnA ; djnz keybounc,keyUpA mov keydata,#0ch ; Data Output = 0ch ret ; keyDnA: jb row1,Nokey ; djnz keybounc,keyDnA mov keydata,#0dh ; Data Output = 0dh ret Nokey:mov keydata,#0FFh ret ;================================ ;The end of Keypad 4x4 subroutine ;================================ delay: mov R0,#0 delay1:mov R2,#50 djnz R2,$ djnz R0,delay1 ret ; end
- Save the program you typed and name it: prog71a.asm
- In the MIDE program, select Build /F9 or to compile the program from *.asm to *.hex.
- Program the microcontroller using the ISP Software Program (See Instructions for Use)
- Make modifications to the program above to scan keypad data 3-6-9-ENT
- Make modifications to the program above to scan keypad data 2-5-8-0
- Make modifications to the program above to scan keypad data 1-4-7-CAN
7.2. Experiment scanning 4x4 keypad data and outputting it to the LCD
In this experiment, the keypad data will be scanned and the data will be output to the Character LCD. To do this experiment, do the following steps:
- Install the EN_LCD jumper
- Open the jumper on EN_DAC, if it is installed
- Connect the Microcontroller Trainer module to the +5V power supply.
- Connect the Microcontroller Trainer module to the programmer circuit
- Open the M-IDE Studio for MCS-51 program, as a program editor and compiler.
- Type the following program
download file prog72a.asm )
col4 bit P2.0 col3 bit P2.1 col2 bit P2.2 col1 bit P2.3 row1 bit P2.4 row2 bit P2.5 row3 bit P2.6 row4 bit P2.7 ; keydata equ 70h keybounc equ 71h keyport equ P2 org 0h mov P2,#11111111b call Init_LCD start: call keypad4x4 ;calling subroutine keypad4x4 Mov A,keydata ;A = keydata Cjne A,#0FFh,WrLCD; sjmp start ;LOOPING FOREVER PART 1 ; WrLCD: Mov R1,#80h ;Pick DDRAM 1st row and 1st col call write_inst Mov R1,#30h Add A,R1 Mov R1,A call write_data ;write data Sjmp start ;LOOPING FOREVER PART 2; ; Init_lcd: mov r1,#00000001b ;Display clear call write_inst ; mov r1,#00111000b ;Function set, Data 8 bit,2 line font 5x7 call write_inst ; mov r1,#00001100b ;Display on, cursor off,cursor blink off call write_inst mov r1,#00000110b ;Entry mode, Set increment call write_inst ret ; Write_inst: clr P3.6 ; P3.6 = RS =0 mov P0,R1 ; P0 = D7 s/d D0 = R1 setb P3.7 ; P3.7 =EN = 1 call delay ; call delay time clr P3.7 ; P3.7 =EN = 0 ret ; Write_data: setb P3.6 ; P3.6 = RS =1 mov P0,R1 ; P0 = D7 s/d D0 = R1 setb P3.7 ; P3.7 =EN = 1 call delay ; call delay time call delay ; call delay time clr p3.7 ; P3.7 =EN = 0 ret ; ;==================================== ; subroutine scan keypad 4x4 ;==================================== Keypad4x4: mov keybounc,#50 ;keybounc = 50 mov keyport,#0FFh ;keyport=P2= FF clr col4 ;col4 = 0 ; keyCOR: jb row4,keyMEN ; Key COR djnz keybounc,KeyCOR mov keydata,#0Ah ;Data Output ret ; keyMEN: jb row3,keyUpA ; Key MEN djnz keybounc,keyMEN mov keydata,#0bh ;Data Output ret keyUpA: jb row2,keyDnA ; Key Up Arrow djnz keybounc,keyUpA mov keydata,#0ch ;Data Output ret ; keyDnA: jb row1,key3 ; Key Down Arrow djnz keybounc,keyDnA mov keydata,#0dh ;Data Output ret ;========================================== key3: setb col4 clr col3 jb row4,key6 djnz keybounc,key3 ; Key 3 mov keydata,#03h ;Data Output ret ; key6: jb row3,key9 djnz keybounc,key6 ; Key 6 mov keydata,#06h ;Data Output ret ; key9: jb row2,keyENT djnz keybounc,key9 ; Key 9 mov keydata,#09h ;Data Output ret ; keyENT: jb row1,key2 djnz keybounc,keyENT ; Key ENT mov keydata,#0eh ;Data Output ret ;============================================ key2: setb col3 clr col2 jb row4,key5 djnz keybounc,key2 mov keydata,#02h ;Data Output ret ; key5: jb row3,key8 djnz keybounc,key5 mov keydata,#05h ; Data Output ret ; key8: jb row2,key0 djnz keybounc,key8 mov keydata,#08h ;Data Output ret ; key0: jb row1,key1 djnz keybounc,key0 mov keydata,#00h ;Data Output ret ;============================================== key1: setb col2 clr col1 jb row4,key4 djnz keybounc,key1 mov keydata,#01h ;Data Output ret ; key4: jb row3,key7 djnz keybounc,key4 mov keydata,#04h ;Data Output ret ; key7: jb row2,keyCAN djnz keybounc,key7 mov keydata,#07h ;Data Output ret ; keyCAN: jb row1,Nokey djnz keybounc,keyCAN mov keydata,#0Fh ;Data Output ret ; Nokey: mov keydata,#0FFh ret ;================================ ;The end of Keypad 4x4 subroutine ;================================ delay: mov R0,#0 delay1: mov R2,#50 djnz R2,$ djnz R0,delay1 ret ; end
- Save the program you typed and name it: prog72a.asm
- In the MIDE program, select Build /F9 or to compile the program from *.asm to *.hex.
- Program the microcontroller using the ISP Software Program (See Instructions for Use)
- Make observations on the LCD
- Make modifications to the program to output keypad data to the LCD DDRAM location:
| No | Lokasi Display LCD Karakter Data Keypad |
|----|-----------------------------------------|
| 1 | Baris 2 Kolom 2 |
| 2 | Baris 2 Kolom 16 |
Make modifications to the circuit to display keypad data to a 7-segment display according to experiment 3.