我这个程序是STC89C52RC的,你可以试试,应该可以直接用的。#include <reg51.h>
sbit voice=P1^5;//蜂鸣器
unsigned char code sound[]={0xff,
0x26,0x20,0x2b,0x20,0x26,0x80,0xff,0x20,0x2b,0x20,0x26,0x20,0x26,0x10,//两只蝴蝶
0x2b,0x10,0x30,0x80,0xff,0x40,0x39,0x20,0x30,0x20,0x2b,0x40,0x2b,0x20,
0x26,0x20,0x2b,0x20,0x30,0x20,0x39,0x20,0x39,0x10,0x30,0x10,0x40,0x80,
0x40,0x80,0x26,0x20,0x2b,0x20,0x26,0x80,0xff,0x20,0x2b,0x20,0x26,0x20,
0x26,0x10,0x2b,0x10,0x30,0x80,0xff,0x40,0x39,0x20,0x30,0x20,0x2b,0x40,
0x2b,0x20,0x26,0x20,0x2b,0x20,0x30,0x20,0x39,0x20,0x30,0x20,0x2b,0xc0,
0x26,0x20,0x2b,0x20,0x26,0x80,0xff,0x20,0x2b,0x20,0x26,0x20,0x26,0x10,
0x2b,0x10,0x30,0xc0,0x39,0x20,0x30,0x20,0x2b,0x40,0x2b,0x20,0x26,0x20,
0x2b,0x20,0x30,0x20,0x39,0x20,0x39,0x10,0x30,0x10,0x40,0xc0,0x26,0x20,
0x20,0x20,0x20,0x80,0xff,0x20,0x20,0x20,0x1c,0x20,0x20,0x20,0x26,0xc0,
0x2b,0x20,0x2b,0x20,0x2b,0x60,0x2b,0x20,0x26,0x20,0x2b,0x20,0x30,0x20,
0x39,0x20,0x39,0x20,0x30,0x20,0x30,0x20,0x30,0xc0,0xff,0x40,0xff,0x40,
0xff,0x20,0x20,0x20,0x20,0x20,0x1c,0x20,0x18,0x20,0x19,0x20,0x19,0x20,
0x1c,0x20,0x26,0x20,0x2b,0x20,0x2b,0x20,0x26,0x20,0x26,0x80,0xff,0x20,
0x26,0x20,0x26,0x20,0x20,0x20,0x1c,0x40,0x1c,0x40,0xff,0x20,0x39,0x20,
0x26,0x20,0x2b,0x20,0x2b,0xc0,0x26,0x20,0x20,0x20,0x20,0x20,0x26,0x20,
0x20,0x40,0xff,0x20,0x18,0x40,0x19,0x20,0x1c,0x20,0x1c,0x10,0x19,0x10,
0x26,0x40,0xff,0x20,0x1c,0x20,0x1c,0x20,0x19,0x20,0x1c,0x20,0x20,0x20,
0x26,0x20,0x2b,0x20,0xff,0x20,0x26,0x20,0x2b,0x20,0x26,0x20,0x20,0x80,
0xff,0x20,0x20,0x20,0x20,0x20,0x1c,0x20,0x18,0x20,0x19,0x20,0x19,0x20,
0x1c,0x20,0x26,0x20,0x2b,0x20,0x2b,0x20,0x26,0x20,0x26,0x80,0xff,0x20,
0x26,0x20,0x26,0x20,0x20,0x20,0x1c,0x40,0x1c,0x40,0xff,0x20,0x39,0x20,
0x26,0x20,0x2b,0x20,0x2b,0xc0,0x26,0x20,0x20,0x20,0x20,0x20,0x26,0x20,
0x20,0x40,0xff,0x20,0x18,0x40,0x19,0x20,0x1c,0x20,0x1c,0x10,0x19,0x10,
0x26,0x40,0xff,0x20,0x1c,0x20,0x1c,0x20,0x19,0x20,0x1c,0x20,0x20,0x20,
0x26,0x20,0x2b,0x20,0xff,0x20,0x40,0x40,0x39,0x20,0x30,0xc0,0xff,0xc0,
0x00
};//音乐数据,0xff为起始数据,0x00为终止数据,以后数据两两一组,前者为音阶,后者为节拍
unsigned char zdjs=0,jp;
void del(yj);
void main(void)
{
unsigned int dpjs=0;
unsigned char yj;
TMOD=0x01;
IE=0x82;
TH0=0xd8;
TL0=0xf0;
TR0=1;
while(1)
{
zdjs=0;
dpjs++;
yj=sound[dpjs];
dpjs++;
jp=sound[dpjs];
while(zdjs!=jp)
{
if(yj!=0xff)
{
if(yj!=0)
{
voice=!voice;
del(yj);
}
else
{
dpjs=0;
break;
}
}
else
{
voice=1;
del(jp);
}
}
}
}
time0() interrupt 1 using 1//定时器中断,记录时间
{
TH0=0xd8;
TL0=0xf0;
zdjs++;
}
void del(yj)
{
unsigned char yj2=2;
while(yj!=0)
{
while(yj2!=0)
{
yj2--;
}
yj2=2;
yj--;
}
}
只用了一个定时器。
|