本帖最后由 nilizhi 于 2012-4-5 16:16 编辑
#include <reg51.h>
#include <intrins.h>
void delay_var(unsigned int t) //有参延时函数
{
unsigned int i;
i=t;
while(i--);
}
sbit speaker=P2^4;
code unsigned char diao1[]={0,47,43,39,35,32,29,26,23,20,18,16,14,12,10,8 //15个基本音调
};
code unsigned char yinyue[]={13,12,10,12,15,13,12,13, //世上只有妈妈好音调
10,12,13,12,10,8,6,12,
10,9,9,10,12,12,13,10,
9,8,12,10,9,8,6,8,5,
5,0
};
code unsigned char sj[]={3,1,2,2,2,1,1,
4,2,1,1,2,2,1, //世上只有妈妈好每个音调长度
1,1,1,4,3,1,2,
1,1,2,2,4,3,1,
1,1,1,1,3,3,0
};
void main(void)
{
unsigned char i=0;
unsigned int f,h,shijian;
while(1)
{
i=0;
while(yinyue!=0)
{
h=sj;
f=h*12000/diao1[yinyue]; //每个音长相对固定,不随频率变化
shijian=diao1[yinyue];
while(f--)
{
speaker=0;
delay_var(shijian+1);
speaker=1;
delay_var(shijian);
}
delay_var(5000);
i++;
}
delay_var(50000);
}
}
新手求交流 EMAIL:nilizhide@163.com QQ:380668522 |