程序在这里-------------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);
}
}
单片机蜂鸣器奏乐,歌曲播放子程序,菜鸟不太懂,求高手详细解释下,谢谢。
答案:3 悬赏:40 手机版
解决时间 2021-03-21 22:54
- 提问者网友:相思似海深
- 2021-03-21 00:14
最佳答案
- 五星知识达人网友:梦中风几里
- 2021-03-21 00:34
#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
}
}
*****/
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
}
}
*****/
全部回答
- 1楼网友:廢物販賣機
- 2021-03-21 01:13
讲白了,就是发送一个不同频率的波,就有不同声音
- 2楼网友:忘川信使
- 2021-03-21 00:50
song[]里边应该有两首歌,以0x00结尾,每首歌的数据存放方式是:频率 时长 频率 时长...
再看看别人怎么说的。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