; keytest.asm ; ; 6-19-2001 Doug Ricket ; Microcontroller Project Lab ; Project 2, Part 1 ; ; This program reads from a keypad on port 1 and ; prints any keys pressed to the serial port. ; main: acall serialinit mainloop: acall get_char acall sndchr sjmp mainloop ; KEYPAY PIN DEFINITIONS keyport equ p1 col1 equ p1.7 col2 equ p1.6 col3 equ p1.5 col4 equ p1.4 row1 equ p1.3 row2 equ p1.2 row3 equ p1.1 row4 equ p1.0 ; ************************* ; Function: check_key ; ; Checks for a key from a 4x4 matrix keypad ; Turns off each row, tests to see if any columns go off ; If so, a key is found, and the key number 1-16 is stored in A ; Otherwise, 0 is stored in A ; ************************* check_key: mov keyport, #0FFh ; ready for input clr row1 ; Row 1 mov a, #01h jnb col1, keyfound ; If col1 went low, then it connects to row1 mov a, #02h jnb col2, keyfound mov a, #03h jnb col3, keyfound mov a, #04h jnb col4, keyfound setb row1 clr row2 ; Row 2 mov a, #05h jnb col1, keyfound mov a, #06h jnb col2, keyfound mov a, #07h jnb col3, keyfound mov a, #08h jnb col4, keyfound setb row2 clr row3 ; Row 3 mov a, #09h jnb col1, keyfound mov a, #0Ah jnb col2, keyfound mov a, #0Bh jnb col3, keyfound mov a, #0Ch jnb col4, keyfound setb row3 clr row4 ; Row 4 mov a, #0Dh jnb col1, keyfound mov a, #0Eh jnb col2, keyfound mov a, #0Fh jnb col3, keyfound mov a, #10h jnb col4, keyfound setb row4 keynotfound: mov a, #00h ; zero = no key pressed ret keyfound: ; a holds 1-16 ret ; ************************ ; Function: key_to_ascii ; ; Takes a key index 1-16 in A ; Returns the ascii code corresponding to that key in A ; ************************ key_to_ascii: ; Convert key code to ascii char movc a, @a+pc ; Lookup from the following table ret db '1', '2', '3', 'A' db '4', '5', '6', 'B' db '7', '8', '9', 'C' db '*', '0', '#', 'D' ; ************************* ; wait_for_key_done ; ; Loops until no key is pressed ; ************************* wait_for_key_done: push acc ; don't disturb the value in A wait_for_key_done_loop: acall check_key jnz wait_for_key_done_loop pop acc ret ; ******************** ; get_key ; ; Waits for a key to be pressed, ; Gets the key into A, ; Waits for the key to be released ; ******************** get_key: acall check_key jz get_key ; key == 0 means no key, so try again acall wait_for_key_done ret ; ************** ; get_char ; ; Waits for key press and release ; Returns ascii character for key ; ************** get_char: acall get_key acall key_to_ascii ret ;================================================================= ; subroutine init ; this routine initializes the hardware ;================================================================= serialinit: ; set up serial port with a 11.0592 MHz crystal, ; use timer 1 for 9600 baud serial communications (19200 for RD2) mov tmod, #20h ; set timer 1 for auto reload - mode 2 mov tcon, #41h ; run counter 1 and set edge trig ints mov th1, #0fdh ; set 9600 baud with xtal=11.059mhz mov scon, #50h ; set serial control reg for 8 bit data ; and mode 1 ret ;=============================================================== ; subroutine sndchr ; this routine takes the chr in the acc and sends it out the ; serial port. ;=============================================================== sndchr: clr scon.1 ; clear the tx buffer full flag. mov sbuf,a ; put chr in sbuf txloop: jnb scon.1, txloop ; wait till chr is sent ret