*
* Kpad4x4.asm - A simple keypad routine for the MiniDRAGON+ Rev. D board
*               (C)2005, written By Wayne Chu
*
*     Function: It scans a keypad and outputs the key number on 7-segment
*
*     Instructions:
*     If you have a 4 x 4 keypad, connect it to the keypad header J22.
*     The middle 8 pins of the 10-pin keypad cable are keypad connections.
*     The pin 1 and pin 10 are not used.
*
*	   If you press any key on the keypad, the 7-segment display should
*          show the key number.
*
;
; There are two different keypads have been used with Wytec development boards.
; The following signal definitions only apply to Grayhill 4X4 keypad:

; PA0 connects ROW0 of the keypad via pin 1 of the 8-pin keypad header J22
; PA1 connects ROW1 of the keypad via pin 2 of the 8-pin keypad header J22
; PA2 connects ROW2 of the keypad via pin 3 of the 8-pin keypad header J22
; PA3 connects ROW3 of the keypad via pin 4 of the 8-pin keypad header J22

; PA4 connects COL0 of the keypad via pin 5 of the 8-pin keypad header J22
; PA5 connects COL1 of the keypad via pin 6 of the 8-pin keypad header J22
; PA6 connects COL2 of the keypad via pin 7 of the 8-pin keypad header J22
; PA7 connects COL3 of the keypad via pin 8 of the 8-pin keypad header J22

; The PA0-PA7 has a 100K pull-up resistor in each line.

; The following signal definitions only apply to Wytec 4X4 membrane keypad:

; PA0 connects COL0 of the keypad via pin 1 of the 8-pin keypad header J22
; PA1 connects COL1 of the keypad via pin 2 of the 8-pin keypad header J22
; PA2 connects COL2 of the keypad via pin 3 of the 8-pin keypad header J22
; PA3 connects COL3 of the keypad via pin 4 of the 8-pin keypad header J22

; PA4 connects ROW0 of the keypad via pin 5 of the 8-pin keypad header J22
; PA5 connects ROW1 of the keypad via pin 6 of the 8-pin keypad header J22
; PA6 connects ROW2 of the keypad via pin 7 of the 8-pin keypad header J22
; PA7 connects ROW3 of the keypad via pin 8 of the 8-pin keypad header J22

; The PA0-PA7 has an internal pull-up resistor on each line.

; To use the Grayhill keypad, remark the label Wytec_keypad and un-remark
; the label Grayhill_keypad

Wytec_keypad:		equ     1       ; use Wytec_keypad
;Grayhill_keypad:        equ     1      ; not to use Grayhill_keypad

#include        reg9s12.h	; include register equates
;
REGBLK:		equ	$0

		org	$1000
;
temp:		rmb	1

STACK:		equ	$2000

	org	$2000
;
	jmp	start
	
;
; Segment conversion table:

; Binary number:	 	0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
; Converted to 7-segment char: 	0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
;
; Binary number:		$10,$11,$12,$13,$14,$15,$16,$17
; Converted to 7-segment char:   G   H   h   J   L   n   o   o
;
; Binary number:  		$18,$19,$1A,$1B,$1C,$1D,$1E,$1F,$20
; Converted to 7-segment char:   P   r   t   U   u   y   _  --  Blank
segm_ptrn:						; segment pattern
	fcb     $3f,$06,$5b,$4f,$66,$6d,$7d,$07		;0-7
;		 0,  1,  2,  3,  4,  5,  6,  7
        fcb     $7f,$6f,$77,$7c,$39,$5e,$79,$71		;8-$0f
;		 8,  9,  A,  b,  C,  d,  E,  F
       	fcb     $3d,$76,$74,$1e,$38,$54,$63,$5c		;10-17
;		 G,  H,  h,  J   L   n   o   o
       	fcb     $73,$50,$78,$3e,$1c,$6e,$08,$40		;18-1f
;		 P,  r,  t,  U,  u   Y   -   -
       	fcb     $00,$01,$48,$41,$09,$49			;20-23
