; Summary: ASCII keypad interface 
; Target hardware: ATMEGA8515 

; AVR ports used: 
; PORT A: Output to the 6 keypad rows (R0-R5) and the shift key (tied to all rows) 
; PORT B: Input from the 4 keypad columns (C0-C3) and the shift key (C4) 
; PORT D: Output of the 7-bit ASCII keycode & 1-bit strobe 

; Description:    Each row is brought low in turn, and the columns are scanned for 
;                 a keypress. If a key is down, the shift key is also checked and 
;                 an offset is added to the keycode if shift is down. The row and 
;                 column of the pressed key are used to extract its ASCII value from 
;                 a table, and this value (plus a 40 ms strobe signal) is sent to 
;                 the outputs. 

; Limitations:    Correct strobe timing requires running at 8 mHz. 


;*** Included files *** 

.include "8515def.inc"     ; definitions file for ATMEGA8515 


;*** Definitions *** 

.def     temp = R16            ; scratch variable 
.def     temp2 = R17           ; scratch variable 
.def     row = R18             ; current row 
.def     keycode = R19         ; row/column value for a key 
.def     ASCII = R20           ; ASCII code for a key 
.def     lastkey = R21         ; previous key pressed 
.def   repeating = R22      ; autorepeating flag 
.def   repeattimer = R23   ; autorepeat loop counter 
.def   repeattimer2 = R24   ; second autorepeat loop counter 

.org     0x0000              ; place the program at the beginning of memory    

;*** Program entry point *** 

rjmp    Reset                ; jump to Reset 


;*** Reset *** 

; configure hardware ports and registers 

Reset: 

ldi       temp, high(RAMEND) 
out       SPH, temp 
ldi       temp, low(RAMEND)     ; set up stack pointer so we can return from rcalls 
out       SPL, temp 

ldi       temp,0xFF           ; configure port A pins as outputs to keypad rows 
out       DDRA,temp 

ldi       temp,0x00           ; configure port B pins as inputs from keypad columns        
out       DDRB,temp 

ldi       temp,0xFF            ; enable pull-up resistors on port B inputs 
out       PORTB,temp 

ldi       temp,0xFF           ; configure port D pins as outputs for ASCII code 
out       DDRD,temp           ; and strobe 

clr    repeating        ; make sure autorepeat counters are zeroed 
clr    repeattimer 
clr    repeattimer2 

; *** Loop *** 

; main program loop 

Loop: 
rcall     Scan_keypad         ; scan for keypress and store result in keycode 
cpi       keycode,0xFF        ; is keycode = 0xFF? if so, no key was pressed 
breq      store_lastkey        ; if no key pressed save 0xFF in lastkey and scan again 
cp        lastkey,keycode      ; same key pressed as last time? 
breq      autorepeat        ; if so, handle autorepeat 
clr       repeattimer         ; if not, clear the repeattimer 
clr       repeattimer2      ; and repeattimer2 
clr       repeating         ; and the repeat flag 

repeat_key:               ; loop re-entry point for autorepeating 
rcall    Get_ASCII         ; we have a new key so get its ASCII code 
ldi      temp,0x80          ; 10000000 = strobe bit 
add      ASCII,temp           ; add the strobe bit to the ASCII code 
out      PORTD,ASCII            ; output the ASCII keycode and the strobe 
ldi      temp, 0x27          ; 40ms delay at 80 mHz 
rcall    Strobe_delay        ; hold strobe 40 milliseconds 
ldi      temp, 0x80          ; reload the strobe bit into temp 
sub      ASCII, temp          ; remove the strobe bit from the ASCII code 
out      PORTD,ASCII             ; output the ASCII code without the strobe 

store_lastkey:    
mov       lastkey,keycode      ; save the keypad scan results 
rjmp      Loop                ; scan again 

autorepeat: 
cpi      repeating,0x01      ; are we already autorepeating? 
breq     repeat_delay      ; if so, jump down to the repeat delay       
inc      repeattimer         ; if not, increment the counter 
cpi      repeattimer, 0xFF   ; ready to increment repeattimer2? 
brne     Loop            ; if not, jump back to the start of the loop 
inc      repeattimer2      ; if so, do it 
cpi      repeattimer2, 0x80   ; ready to trigger autorepeating? 
brne     Loop            ; if not, jump back to the start of the loop 
ldi      repeating,0x01      ; if so, set the repeating flag 

