永发信息网

单片机蜂鸣器奏乐,歌曲播放子程序,菜鸟不太懂,求高手详细解释下,谢谢。

答案:3  悬赏:40  手机版
解决时间 2021-03-21 22:54
程序在这里-------------temp1 temp2 搞不清楚了,能不能详细解释下歌曲播放那个程序
#include <reg51.h>
unsigned char count;
sbit speak=P2^3;
unsigned char code song[]={
0x26,0x20,0x20,0x20,0x20,0x20,0x26,0x10,0x20,0x10,0x20,0x80,0x26,0x20,0x30,0x20,
0x30,0x20,0x39,0x10,0x30,0x10,0x30,0x80,0x26,0x20,0x20,0x20,0x20,0x20,0x1c,0x20,
0x20,0x80,0x2b,0x20,0x26,0x20,0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x80,0x26,0x20,
0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x60,0x40,0x10,0x39,0x10,0x26,0x20,
0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x80,0x26,0x20,0x2b,0x10,0x2b,0x10,
0x2b,0x20,0x30,0x10,0x39,0x10,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x20,
0x20,0x10,0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x18,0x20,0x18,0x20,0x26,0x20,
0x20,0x20,0x20,0x40,0x26,0x20,0x2b,0x20,0x30,0x20,0x30,0x20,0x1c,0x20,0x20,0x20,
0x20,0x80,0x1c,0x20,0x1c,0x20,0x1c,0x20,0x30,0x20,0x30,0x60,0x39,0x10,0x30,0x10,
0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x10,0x26,0x10,0x26,0x10,0x2b,0x10,0x2b,0x80,
0x18,0x20,0x18,0x20,0x26,0x20,0x20,0x20,0x20,0x60,0x26,0x10,0x2b,0x20,0x30,0x20,
0x30,0x20,0x1c,0x20,0x20,0x20,0x20,0x80,0x26,0x20,0x30,0x10,0x30,0x10,0x30,0x20,
0x39,0x20,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x10,0x40,0x10,0x20,0x10,
0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x00,
0x30,0x1C,0x10,0x20,0x40,0x1C,0x10,0x18,0x10,0x20,0x10,0x1C,0x10,0x18,0x40,0x1C,
0x20,0x20,0x20,0x1C,0x20,0x18,0x20,0x20,0x80,0xFF,0x20,0x30,0x1C,0x10,0x18,0x20,
0x15,0x20,0x1C,0x20,0x20,0x20,0x26,0x40,0x20,0x20,0x2B,0x20,0x26,0x20,0x20,0x20,
0x30,0x80,0xFF,0x20,0x20,0x1C,0x10,0x18,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,
0x20,0x2B,0x40,0x20,0x20,0x1C,0x10,0x18,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,
0x20,0x2B,0x40,0x20,0x30,0x1C,0x10,0x18,0x20,0x15,0x20,0x1C,0x20,0x20,0x20,0x26,
0x40,0x20,0x20,0x2B,0x20,0x26,0x20,0x20,0x20,0x30,0x80,0x20,0x30,0x1C,0x10,0x20,
0x10,0x1C,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,0x20,0x2B,0x40,0x20,0x15,0x1F,
0x05,0x20,0x10,0x1C,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,0x20,0x2B,0x40,0x20,
0x30,0x1C,0x10,0x18,0x20,0x15,0x20,0x1C,0x20,0x20,0x20,0x26,0x40,0x20,0x20,0x2B,
0x20,0x26,0x20,0x20,0x20,0x30,0x30,0x20,0x30,0x1C,0x10,0x18,0x40,0x1C,0x20,0x20,
0x20,0x26,0x40,0x13,0x60,0x18,0x20,0x15,0x40,0x13,0x40,0x18,0x80,0x00,
};
void time0_init()
{
TMOD=0x01;
IE=0x82;
TH0=0xDC;
TL0=0x00;
}
void time1_int() interrupt 1
{
TH0=0xDC;
TL0=0x00;
count++;
}
void delay_xms(unsigned int x)
{
unsigned int i,j;
for(i=0; i<x; i++)
for(j=0; j<3; j++);
}
void play_song(unsigned char i)
{
unsigned char temp1,temp2;
unsigned int addr;
count=0;
addr=i*217;
while(1)
{
temp1=song[addr++];
if(temp1==0xff)
{
TR0=0;
delay_xms(100);
}
else if(temp1==0x00)
{
return;
}
else
{
temp2=song[addr++];
TR0=1;
while(1)
{
speak=~speak;
delay_xms(temp1);
if(temp2==count)
{
count=0;
break;
}
}
}
}
}
void main()
{
time0_init();
while(1)
{
play_song(0);
}
}
最佳答案
#include <reg51.h> //头文件
unsigned char count; //定义全局变量,是一个计数器,0-255计数,用来计时一个频率的时长
sbit speak=P2^3; //蜂鸣器口
unsigned char code song[]={ //歌曲代码,存储方式为:频率,时长,频率,时长...
0x26,0x20,0x20,0x20,0x20,0x20,0x26,0x10,0x20,0x10,0x20,0x80,0x26,0x20,0x30,0x20,
0x30,0x20,0x39,0x10,0x30,0x10,0x30,0x80,0x26,0x20,0x20,0x20,0x20,0x20,0x1c,0x20,
0x20,0x80,0x2b,0x20,0x26,0x20,0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x80,0x26,0x20,
0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x60,0x40,0x10,0x39,0x10,0x26,0x20,
0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x80,0x26,0x20,0x2b,0x10,0x2b,0x10,
0x2b,0x20,0x30,0x10,0x39,0x10,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x20,
0x20,0x10,0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x18,0x20,0x18,0x20,0x26,0x20,
0x20,0x20,0x20,0x40,0x26,0x20,0x2b,0x20,0x30,0x20,0x30,0x20,0x1c,0x20,0x20,0x20,
0x20,0x80,0x1c,0x20,0x1c,0x20,0x1c,0x20,0x30,0x20,0x30,0x60,0x39,0x10,0x30,0x10,
0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x10,0x26,0x10,0x26,0x10,0x2b,0x10,0x2b,0x80,
0x18,0x20,0x18,0x20,0x26,0x20,0x20,0x20,0x20,0x60,0x26,0x10,0x2b,0x20,0x30,0x20,
0x30,0x20,0x1c,0x20,0x20,0x20,0x20,0x80,0x26,0x20,0x30,0x10,0x30,0x10,0x30,0x20,
0x39,0x20,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x10,0x40,0x10,0x20,0x10,
0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x00,
0x30,0x1C,0x10,0x20,0x40,0x1C,0x10,0x18,0x10,0x20,0x10,0x1C,0x10,0x18,0x40,0x1C,
0x20,0x20,0x20,0x1C,0x20,0x18,0x20,0x20,0x80,0xFF,0x20,0x30,0x1C,0x10,0x18,0x20,
0x15,0x20,0x1C,0x20,0x20,0x20,0x26,0x40,0x20,0x20,0x2B,0x20,0x26,0x20,0x20,0x20,
0x30,0x80,0xFF,0x20,0x20,0x1C,0x10,0x18,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,
0x20,0x2B,0x40,0x20,0x20,0x1C,0x10,0x18,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,
0x20,0x2B,0x40,0x20,0x30,0x1C,0x10,0x18,0x20,0x15,0x20,0x1C,0x20,0x20,0x20,0x26,
0x40,0x20,0x20,0x2B,0x20,0x26,0x20,0x20,0x20,0x30,0x80,0x20,0x30,0x1C,0x10,0x20,
0x10,0x1C,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,0x20,0x2B,0x40,0x20,0x15,0x1F,
0x05,0x20,0x10,0x1C,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,0x20,0x2B,0x40,0x20,
0x30,0x1C,0x10,0x18,0x20,0x15,0x20,0x1C,0x20,0x20,0x20,0x26,0x40,0x20,0x20,0x2B,
0x20,0x26,0x20,0x20,0x20,0x30,0x30,0x20,0x30,0x1C,0x10,0x18,0x40,0x1C,0x20,0x20,
0x20,0x26,0x40,0x13,0x60,0x18,0x20,0x15,0x40,0x13,0x40,0x18,0x80,0x00,
};
void time0_init() //定时器初始化函数
{
TMOD=0x01; //工作方式,16位定时器
IE=0x82; //允许定时器T0中断和总中断
TH0=0xDC; //定时器初值高8位,具体时间需要根据晶振计算
TL0=0x00; //定时器初值低8位
}
void time1_int() interrupt 1 //定时器中断函数
{
TH0=0xDC; //定时器重赋初值高8位
TL0=0x00; //定时器重赋初值低8位
count++; //计数器自加
}
void delay_xms(unsigned int x) //延时函数
{
unsigned int i,j; //定义延时函数局部变量
for(i=0; i<x; i++) //用for循环实现延时
for(j=0; j<3; j++); //数值很小,不应该是ms级的,此函数用来改变蜂鸣器频率
}
void play_song(unsigned char i) //歌曲播放函数
{
unsigned char temp1,temp2; //定义局部变量,temp1为频率缓存器,temp2为时长缓存器
unsigned int addr; //定义局部变量,是一个数据地址缓存器
count=0; //计数器清零
addr=i*217; //两首歌,数组中0~216为歌曲0,
while(1) //此循环
{
temp1=song[addr++]; //从0开始,取偶数地址中的值(频率)放到缓存器temp1,addr自加
if(temp1==0xff) //如果遇到0xff
{
TR0=0; //不允许定时器T0计时
delay_xms(100); //延时,此处0xff可能是停顿符号
}
else if(temp1==0x00) //否则如果遇到0x00
{
return; //函数返回,此处的0x00应该是歌曲播完的符号
}
else //否则
{
temp2=song[addr++]; //取奇数地址中的值放到缓存器temp2,addr自加
TR0=1; //允许定时器T0计时
while(1) //此循环用来调节蜂鸣器发声频率和时长
{
speak=~speak; //蜂鸣器取反,此蜂鸣器为它激式,可以用改变取反频率方式实现发出不同音调
delay_xms(temp1); //延时相应频率所需时长
if(temp2==count) //如果相应频率发声时长与计数器数值相同
{
count=0; //计数器清零
break; //跳出该while循环
}
}
}
}
}
void main() //主函数
{
time0_init(); //调用定时器初始化函数
while(1) //单片机进入正常工作死循环
{
play_song(0); //调用播放歌曲函数,并且播放歌曲0
}
}