;		blk, -,  =,  =,  =,  =
;
seven_segment:
	pshx
	pshb
	ldx	#segm_ptrn
	psha
	anda	#$3f
	tab
	abx
	ldaa	0,x		; get segment
	pulb
	andb	#$80		; add DP
	aba
	pulb
	pulx
	rts
;
;
#ifdef  Wytec_keypad
; keypad scan for Wytec 4X4 membrane keypad (pin 1-4 = col 0-3, pin 5-8 = row 0-3)
; at exit:  if a key is down, the carry bit =1 and the accumulator b
;	    holds the key number
;	    if no key is dwon the carry bit =0

keypad:
;	ldaa 	#00001111b
	ldaa 	#$0F		; pa0-pa3 are outputs, pa4-pa7 are inputs
	staa 	ddra

	ldab	#15
;	ldaa	#11110111b
	ldaa	#$F7		; pa3=low, pa0-pa2=high
	staa	temp		; save it at temp
next_row:
	staa	porta
	ldaa	#10		; add delay before checking key down
k_dly:	deca
	bne	k_dly

	ldaa	porta
	anda	#$F0		; only read 4 MSBs pa4-pa7
	cmpa	#$F0
	bne	keyin		; a key is pressed
	decb
	cmpb	#11
	beq	no_keyin	; after 4 tests, accu B will be 11

 	ror	temp
	ldaa	temp
	jmp	next_row

no_keyin:
	clc
	rts			; no key input

keyin:	rola
 	bcc	key_ok
 	subb    #4
 	jmp     keyin
key_ok:	sec
	rts
#endif
;

#ifdef  Grayhill_keypad
;
; keypad scan for Grayhill 4x4 keypad (pin 1-4 = row 0-3, pin 5-8 = col 0-3)
; at exit:  if a key is down, the carry bit =1 and the accumulator b
;	    holds the key number
;	    if no key is dwon the carry bit =0

keypad:
;	ldaa 	#00001111b
	ldaa 	#$0F		; pa0-pa3 are outputs, pa4-pa7 are inputs
	staa 	ddra

	ldab	#16
;	ldaa	#11110111b
	ldaa	#$F7		; pa3=low, pa0-pa2=high
	staa	temp		; save it at temp
next_row:
	staa	porta
	ldaa	#10		; add delay before checking key down
k_dly:	deca
	bne	k_dly

	ldaa	porta
	anda	#$F0		; only read 4 MSBs pa4-pa7
	cmpa	#$F0
	bne	keyin		; a key is pressed
	subb	#4
	beq	no_keyin	; after 4 tests, accu B will be 0

	ror	temp
	ldaa	temp
	jmp	next_row

no_keyin:
	clc
	rts			; no key input

keyin:	decb
	rola
	bcs	keyin
	sec
	rts
#endif

delay_10ms:
	psha
	ldaa    #10
dly_10ms:
	jsr     delay_1ms
	deca
	bne 	dly_10ms
	pula
	rts
	
delay_1ms:
	pshx
	ldx     #3000		; 3000 x 8 = 24,000 cycles = 1ms
del1:	dex			; 1 cycle
	inx			; 1 cycle
	dex			; 1 cycle
        inx			; 1 cycle
	dex			; 1 cycle
	bne	del1		; 3 cycles
	pulx
       	rts

start:	lds	#STACK
 	ldx	#REGBLK
 	ldaa	#$ff
	staa	ddrh,x		; porth = output
	clr	pth,x		; turn off LED display
	bset	pucr,x,1	; enable pullup on portA
;
begin:
        jsr 	keypad
	bcc	nokey
   	jsr	delay_10ms
	jsr 	keypad
	bcc	nokey
; key down
        
not_zero:
	tba
	jsr	seven_segment	; convert Accu A to segment pattern, no DP
	staa	pth,x
	jmp	begin
;
nokey:  jsr	delay_10ms
        jmp	begin
;
	end
