HUAN BIT 20H
ORG 0000H
JMP MAIN
ORG 000BH
MOV TL0,R2
MOV TH0,R3
CPL P2.0
ORG 001BH
DJNZ R4,GOON
MOV IE,#80H
JMP T1END
GOON: MOV TL1,#0B0H
MOV TH1,#3CH
T1END: RETI
ORG 0100H
MAIN: MOV R5,#99H
LOOP: MOV TMOD,#11H
MOV IP,#02H
MOV TL0,#0B0H
SETB EA
NEXT: MOV DPTR,#TAB
MOV R1,#00H
L1: MOV A,R1
MOVC A,@A+DPTR
MOV R3,A
JZ L3
INC R1
MOV A,R1
MOVC A,@A+DPTR
MOV R2,A
XRL A,#0FFH
JNZ L4
MOV A,R3
XRL A,#0FFH
JZ L5
L4: INC R1
MOV A,R1
L3: MOVC A,@A+DPTR
MOV R4,A
MOV TH0,R3
MOV TL0,R2
SETB ET0
SETB TR0
SETB ET1
SETB TR1
SETB HUAN
JB HUAN,$
INC R1
CJNE R1,#00H,L2
INC DPH
L2: JMP L1
L5: DJNZ R5,LOOP
JMP $
;《两只老虎》播放数据
TAB: DB 0FBH,0F9H,08H,0FCH,066H,08H,0FCH,0C7H,08H,0FBH,0F9H,08H
DB 0FBH,0F9H,08H,0FCH,066H,08H,0FCH,0C7H,08H,0FBH,0F9H,08H
DB 0FCH,0C7H,08H,0FCH,0F3H,08H,0FDH,0A4H,10H,0FCH,0C7H,08H
DB 0FCH,0F3H,08H,0FDH,04AH,08H,0FDH,04AH,04H,0FDH,095H,04H
DB 0FDH,04AH,04H,0FCH,0F3H,04H,0FCH,0C7H,08H,0FBH,0F9H,08H
DB 0FDH,04AH,04H,0FDH,095H,04H,0FDH,4AH,08H,04H,0FCH,0FCH,04H
DB 0FCH,0C7H,08H,0FBH,0F9H,08H,0FCH,066H,08H,0FAH,093H,08H
DB 0FBH,0F9H,08H,00H,08H,0FCH,66H,04H,0FAH,093H,08H
DB 0FBH,0F9H,08H,00H,10H,0FFH,0FFH
END
要播放其余的歌则用相应的播放数据
#include
#define uchar unsigned char
#define uint unsigned int
int cnt_h,cnt_l;
sbit spk=P2^3;
//sbit led=P2^0;
uchar cnt,i,step,s_bit;
code uint yin[7]={1912,1704,1516,1432,1276,1136,1012}; //1:低八度 3:高八度
//《北京欢迎你》 简谱代码
code uint song[]={123,125,123,122,123,122,223,123,122,116,121,123,122,222,122,121,116,121,122,123,125,122,123,126,125,116,122,121,121
,122,121,116,121,122,123,125,122,123,126,125,125,123,123,122,123,122,121,125,126,222,115,123,123,222,223,
116,123,122,122,221,423,425,131,125,226,226,125,123,123,125,225,123,125,126,131,132,131,125,123,122,125,223,223,123,125,131,125,226,231,132,131,125,123,125,127,226,223,122,123,125,133,232,127,331,999};
void delay(uint a);//
void init(); // 简谱代码解释:每个音及其节拍用一个三位数表示
void main() // 三位数的百位表示节奏的整数倍
{ // 十位表示音的高低(1位低音,3为高音)
init(); // 个位数表示是哪个基本音素
while(1)
{
step=(song[i]/100)*4; //将2变成其他 数可改变节奏的快慢
// TH1=(65536-65535)/256;
// TL1=(65536-65535)%256;
switch((song[i]/10)%10)
{
case 1:
{
cnt_h=-(yin[song[i]%10-1]*2)/256;
cnt_l=-(yin[song[i]%10-1]*2)%256;
break;
}
case 2:
{
cnt_h=-yin[song[i]%10-1]/256;
cnt_l=-yin[song[i]%10-1]%256;
break;
}
case 3:
{
cnt_h=-(yin[song[i]%10-1]/2)/256;
cnt_l=-(yin[song[i]%10-1]/2)%256;
break;
}
}
TH0=cnt_h;
TL0=cnt_l;
TR1=1;
TR0=1;
next:
while(cnt!=step)
goto next;
// led=~led;
TR0=0;
cnt=0;
TR1=0;
delay(200);
i++;
if(song[i]==999)
{
i=0;
delay(900);
}
}
}
void init()
{
EA=1;
ET0=1;
ET1=1;
TMOD=0x11;
i=0;
cnt=0;
TR0=0;
TR1=0 ;
}
void t0(void) interrupt 1
{
TH0=cnt_h;
TL0=cnt_l;
spk=~spk;
}
void t1(void) interrupt 3
{
// TH1=(65536-65535)/256;
// TL1=(65536-65535)%256;
cnt++;
}
void delay(uint a)
{
uint q,k;
for(q=0;q<84;q++)
for(k=0;k}
===================
自己很久前写的,你看看呢