⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 key_scan.asm

📁 智能玩具实际文件
💻 ASM
字号:
;****************************************************************;
; Key Scan Function
;
; Key input pin: PRTD[7]
; Include two part: scan key when power on; 
;                   scan key in the course of movement
; Written by: lixy
; Date: 2004-02-16
;****************************************************************;

;================================================================;
;Constant Area
;================================================================;
c_prtd0_1		equ	#00000001b
c_prtd0_0		equ	#11111110b
c_offset_f_ini		equ	#0
c_rnd_range_f_ini	equ	#8
c_offset_stop		equ	#232		;=29(row)*8(colunm)
c_rnd_range_stop	equ	#0
c_dly_dbc_h		equ	#0ch		;key pressed debounce delay constant


;================================================================;
;Data Area
;================================================================;
       .area       key_scan_var(data)

;flg_key_pressed_1st::	.ds	1	;the flag of key pressed for the first time
;flg_key_ok::		.ds	1	;the flag of key pressed and released the first times when power on
;flg_key_scan::		.ds	1
;flg_key_pressed::	.ds	1	;the flag of key pressed
temp_dly_dbc::		.ds	2	;key press debounce delay time variable
flg_nose_pressed::	.ds	1	;if it be set,not decrease 50 point untill release the nose key
flg_touched_key::	.ds	1	;0:this key isn't pressed
					;1:the key is pressed with broadcast ir received,motor will stop,wait for touched ir sign
					;2:touched if sign isn't received within 4s,will decrease 50point,then be clear 
;================================================================;
;Code Area
;================================================================;
       .area       scan_key_code(code)
;----------------------------------------------------------------;
; Key Scan (for starting when power on)
;----------------------------------------------------------------;
scan_key_start::
	clrwdt
	call	lcd_display		;initial value is set in initial part of main program
;--------------/*04-02-17*/-----------------------;
;random number initial value counter keep counting,	
;until key is pressed and released when powered on.
	lda     cnt_rnd_num_ini
	inca
	sta	cnt_rnd_num_ini
	lda	cnt_rnd_num_ini+1
	addc	#00h
	sta	cnt_rnd_num_ini+1
;-------------------------------------------------;
	lda	#0ffh		;stop motor and set prtd0,1,2 7 to input pin
	sta	r_prtd
	nop
	nop
	lda	r_prtd
	anda	#c_prtd0_1       ;if nose key is pressed,z=0
	brnz	scan_key_start
	lda	#0ffh
	sta	r_prtd
	call	debounce	;key pressed debouncing
	lda	r_prtd
	anda	#c_prtd0_1
	brnz	scan_key_start 	;key invalid,re-scan
waitkey_release:
	clrwdt
	lda	r_prtd
	ora	#00000111b	;d0 is nose,d1 is trap,d2 is stall
	sta	r_prtd
	nop
	nop
	lda	r_prtd		;key valid,wait for key release
	anda	#c_prtd0_1
	brz	waitkey_release
	call	debounce
	lda	r_prtd
	ora	#00000111b	;d0 is nose,d1 is trap,d2 is stall
	sta	r_prtd
	nop
	nop
	lda	r_prtd
	anda	#c_prtd0_1
	brz	waitkey_release
	lda	cnt_rnd_num_ini+1	;load random initial value(high 8bit)装载随机数的初值
 	sta	rnd_num_16+1
 	lda	cnt_rnd_num_ini		;load random initial value(low 8bit)
	sta	rnd_num_16
 	call	generate_random
	lda	#0
	sta	cnt_point_free
;	sta	flg_rcv_barrier		;clear receive barrier flag 
	lda	#1
	sta	flg_new_random		;need generate new random(turn left,turn right,go ahead)
	call 	select_sound_normal
 	ret
;----------------------------------------------------------------;
; Key Scan (for stopping in the course of the movement)
;----------------------------------------------------------------;
scan_key::
	lda	flg_touched_key
	cmpe	#2		;4s after touched key,touched ir sign isn't received,the flg is 2
	brz	decrease_50point ;shall decrease 50 points(nose key)
	lda	r_prtd
	ora	#10000111b	;d0 is nose,d1 is trap,d2 is stall
	sta	r_prtd
	nop
	nop
	nop
	lda	r_prtd
	anda	#00000111b
	cmpe	#00000111b
	brnz	if_key_pressed
	lda	#0
	sta	flg_nose_pressed	;clear the nose key pressed flag
	ret			;no any key is pressed
if_key_pressed:
	call debounce
	lda	r_prtd
	ora	#10000111b	;d0 is nose,d1 is trap,d2 is stall
	sta	r_prtd
	nop
	nop
	lda	r_prtd
	anda	#00000111b
	cmpe	#00000100b	;stall,Acc>0100b,c=1
	brnc	stall_motor
	anda	#00000011b
	cmpe	#00000010b	;trap
	brnc	entry_trap
	anda	#00000001b
	brz	nose_pressed
	ret
entry_trap:
	lda	#0
	sta	cnt_point
	sta	cnt_point+1
	call 	lcd_display
	br	mouse_fail	
stall_motor:
	anda	#00000010b
	brz	entry_trap
nose_pressed:
	lda	flg_rcv_broadcast	;if broadcast sign is received,
	cmpe	#1
	brz	cheese_touched_key
	lda	flg_nose_pressed
	cmpe	#1
	brz	if_nose_released
decrease_50point:
	lda	#0
	sta	flg_touched_key
	lda	cnt_point
	suba	#50             ;barrier
	sta	cnt_point
	lda	cnt_point+1      
	subc	#0
	sta	cnt_point+1     ;(cnt_point+1)cnt_point decrease 1,if the result>=0,c=1(Note)
	brnc	entry_trap   ;cy=0,cnt_point<0,die
	call	select_sound_barrier
if_nose_released:
	lda	#0
	sta	flg_touched_key
	call	lcd_display
	lda	r_prtd
	ora	#10000001b	;d0 is nose,d1 is trap,d2 is stall
	sta	r_prtd
	nop
	nop
	lda	r_prtd 		;judge if release the nose key
	anda	#00000001b	;if =0(z=1),nose released,clear flg_nose_pressed
	brnz	nose_released
	lda	#1		;nose key isn't released
	sta	flg_nose_pressed
nose_released:
	lda	#1
	sta	flg_rcv_barrier
	sta	flg_last_barrier  ;there is barrier at the front,
	call	Run_backup
 	ret
   
cheese_touched_key:		;wait for cheese touched ir sign
	lda	#1
	sta	flg_touched_key ;note:key value =2,is the key is wrong regarded as the wrong key.
		                ;it shall receive the touched ir sign,if not,it is wrong.
	lda	#11111111b	;motor will stop(r_prtd3,4,5,6, other port are input)
	sta 	r_prtd
	ret
;----------------------------------------------------------------;
;Debounce Subroutine
;----------------------------------------------------------------;
debounce::				;key pressed debounce time = 18*256*(#cnst_dly_dbc_h)*1.5/4M = 20.16ms
	lda	#00h
	sta	temp_dly_dbc
	sta	temp_dly_dbc+1
delay_debounce:
;	clrwdt
	adda	#00h			;clear c
	lda	temp_dly_dbc
	inca
	sta	temp_dly_dbc
	lda	#00h
	addc	temp_dly_dbc+1
	sta	temp_dly_dbc+1
	cmpe	#c_dly_dbc_h
	brnz 	delay_debounce
debounce_end:
        ret

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -