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

📄 main.asm

📁 一款智能老鼠的玩具源代码
💻 ASM
字号:
;****************************************************************;
; MOUSE-hourse
; Version 1.0
;
; MCU Body: KB 89C21(89R21)
; ROM size: 16K Bytes
; RAM size: 512 Bytes
; Interrupt: three timer and two external interrupt
; Clock: Dual,Fast,Slow clock (4M, set temporarily)
; Operating Mode: Dual,Fast,Slow,Idle,Sleep
; Built-in WATCH DOG TIMER
; 16 bi-directional I/O pins
;; Written by: lixy
; Date: 2004-02-16 
; chagen by :he qingrang
; date: 2004-06-25
;****************************************************************;

;================================================================;
;CONSTANT AREA
;================================================================;
c_sp		equ 	#0efh		;the sp address
c_ram		equ	#0ffh		;the maximum ram size
c_t1h		equ	#00h		;set timer1 frequency 8KHz,(slow clock)32K/8K-1=3=0003h
c_t1l		equ	#03h     
c_t2h		equ	#22h		;set timer2 frequency 300Hz,(fast clock)4M/1.5/300-1=8888=22b8h
c_t2l		equ	#0b8h
c_op1		equ	#00011100b	;R_OP1: bit7:DRDY, bit6:STOP, bit5:SLOW, bit4:INTE
					;       bit3:T2E,  bit2:T1E,  bit1:Z,    bit0:C
;c_ier		equ	#00110b		;R_IER: bit4:TB,   bit3:INT1, bit2:T1,   bit1:T2,  bit0:INT2
c_ier		equ	#00111b		;R_IER: bit4:TB,   bit3:INT1, bit2:T1,   bit1:T2,  bit0:INT2
c_ier_en_t1	equ	#00100b		;R_IER: bit4:TB,   bit3:INT1, bit2:T1,   bit1:T2,  bit0:INT2
c_ier_en_t2	equ	#00010b		;R_IER: bit4:TB,   bit3:INT1, bit2:T1,   bit1:T2,  bit0:INT2
c_ier_en_int2	equ	#00001b		;R_IER: bit4:TB,   bit3:INT1, bit2:T1,   bit1:T2,  bit0:INT2
c_init_point_h	equ	#03h
c_init_point_l	equ	#0e7h           ;999=03e7H
c_fail_loop	equ	#5		;when fail,play fail voice and lcd flash times
c_point_dec_intvl	equ	#120	;120/300=400ms
c_play_v_normal_intvl	equ	#6     ;(c_point_dec_intvl/300)*c_play_voice_intvl
c_play_v_fast_intvl	equ	#1
c_fast_voice		equ	#50	;<255 and >50,fast voice
c_over_sound_rum	equ	#100	;
;================================================================;
;DATA AREA
;================================================================;
	.area	main_var(data)

flg_new_random::	.ds	1	;the flag of generating new random number
                                        ;"0" not generate new random number
                                        ;"1" generate new random number in free movement course 
                                        ;"2" generate new random number when received IR
cnt_point::		.ds	2       ;point counter,it will decrease 1 each interval
cnt_fail_loop::		.ds	1
cnt_play_v_intvl::	.ds	1	;play voice interval,(c_point_dec_intvl/300)*cnt_play_v_intvl,
cnt_over_sound_rum::	.ds	1	;play gameover song times
;================================================================;
;CODE AREA
;================================================================;
	.area	main_code(code,abs)
	.org	0x00
	br	start_initial
	.org	0x06			;Int1(prtd.6) entry
	br	int1_isr
	.org	0x09			;Timer1 entry
	br	t1_isr
	.org	0x0c			;Timer2 entry
	br	t2_isr
	.org	0x0f			;Int2(prtd.7) entry
	br	INT2_isr
	.org	0x12			;Time base entry
	br	tb_isr

int1_isr:
;t1_isr:
;t2_isr:
;int2_isr:
tb_isr:
	reti
;----------------------------------------------------------------;
;Start Initial
;----------------------------------------------------------------;
start_initial:
	lda	#c_sp			;set stack pointer
	sta	r_sp
	lda	#11111111b
	sta	r_prtd			;set i/o prtd
	lda	#11111111b		;set prtc[0,1,2] "0" when initial, IR driver circuit worked at high level.
	sta	r_prtc			;set i/o prtc
clear_memory:				;clear sram(page0)其中包括显存(f0h-ffh)
	lda	#00h
	sta	r_dp
clr_m:
	lda	#00h
	sta	i
	lda	r_dp
	cmpe	#c_ram
	brz	clr_end
	incdp
	br	clr_m
clr_end:
;----------------------------------------------------------------;
;Set Timer1 Frequency Subroutine
;----------------------------------------------------------------;
	lda	#c_t1h			;set timer1
	sta	r_t1h
	lda	#c_t1l
	sta	r_t1l
	lda	#c_t2h			;set timer2
	sta	r_t2h
	lda	#c_t2l
	sta	r_t2l
;Initial Registor Subroutine
;----------------------------------------------------------------;
	lda	#00h
	sta	r_tpp
	sta	r_tph
	sta	r_tpl
	sta	r_pp
	sta	r_voc
	sta	r_pwmc
	lda	#00000000b		;set normal mode,close int,t1,t2,tb
	sta	r_op1
	sta	r_op2
	lda	#00000b			;disable all interrupt
	sta	r_ier