repeat_delay: 
ldi      temp, 0xFF         ; autorepeat delay 
rcall    Strobe_delay      ; re-use the strobe delay loop :) 
ldi      temp, 0xFF         ; two times through seems about right 
rcall    Strobe_delay 
rjmp     repeat_key         ; output the key with autorepeat 


; *** Scan_keypad *** 

; scan each keypad row and column for a keypress and store the result in keycode 

Scan_keypad: 
ldi      row,0x00         ; start with R0 
ldi      keycode,0xFF        ; assume no keypress until proven otherwise 
ldi      temp2,0xFE            ; get ready to output 11111110 on PORTA to check R0 


scan_next_row: 
out      PORTA,temp2           ; bring the current row low on PORTA 
ldi      temp,0x14            ; give it time to get low 

short_wait: 
dec       temp 
brne      short_wait 

; one row has been brought low, so hunt for a low column input 

sbis      PINB,0x00            ; skip if column 0 is high 

ldi       keycode,0x00        ; if column 0 is low, store 0 in keycode 
sbis      PINB,0x01         ; skip if column 1 is high 
ldi       keycode,0x01      ; if column 1 is low, store 1 in keycode 
sbis      PINB,0x02         ; skip if column 2 is high 
ldi       keycode,0x02      ; if column 2 is low, store 2 in keycode 
sbis      PINB,0x03         ; skip if column 3 is high 
ldi       keycode,0x03      ; if column 3 is low, store 3 in keycode 

cpi       keycode,0xFF        ; was one of the columns low? 
brne      got_key             ; if so, handle it 

; if no column was low, bring the next row low and scan the columns again 

inc       row               ; get ready for the next row 
cpi       row,0x06         ; have we scanned all 6 rows yet? 
breq      scan_done            ; if so, we're done 
rol       temp2            ; if not, rotate the low output bit to the next row 

rjmp      scan_next_row 

got_key: 
ldi      temp,0x18         ; ASCII table offset for the shift key 
sbis     PINB,0x04          ; is the shift key down? 
add      keycode,temp      ; if so, add the shift offset to the keycode 
cpi      row,0x00         ; was the key in R0? 
breq     scan_done         ; if so, we don't need to add anything more to the code 
ldi      temp,0x04         ; otherwise, we add 4 for each row 

add_row_offset: 
add      keycode,temp      ; add 4 for each row 
dec      row 
cpi      row,0x00 

brne     add_row_offset 

rjmp     scan_done          

scan_done: 
ret 


; *** Strobe_delay *** 

Strobe_delay: 
ldi     temp2, 0xFF         ; perform a 40 ms loop at 8 mHz 

delay_loop: 
dec     temp2 
cpi     temp2, 0x00 
brne    delay_loop 
dec     temp 
cpi     temp, 0x00 
brne    Strobe_delay 
ret 


;*** Get_ASCII *** 

; translate the row/col code in keycode into an ASCII code and store in ASCII 

Get_ASCII: 

ldi     ZH,high(2 * ASCII_table) 
ldi     ZL,low(2 * ASCII_table) 
add     ZL,keycode          ; increase the table address based on keycode 
brcc    got_ascii            ; if we didn't overflow the low byte, we're done 
inc     ZH                 ; if we did, increment the high byte 

got_ascii: 
lpm                        ; move the byte addressed by the Z register into R0 
mov      ASCII,R0           ; copy R0 into ASCII 
ret 


; *** ASCII_table *** 

; contains the ASCII codes for the keys on the keypad 

ASCII_table: 

.db      0x20, '0', 0x5f, 0x00, '1', '2', '3', 0x0D 
.db      '4', '5', '6', '.', '7', '8', '9', ':' 
.db      'A', 'B', 'C', 'R', 'D', 'E', 'F', 'W' 
; beginning of shifted data 
.db      0x20, '0', 0x5f, 0x00, 'Y', 'Z', '$', 0x0D 
.db      'T', 'U', 'V', 'X', 'O', 'P', 'Q', 'S' 
.db      'K', 'L', 'M', 'N', 'G', 'H', 'I', 'J'