// 用stc89c52rc單片機和一蜂鳴器制作:
;蘭花草音樂播放程序**************************c51匯編語言
org 00h
jmp start
org 0bh
jmp tim0
start:;JB P3.7,$ ;檢測該口的按鍵是否按下?
mov tmod,#01h
mov ie,#82h
start0:mov 30h,#00
next:mov a,30h
mov dptr,#table
movc a,@a+dptr
mov r2,a
jz end0
anl a,#0fh
mov r5,a
mov a,r2
swap a
anl a,#0fh
jnz sing
clR tr0
jmp d1
sing:dec a
mov 22h,a
rl a
mov dptr,#table1
movc a,@a+dptr
mov th0,a
mov 21h,a
mov a,22h
rl a
inc a
movc a,@a+dptr
mov tl0,a
mov 20h,a
setb tr0
d1:call delay
inc 30h
jmp next
end0:clr tr0
jmp start0
tim0:push acc
push psw
mov tl0,20h
MOV th0,21h
cpl P1.6 ;由該口輸出音頻數(shù)據(jù);可以更改口線
POP PSW
pop acc
reti
delay:mov r7,#02
d2:mov r4,#125
d3:mov r3,#248
djnz r3,$
djnz r4,d3
djnz r7,d2
djnz r5,delay
ret
table1:
dw 64021,64103,64260,64400
dw 64524,64580,64684,64777
dw 64820,64898,64968,65030
dw 64934
table:db 42h,82h,82h,82h,84h,02h,72h
db 62h,72h,62h,52h,48h
db 0b2h,0b2h,0b2h,0b2h,0b4h,02h,0a2h
db 12h,0a2h,0d2h,92h,88h
db 82h,0b2h,0b2h,0a2h,84h,02h,72h
db 62h,72h,62h,52h,44h,02h,12h
db 12h,62h,62h,52h,44h,02h,82h
db 72h,62h,52h,32h,48h
db 00h
end
*************************************************************************
***************************************c51語言編寫對照
#include"reg52.h"
#define uint unsigned int
#define uchar unsigned char
sbit speaker=P1^0;
uint a;
uint code table1[]={2,2,2,2,4,2,2,
2,2,2,2,8,
2,2,2,2,4,2,2,
2,2,2,2,8,
2,2,2,2,4,2,2,
2,2,2,2,4,2,2,
2,2,2,2,4,2,2,
2,2,2,2,8 };
uint code table2[]={0x04,0x08,0x08,0x08,0x08,0x02,0x07,
0x06,0x07,0x06,0x05,0x04,
0x0b,0x0b,0x0b,0x0b,0x0b,0,0x0a,
0x01,0x0a,0x0d,0x09,0x08,
0x08,0x0b,0x0b,0x0a,0x08,0,0x07,
0x06,0x07,0x06,0x05,0x04,0,0x01,
0x01,0x06,0x06,0x05,0x04,0,0x08,
0x07,0x06,0x05,0x03,0x04 };
uchar code table3[]={64021,64103,64260,64400,
64524,64580,64684,64777,
64820,64898,64968,65030,
64934 };
void rhythm(uchar y)
{
TH0=(65536-y)/256;
TL0=(65536-y)%256;
TR0=1;//開啟定時器0
}
void init()
{
TMOD=0x01;
EA=1;
ET0=1;
}
void timer0 ()interrupt 1
{
speaker=1;
}
void delay(uint z,uchar temp)
{
uint x,j,k;
for(x=z;x>=0;x--)
{
for(j=125;j>0;j--)
{
for(k=250;k>0;k--);
rhythm(temp);
speaker=0;
}
}
}
void main()
{
while(1)
{
init();
for(a=0;a<60;a++)
{
delay(table1[a],table3[table2[a]]);
}
}
}
|