*****/
全部回答
讲白了,就是发送一个不同频率的波,就有不同声音
song[]里边应该有两首歌,以0x00结尾,每首歌的数据存放方式是:频率 时长 频率 时长... 再看看别人怎么说的。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
暗黑2 佣兵快捷键不能用了
usb鼠标插头处线的排列跟鼠标里线的排列一样
海丰县国土资源局怎么去啊,有知道地址的么
退仓(HK)什么意思
staggers是什么意思?
硅谷写字间在什么地方啊,我要过去处理事情
回郭镇贴吧泰邦厂门口车祸事件
判断题鸟发达的胸肌两端都附着在胸骨上.
朝鲜能不能从中国进口空客飞机
单选题下列有关文学常识的表述有误的一项是A.
现在完成时have后什么时候后面加been
云南省电信服务密码忘记了我该怎么办
一斤蛋糕粉配多少水
财付通支付限额怎么改,财付通怎么设置消费限
上口香地址在什么地方,想过去办事
推荐资讯
三水压倒山,尔字有人陪,瓜里有个籽,两水十
对方违约,我有他都给写的定金条,起诉书怎么
《英雄联盟》感恩活动有没有什么情节让你特别
对..感到满意翻译成英文3种
高考复习一年究竟能提多少分?
tambon bang sao thong 离曼谷市中心有多远
HP笔记本,外接小数字键盘不能单独使用?小键
室内装潢的主要污染物是甲醛,甲醛的化学式为
北京现代i30这个是什么故障
联想电脑升win10后,充电时或充满了抽充电器
170的女生体重多少才算标准?
阿拉伯语祝福语怎么写,弹琴动作描写句子
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?