;	lda	#01h
;	sta	r_lcdc			;点亮LCD
	clrint
  	lda	#c_init_point_h         ;lcd display initial "999"
	sta	cnt_point+1
	lda	#c_init_point_l
	sta	cnt_point
	lda	#c_op1		;#00011100b,normal mode, open interrupt,enable timer1
	sta	r_op1		; R_OP1: bit7:DRDY, bit6:STOP, bit5:SLOW, bit4:INTE
	lda	#c_ier_en_t2		;enable timer2
	sta	r_ier
	ora	#c_ier_en_t1		;enable timer1
	sta	r_ier
	ora	#c_ier_en_int2		;enable int2
	sta	r_ier
;----------------------------------------------------------------;
;Wait for nose key pressed, if nose key is pressed and released,the mouse start run
;----------------------------------------------------------------;
	call	scan_key_start
main:
	clrwdt				;add Watch Dog function
;-----------------------------------------------------------------------
;judge if touched sign is received, if yes, the game will over
;-----------------------------------------------------------------------
	call	if_gameover
;calculating the points and display it,play the sound
	call	cal_point
;-----------------------------------------------------------------------
;scan stall,nose,trap key
	call	scan_key
;-----------------------------------------------------------------------
	call	Run
	br	main
	
;//***********************Game Over*************************************
if_gameover::
	lda	flg_rcv_touched
	cmpe	#1			;touched the cheese 
	brz	gameover
	ret
gameover:
	call	select_sound_gameover
	lda	#0
	sta	cnt_over_sound_rum
gameover_song:
	clrwdt
  	lda	#0ffh
        cmpe	M_stopbit
        brnz	gameover_song
        call	debounce
	call	select_sound_gameover
gameover_song1:
	clrwdt
 ; 	lda	#0ffh
 ;      cmpe	M_stopbit
 ;      brnz	gameover_song1
        lda	cnt_over_sound_rum
        inca
        sta	cnt_over_sound_rum
        cmpe	#c_over_sound_rum
        brnc	gameover_song
gameover_stop:
	lda	#11111111b		;stop all motors and ir tx
	sta	r_prtd			;set i/o prtd
  	sta	r_prtc			;set i/o prtc
	lda	#00h
	sta	r_voc
	sta	r_pwmc
	sta	status_ir_rcv   	;restore the receive status
;	lda	#00h
	sta	r_lcdc			;disable lcd display
;	lda	#00000b			;disable all interrupt
	sta	r_ier
	lda	#01000000b		;set sleep mode,close int,t1,t2,tb
	sta	r_op1
	nop
	nop
	nop
	nop
	br	gameover_stop
;-----------------------------------------------------------------------
;calculator point subroutie program,
;-----------------------------------------------------------------------
cal_point::
	lda	cnt_point_free 
	cmpe	#c_point_dec_intvl ;>400ms
	brc	cal_point_0        ;yes,jump
	ret
cal_point_0:
	lda	#0
	sta	cnt_point_free
	lda	cnt_play_v_intvl   ;play voice interval (c_point_dec_intvl*/300)*cnt_play_v_intvl
	inca
	sta	cnt_play_v_intvl	;the register is also used to fail loop times	
	lda	cnt_point
	suba	#1
	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)
	brz	play_voice_select
	brnc	mouse_fail      ;cy=0,cnt_point<0
	br	play_normal_voice  ;>255 ,play normal voice
	
play_voice_select:	
	call	lcd_display
	lda	cnt_point	;cnt_point+1=0
	ora	#0              ;judge z flag bit
	brz	mouse_fail      ;=0,seek food fail,
	lda	cnt_point
	cmpe	#c_fast_voice
	brc	play_fast_voice ;50< cnt_point<255
;play normal voice
play_normal_voice:
	lda	flg_rcv_broadcast 
	cmpe	#1
	brz	play_fast_voice	;receive the broadcast sign, fast voice	

	call	lcd_display
	lda	cnt_play_v_intvl
	cmpe	#c_play_v_normal_intvl
	brc	play_sound1
	ret
play_fast_voice:
	lda	cnt_play_v_intvl
	cmpe	#c_play_v_fast_intvl
	brc    play_sound1
	ret
play_sound1:
	call	select_sound_normal
	lda	#0
	sta	cnt_play_v_intvl
	ret
mouse_fail::
	call	lcd_display
	lda	#0ffh
	sta	r_prtd		;stop motors
	sta	r_prtc		;stop transmit ir
	lda	#0
	sta	status_ir_transmit ;stop transmit
	sta	cnt_point_free	   ;timer 255/300 s
	call	select_sound_fail
	lda	#c_fail_loop
	sta	cnt_fail_loop
Play_fail_delay:
	clrwdt
        call	lcd_display
	lda	cnt_point_free
	cmpe	#255              ;delay 255/300s
	brnz	play_fail_delay
	sta	cnt_point_free
play_fail_loop:
	clrwdt
	lda	#0
	sta	r_lcdc		;disable lcd display
	lda	cnt_point_free
	cmpe	#255
	brnz	play_fail_loop
	lda	#0
	sta	cnt_point_free	   ;timer 255/300 s
	call	select_sound_fail
 	lda	cnt_fail_loop
 	deca
 	sta	cnt_fail_loop
 	brc	play_fail_delay     ;if>0,c=1
 	br	gameover_stop

⌨️ 快捷键说明

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