1、写入头文件。
2、加入宏定义。
3、位定义 给出蜂鸣器变量 beep,接口是P2^3。sbit beep = P2^3;。
4、设计延迟函数:void delay(uint z){uint x,y;for(x = z; x > 0; x--)for(y = 60; y > 0 ; y--);} 。
5、设计主函数。
6、加入循环效果,蜂鸣器。beep = ~beep中: ~按位取反。
用语言芯片不好。ISD4003可以录音8分钟的,方原声都得
#include
#define uint unsigned int
#define uchar unsigned char
sbit BEEP=P1^2;
uint h,i;
uint hz;
uchar jiep;
bit flag;
uint code fre[]={ 64400,64524,64580,64684,64777,64820,64898,64968,65030,65058,65110,65157,65178,65217};
uchar code song[]={6,9, 6,3, 7,12, 6,12, 9,12, 8,24, 6,9, 6,3, 7,12,
6,12, 10,12, 9,24, 6,9, 6,3, 13,12, 11,12, 8,12, 7,12,
12,9, 12,3, 11,12, 9,12, 10,12, 9,24}; //歌谱 2个一组(0,0)(音调,节拍)
void init () //初始化
{
EA=1;
TMOD=0x11;
ET0=1;
ET1=1;
i=0;
flag=1;
}
void sing()
{
if(flag==1)
{
h=song[i];
jiep=song[i+1];
i=i+2;
if(i>46) i=0;
hz=fre[h];
TH0=(hz)/256;
TL0=(hz)%256;
flag=0;
TR0=1;
TR1=1;
}
}
void main()
{
init();
while(1)
{
sing();
}
}
void time0() interrupt 1 //定时器0
{
TH0=(hz)/256;
TL0=(hz)%256;
if(!flag) BEEP=!BEEP;
else TR0=0;
}
void time1() interrupt 3
{
TH1=(15536)/256;
TL1=(15536)%256;
--jiep;
if(jiep==0)
flag=1;
}
#include
#define uchar unsigned char
#define uint unsigned int
sbit BEEP=P2^6;
void Music(uchar number);
void delay10us(uchar time);
void delay50us(uchar time);
uchar code SOUNDLONG[]= //控制音调
{
18,6,24,24,24,48,
18,6,24,24,24,48,
18,6,24,24,24,24,24,
18,6,24,24,24,48,0
};
uchar code SOUNDTONE[]= //控制音色
{
212,212,190,212,159,169,
212,212,190,212,142,159,
212,212,106,126,159,169,190,
119,119,126,159,142,159,0 //生日快乐end
};
void Play_Music()
{
uint k,n;
uint SoundLong,SoundTone;
uint i,j,m;
do
{
if(i>=25) i=0;
SoundLong=SOUNDLONG[i];
SoundTone=SOUNDTONE[i];
i++;
for(n=0;n
for(k=0;k<12;k++)
{
BEEP=0;
for(m=0;m
for(m=0;m
}
}
delay50us(6);
}while((SOUNDLONG[i]!=0)||(SOUNDTONE[j]!=0));
}
//延时程序
void delay10us(uchar time)
{
uchar a,b,c;
for(a=0;a
89C51的芯片现在还能买得到么?