| ??? 01/22/01 12:19 Read: times |
#8452 - Keypad issues |
Myself and my working partner are doing a project which scans a keypad and displays the value pressed on the keypad to a seven-segment display this is the following code we have used but it has failed to work.Any help in finding what is wrong with the code or new code would be much appreciated.
$MOD52 ;------------------------------------------------------------------------------------------- ;KeysPoll.asm ; ;Keypad scan routines ; ;The polling implementation ;=========================================================================================== ;UIOD connections ;output ports ;Col0 to P3.4 ;Col1 to P3.5 ;Col2 to P3.6 ;Col3 to p3.7 ; ;input ports ;Row 0 to P3.0 ;Row 1 to P3.1 ;Row 2 to p3.2 ;Row 3 to p3.3 ;------------------------------------------------------------------------------------------- ;system calls registers used ;------------------------------------------------------------------------------------------- crlf equ 0115h ;a delay equ 0118h ;a display equ 011bh ;a getchr equ 0121h ;a inkey equ 012ah ;a mon_return equ 0130h print equ 0136h ;a, dptr prthex equ 013fh ;a, r2 setintvec equ 0145h ;a, dtpr ;=========================================================================================== ; Main Program ;=========================================================================================== ORG 0000h start: poll: lcall KeyPressed ; see if a key is pressed jnc poll ; repeat if no key is pressed ;--- if a key is pressed the program falls out of the loop---------------------------------- ;--- the B register keeps row and column number of the-------------------------------------- ;--- pressed key in its low and high nibbles, respectively. -------------------------------- lcall FindRow ; get row number mov b, a ; save in B register jnc Getcolumn ; continue if no error lcall print ; else print error message db 'Error - key row not found',0 lcall crlf KP1: lcall KeyPressed ; wait until key is released jc KP1 sjmp start ; repeat GetColumn: lcall FindColumn ; get column number swap a ; swap nibbles orl a, b ; save as B register high nibble mov b, a jnc ReportKey ; continue if no error lcall print ; else print error message db 'Error - key row not found',0 lcall crlf KP2: lcall KeyPressed ; wait until key is released jc KP2 sjmp start ; repeat ReportKey: ; lcall DisplayKey ; commented out ;---show key in hex on the 7-segment display------------------------------------------------ lcall Convertkey lcall present mov P2, a KP3: lcall KeyPressed ; wait until key is released jc KP3 ljmp start ; repeat ;=========================================================================================== ;subroutine KeyPressed ;sets the carry flag if a key is pressed ; ; input : Port 3 bits are connected to a 4 by 4 ; matrix type keypad. Bits P3.0 through P3.3 are ; connected to the rows, and P3.4 through P3.7 are ; connected to the columns. ; ; output : the carry flag is set if a key is pressed, else ; the carry flag is cleared. ; ; calls : nothing ; destroys : a, C ;=========================================================================================== KeyPressed: mov P3, #0Fh ;set columns to 0, rows to 1 mov a, P3 ; read rows orl a, #0F0H ; mask high nibble cpl a ; a is 0 if no key is pressed jz NoKey setb C ret NoKey: clr C ret ;=========================================================================================== ;Subroutine FindRow ;returns the row number [0 - 3] of the key pressed in the accumulator ; ; input : Port 3 bits are connected to a 4 by 4 ; matrix type keypad. Bits P3.0 through P3.3 are ; connected to the rows, and P3.4 through P3.7 are ; connected to the columns. ; ; output : the row number of the key pressed is returned in a. ; the carry flag is set if a key is pressed, else ; the carry flag is cleared. ; ; calls : nothing ; destroys : a, r0 , C ;=========================================================================================== FindRow: mov a, P3 ; read rows mov r0, #4 ; initialize counter (R0) TryNextRow: rrc a ; read next row jnc RowFound djnz r0, TrynextRow ;--if loop FindRow does not find the row an error has occurred------------------------------ setb C ; signal an error ret ; done RowFound: mov a, #4 ; number the rows subb a, r0 ; compute row number - note C = 0 clr C ; no error ret ;=========================================================================================== ;subroutine FindColumn ;returns the column number [0 - 3] of the key pressed in the accumulator ; ; input : Port 3 bits are connected to a 4 by 4 ; matrix type keypad. Bits P3.0 through P3.3 are ; connected to the rows, and P3.4 through P3.7 are ; connected to the columns. ; ; output : the row number of the key pressed is returned in a. ; the carry flag is set if a key is pressed, else ; the carry flag is cleared. ; ; calls : nothing ; destroys : a, r0, C ;=========================================================================================== FindColumn: mov r0, #4 ; initialize counter (R0) mov a, #0EFh ; initialize accumulator... push acc ; ...and save on stack TryNextColumn: pop acc mov P3, a rl a push acc mov a, P0 orl a, #0F0h cpl a jnz ColumnFound djnz r0, TryNextColumn pop acc ; restore stack setb C ; siganl an error ret ColumnFound: pop acc ; resore stack mov a, #4 ; number of rows clr C ; get ready for the SUBB instruction subb a, r0 ; compute column number clr C ; no error present: inc a movc a, @a+pc ret db 0c0h ;0 db 0f9h ;1 db 0a4h ;2 db 0b0h ;3 db 99h ;4 db 92h ;5 db 82h ;6 db 0f8h ;7 db 80h ;8 db 90h ;9 db 88h ;a db 83h ;b db 0c6h ;c db 0a1h ;d db 86h ;e db 8eh ;f ;=========================================================================================== ;subroutine DisplayKey ;sends a message to the host reporting the row and column ;number of the key pressed ; ; input : B register holds the column and row numbers in its ; nibbles in BCD ; ; output : a message is sent to the host ; ; calls : prthex, crlf ; destroys : a, r0, r2 ; ;=========================================================================================== DisplayKey: lcall print db 'Key Pressed - column and row:', 0 mov a, b lcall prthex lcall crlf ret ;=========================================================================================== ;subroutine ConvertKey ;converts the key row and column of a 4 by 4 matrix type keypad ;to a hexadecimal number ; ; input : B register holds the column and row numbers in its ; nibbles in BCD. ; ; output : the hex number [0 - F] is returned in the accumulator. ; ; calls : nothing ; destroys : a ; ;=========================================================================================== ConvertKey: mov a,b ; recall column and row rl a ; make the row number the more significant rl a ; two bits mov C, acc.6 ; copy column number low bit to... mov acc.0, C ; ...acc.0 mov C, acc.7 ; copy column number high bit to... mov acc.1,C ; mask off accumulator high nibble ret END Thanking you in advance Michael Morris & Mark Moran |
| Topic | Author | Date |
| Keypad issues | 01/01/70 00:00 | |
| RE: Keypad issues | 01/01/70 00:00 | |
| RE: Keypad issues | 01/01/70 00:00 | |
| Debouncing Keypads | 01/01/70 00:00 | |
| RE: Keypad issues | 01/01/70 00:00 | |
| RE: Keypad issues | 01/01/70 00:00 | |
| RE: Keypad issues | 01/01/70 00:00 | |
| RE: Keypad issues | 01/01/70 00:00 | |
| RE: Keypad issues | 01/01/70 00:00 | |
| RE: Keypad issues | 01/01/70 00:00 | |
| RE: Keypad issues | 01/01/70 00:00 | |
| RE: Keypad issues | 01/01/70 00:00 | |
| RE: Keypad issues | 01/01/70 00:00 | |
| RE: Keypad issues | 01/01/70 00:00 | |
| RE: Keypad issues | 01/01/70 00:00 | |
| RE: Keypad issues | 01/01/70 00:00 | |
| RE: Keypad issues | 01/01/70 00:00 | |
| RE: Keypad issues | 01/01/70 00:00 | |
| Keypad issues | 01/01/70 00:00 | |
RE: Debounced switch | 01/01/70 00:00 |



