www.pudn.com > playmusic.zip > 单片机演奏音乐实例.asm, change:2007-06-23,size:3199b


;----------------------------------- 
;                   单片机演奏音乐实例 
;功能:演奏歌曲"祝你平安" 
;----------------------------------- 
BUZZER_PIN		EQU		P0.0  
TIME_COUNT		DATA	30H 
 
ORG	0000H				;伪指令,指定程序从0000H开始存放 
LJMP	MAIN				;跳转指令,程序跳转到MAIN处 
ORG	000BH				;伪指令,指定程序从T0入口地址000BH开始存放 
LJMP	T0_INT				;跳转指令,程序跳转到T0_INT处 
 
MAIN: 
	MOV	SP,#60H			;给堆栈指针赋初值 
	MOV	TH0,#0D8H		;设定定时器T0的初始值TH0 
	MOV	TL0,#0EFH		;设定定时器T0的初始值TL0 
	MOV	TMOD,#01H 		;定时器T0工作在方式1 
	MOV	IE,#82H			;中断允许 
MUSIC0: 
	MOV	DPTR,#MUSIC_TABLE		;表头地址传送到DPTR 
	MOV	TIME_COUNT,#00H			;中断计数器清0 
	MOV	B,#00H					;表序号清0 
MUSIC1: 
	CLR		A 
	MOVC	A,@A+DPTR				;查表取代码 
	JZ		END0					;是00H,则结束 
	CJNE	A,#0FFH,MUSIC5 
	LJMP	MUSIC3 
MUSIC5: 
	MOV	R6,A 
	INC		DPTR 
	MOV	A,B 
	MOVC	A,@A+DPTR				;取节拍代码送R7 
	MOV	R7,A 
	SETB	TR0						;启动计数 
MUSIC2: 
	CPL		BUZZER_PIN				; 
	MOV	A,R6 
	MOV	R3,A 
	LCALL	DELAY 
	MOV	A,R7 
	CJNE	A,TIME_COUNT,MUSIC2		;中断计数器(20H)=R7否? 
	;不相等,则继续循环 
	MOV	TIME_COUNT,#00H			;等于,则取下一代码 
	INC		DPTR 
	LJMP	MUSIC1 
MUSIC3: 
	CLR		TR0						;休止100毫秒 
	MOV	R2,#0DH 
MUSIC4: 
	MOV	R3,#0FFH 
	LCALL	DELAY 
	DJNZ	R2,MUSIC4 
	INC		DPTR 
	LJMP	MUSIC1 
END0: 
	MOV	R2,#64H					;歌曲结束,延时1秒后继续 
MUSIC6: 
	MOV	R3,#00H 
	LCALL	DELAY 
	DJNZ	R2,MUSIC6 
	LJMP	MUSIC0 
 
T0_INT: 
;--------------------------------- 
;     T0中断服务程序 
;12M晶振时,每10毫秒中断一次 
;---------------------------------     
	INC		TIME_COUNT				;中断计数器加1 
	MOV	TH0,#0D8H 
	MOV	TL0,#0EFH 
RETI 
 
DELAY: 
;--------------------------------- 
;     延时子程序 
;说明:延时时间通过R3传递,延时时间为(R3×20)uS 
;--------------------------------- 
	MOV	R4,#02H 
DELAY1: 
	NOP 
	DJNZ	R4,DELAY1 
	NOP 
	DJNZ	R3,DELAY 
RET 
 
MUSIC_TABLE: 
DB 026H,020H,020H,020H,020H,020H,026H,010H,020H,010H,020H,080H,026H,020H,030H,020H 
DB 030H,020H,039H,010H,030H,010H,030H,080H,026H,020H,020H,020H,020H,020H,01CH,020H 
DB 020H,080H,02BH,020H,026H,020H,020H,020H,02BH,010H,026H,010H,02BH,080H,026H,020H 
DB 030H,020H,030H,020H,039H,010H,026H,010H,026H,060H,040H,010H,039H,010H,026H,020H 
DB 030H,020H,030H,020H,039H,010H,026H,010H,026H,080H,026H,020H,02BH,010H,02BH,010H 
DB 02BH,020H,030H,010H,039H,010H,026H,010H,02BH,010H,02BH,020H,02BH,040H,040H,020H 
DB 020H,010H,020H,010H,02BH,010H,026H,030H,030H,080H,018H,020H,018H,020H,026H,020H 
DB 020H,020H,020H,040H,026H,020H,02BH,020H,030H,020H,030H,020H,01CH,020H,020H,020H 
DB 020H,080H,01CH,020H,01CH,020H,01CH,020H,030H,020H,030H,060H,039H,010H,030H,010H 
DB 020H,020H,02BH,010H,026H,010H,02BH,010H,026H,010H,026H,010H,02BH,010H,02BH,080H 
DB 018H,020H,018H,020H,026H,020H,020H,020H,020H,060H,026H,010H,02BH,020H,030H,020H 
DB 030H,020H,01CH,020H,020H,020H,020H,080H,026H,020H,030H,010H,030H,010H,030H,020H 
DB 039H,020H,026H,010H,02BH,010H,02BH,020H,02BH,040H,040H,010H,040H,010H,020H,010H 
DB 020H,010H,02BH,010H,026H,030H,030H,080H,000H 
 
END