;《世上只有妈妈好》
51单片机汇编语言程序
;2007-5-22作者:徐学军
;注意有一个51单片机C语言程序与之对应
;此程序在硬件上调试通过
;本程序的单片机晶振采用11.0592MHZ
SPK BIT P1.7
ORG 0000H
LJMP MAIN
ORG 000BH
LJMP TIMER0
MAIN:
MOV TMOD,#01H ;置CT0定时工作方式1
MOV IE,#82H;CPU开中断,CT0开中断
MOV R5,#0
PLAY:
MOV A,R5
MOV DPTR,#TABLE1
MOVC A,@A+DPTR
MOVR6,A
INC R5
MOV A,R5
MOVC A,@A+DPTR
MOVR7,A
INC R5
MOV A,R5
MOVC A,@A+DPTR
JNZ PLAY1
MOV R5,#0
LJMP PLAY
PLAY1:
MOV B,#3
MUL AB
MOV R2,A
MOV B,#7
MOV A,R7
MUL AB
ADD A,R6
DEC A
MOV R6,A
MOV DPTR,#FREQH
MOVC A,@A+DPTR
MOV R4,A
MOV DPTR,#FREQL
MOV A,R6
MOVC A,@A+DPTR
MOV R3,A
LCALL SONG;调用音符播放程序
INC R5
SJMP PLAY;转入调用下一个音符OV R3,A
SONG:
MOV TH0,R4
MOV TL0,R3
SETB TR0;启动CT0(开始唱某一音符)
DELAY:
MOV R1,#85H;节拍延时(即某一音符唱多长时间)
DEL1:
MOV R0,#0FFH
DEL0:
NOP
NOP
NOP
DJNZ R0,DEL0
DJNZ R1,DEL1
DJNZ R2,DELAY
CLR TR0;禁止CT0(某一音符唱完后停止)
RET
TIMER0:;定时中断程序(用于决定某一音符唱多高)
CPL SPK
MOV TH0,R4
MOV TL0,R3
RETI
TABLE1:
DB6,2,3,5,2,1,3,2,2, 5,2,2, 1,3,2, 6,2,1, 5,2,1
DB6,2,4,3,2,2,5,2,1, 6,2,1, 5,2,2, 3,2,2, 1,2,1
DB6,1,1,5,2,1,3,2,1, 2,2,4, 2,2,3, 3,2,1, 5,2,2
DB5,2,1,6,2,1,3,2,2, 2,2,2, 1,2,4, 5,2,3, 3,2,1
DB2,2,1,1,2,1,6,1,1, 1,2,1, 5,1,6, 0,0,0
; 音阶频率表
FREQH:
DB0F2H,0F3H,0F5H,0F5H,0F6H,0F7H,0F8H
DB0F9H,0F9H,0FAH,0FAH,0FBH,0FBH,0FCH, 0FCH ;1,2,3,4,5,6,7,8,i
DB0FCH,0FDH,0FDH,0FDH,0FDH,0FEH
DB0FEH,0FEH,0FEH,0FEH,0FEH,0FEH,0FFH
FREQL:
DB42H, 0C1H, 17H, 0B6H,0D0H,0D1H,0B6H
DB21H, 0E1H, 8CH, 0D8H,68H, 0E9H, 5BH,8FH ;1,2,3,4,5,6,7,8,i
DB0EEH,44H,6BH,0B4H,0F4H,2DH
DB47H, 77H,0A2H,0B6H,0DAH,0FAH,16H
END