1. 谁知道51单片机控制蜂鸣器,使其能响出音乐。
#include<reg52.h>
8 #define uchar unsigned char
9 #define uint unsigned int
10 uchar Count;
11
12 sbit Speak =P1^2; //蜂鸣器器控制脚
13 /*以下数组是音符编码*/
14 unsigned char code SONG[] ={ //祝你平安
15 0x26,0x20,0x20,0x20,0x20,0x20,0x26,0x10,0x20,0x10,0x20,0x80,0x26,0x20,0x30,0x20,
16 0x30,0x20,0x39,0x10,0x30,0x10,0x30,0x80,0x26,0x20,0x20,0x20,0x20,0x20,0x1c,0x20,
17 0x20,0x80,0x2b,0x20,0x26,0x20,0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x80,0x26,0x20,
18 0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x60,0x40,0x10,0x39,0x10,0x26,0x20,
19 0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x80,0x26,0x20,0x2b,0x10,0x2b,0x10,
20 0x2b,0x20,0x30,0x10,0x39,0x10,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x20,
21 0x20,0x10,0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x18,0x20,0x18,0x20,0x26,0x20,
22 0x20,0x20,0x20,0x40,0x26,0x20,0x2b,0x20,0x30,0x20,0x30,0x20,0x1c,0x20,0x20,0x20,
23 0x20,0x80,0x1c,0x20,0x1c,0x20,0x1c,0x20,0x30,0x20,0x30,0x60,0x39,0x10,0x30,0x10,
24 0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x10,0x26,0x10,0x26,0x10,0x2b,0x10,0x2b,0x80,
25 0x18,0x20,0x18,0x20,0x26,0x20,0x20,0x20,0x20,0x60,0x26,0x10,0x2b,0x20,0x30,0x20,
26 0x30,0x20,0x1c,0x20,0x20,0x20,0x20,0x80,0x26,0x20,0x30,0x10,0x30,0x10,0x30,0x20,
27 0x39,0x20,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x10,0x40,0x10,0x20,0x10,
28 0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x00,
29 //路边的野华不要采
30 0x30,0x1C,0x10,0x20,0x40,0x1C,0x10,0x18,0x10,0x20,0x10,0x1C,0x10,0x18,0x40,0x1C,
31 0x20,0x20,0x20,0x1C,0x20,0x18,0x20,0x20,0x80,0xFF,0x20,0x30,0x1C,0x10,0x18,0x20,
32 0x15,0x20,0x1C,0x20,0x20,0x20,0x26,0x40,0x20,0x20,0x2B,0x20,0x26,0x20,0x20,0x20,
33 0x30,0x80,0xFF,0x20,0x20,0x1C,0x10,0x18,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,
34 0x20,0x2B,0x40,0x20,0x20,0x1C,0x10,0x18,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,
35 0x20,0x2B,0x40,0x20,0x30,0x1C,0x10,0x18,0x20,0x15,0x20,0x1C,0x20,0x20,0x20,0x26,
36 0x40,0x20,0x20,0x2B,0x20,0x26,0x20,0x20,0x20,0x30,0x80,0x20,0x30,0x1C,0x10,0x20,
37 0x10,0x1C,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,0x20,0x2B,0x40,0x20,0x15,0x1F,
38 0x05,0x20,0x10,0x1C,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,0x20,0x2B,0x40,0x20,
39 0x30,0x1C,0x10,0x18,0x20,0x15,0x20,0x1C,0x20,0x20,0x20,0x26,0x40,0x20,0x20,0x2B,
40 0x20,0x26,0x20,0x20,0x20,0x30,0x30,0x20,0x30,0x1C,0x10,0x18,0x40,0x1C,0x20,0x20,
41 0x20,0x26,0x40,0x13,0x60,0x18,0x20,0x15,0x40,0x13,0x40,0x18,0x80,0x00,
42
43 };
44
45 /********************************************************************
46 * 名称 : Time0_Init()
47 * 功能 : 定时器的初始化,定时时间可用光盘中软件计算,11.0592MZ晶振,10ms
48 * 输入 : 无
49 * 输出 : 无
50 ***********************************************************************/
51 void Time0_Init()
52 {
53 1 TMOD = 0x01;
54 1 IE = 0x82;
55 1 TH0 = 0xDC;
C51 COMPILER V7.50 蜂胈芲唱歌 08/26/2011 10:26:23 PAGE 2
56 1 TL0 = 0x00;
57 1 }
58
59 /********************************************************************
60 * 名称 : Time0_Int()
61 * 功能 : 定时器中断,中断中实现 Count 加一
62 * 输入 : 无
63 * 输出 : 无
64 ***********************************************************************/
65 void Time0_Int() interrupt 1
66 {
67 1 TH0 = 0xDC;
68 1 TL0 = 0x00;
69 1 Count++; //长度加1
70 1 }
71
72 /********************************************************************
73 * 名称 : Delay_xMs()
74 * 功能 : 延时子程序,经过软件调试,测得延时程序大概为55us.
75 * 输入 : x
76 * 输出 : 无
77 ***********************************************************************/
78 void Delay_xMs(uint x)
79 {
80 1 uint i,j;
81 1 for(i=0; i<x; i++)
82 1 {
83 2 for(j=0; j<3; j++);
84 2 }
85 1 }
86
87 /********************************************************************
88 * 名称 : Play_Song()
89 * 功能 : 播放蜂鸣器控制程序
90 * 输入 : i (选择播放哪首歌曲,0为“祝你平安”,1为“路边的野花你不要采”
91 * 输出 : 无
92 ***********************************************************************/
93 void Play_Song(uchar i)
94 {
95 1 uchar Temp1,Temp2;
96 1 uint Addr;
97 1 Count = 0; //中断计数器清0
98 1 Addr = i * 217;
99 1 while(1)
100 1 {
101 2 Temp1 = SONG[Addr++];
102 2 if (Temp1 == 0xFF) //休止符
103 2 {
104 3 TR0 = 0;
105 3 Delay_xMs(100);
106 3 }
107 2 else if (Temp1 == 0x00) //歌曲结束符
108 2 {
109 3 return;
110 3 }
111 2 else
112 2 {
113 3 Temp2 = SONG[Addr++];
114 3 TR0 = 1;
115 3 while(1)
116 3 {
117 4 Speak = ~Speak;
C51 COMPILER V7.50 蜂胈芲唱歌 08/26/2011 10:26:23 PAGE 3
118 4 Delay_xMs(Temp1);
119 4 if(Temp2 == Count)
120 4 {
121 5 Count = 0;
122 5 break;
123 5 }
124 4 }
125 3 }
126 2 }
127 1 }
2. 51单片机蜂鸣器唱歌程序
[2]单片机音乐盒花样流水灯设计
功能描述:
1、按键切换音乐
2、彩灯可以跟随音调亮灭
3、按键切换彩灯显示花样
4、支持功能定制
☆已作出的实物优酷视频演示地址:
http://v.youku.com/v_show/id_XMTUxMDU3MzcyOA==.html
3. 51单片机的蜂鸣器来控制音乐,音乐数组用的是十六进制数,请问是怎么个编法
给你大概说个原理吧,用两个定时器,一个来产生某个频率的方波,一个来定时产生这个频率的方波的持续时间。
音频上的每个调都有自己的频率的,这个你可以自己去网上查查。
每个调的持续时间都不一样,这个也要去控制,否则你听到的都是一样时长的调子,听起来就不像是歌曲了。
4. 51单片机蜂鸣器程序,音乐的随时跳转
这里修改:
sbitkey1=P1^0;
sbitkey2=P1^1;
voidPlay_Song(unsignedchari)
{
unsignedcharTemp1,Temp2;
unsignedintAddr;
Count=0;//中断计数器清0
Addr=i*217;
while(1)
{
if(key2==0)break;//插入这一句
Temp1=SONG[Addr++];
if(Temp1==0xFF)//休止符
{
TR0=0;
Delay_xMs(100);
}
elseif(Temp1==0x00)//歌曲结束符
{
return;
}
else
{
Temp2=SONG[Addr++];
TR0=1;
while(1)
{
_Speak=~_Speak;
Delay_xMs(Temp1);
if(Temp2==Count)
{
Count=0;
break;
}
}
}
}
}
PlaySong2(................
{
....
whlie(1)
{
if(key1==0)break;//插入这一句
.....
}
}
voidmain()
{
Time0_Init();//定时器0中断初始化
while(1)
{
Play_Song(0);//播放1
Play_Song2(0);//播放2
}
}
5. 求51单片机蜂鸣器播放音乐的教程 C语言
这是我学校电子设计时做的,可以弹琴,也有几首歌在里面。。你应该看得懂#include <reg52.h> #define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
sbit BEEP=P2^0; //喇叭输出脚uchar th0_f; //在中断中装载的T0的值高8位
uchar tl0_f; //在中断中装载的T0的值低8位
uchar key;/*------------------------------------------------
函数声明
------------------------------------------------*/
uchar keyscan(void);//键盘扫描程序
void delay(uint i); //延时子程序/*-------- T0的值,及输出频率对照表 --------------*/
uchar code freq[36*2]={
0xA9,0xEF, //0x3F,0XEE,//00220HZ ,1 //0
0x93,0xF0, //0X3D,0XEF,//00233HZ ,1#
0x73,0xF1, //0X30,0XF0,//00247HZ ,2
0x49,0xF2, //0X18,0XF1,//00262HZ ,2#
0x07,0xF3, //0XE6,0XF1,//00277HZ ,3
0xC8,0xF3, //0XB7,0XF2,//00294HZ ,4
0x73,0xF4, //0X71,0XF3,//00311HZ ,4#
0x1E,0xF5, //0X2A,0XF4,//00330HZ ,5
0xB6,0xF5, //0XCF,0XF4,//00349HZ ,5#
0x4C,0xF6, //0X72,0XF5,//00370HZ ,6
0xD7,0xF6, //0X09,0XF6,//00392HZ ,6#
0x5A,0xF7, //0X97,0XF6,//00415HZ ,7
0xD8,0xF7, //0X20,0XF7,//00440HZ 1 //12
0x4D,0xF8, //0X9F,0XF7,//00466HZ 1# //13
0xBD,0xF8, //0X18,0XF8,//00494HZ 2 //14
0x24,0xF9, //0X88,0XF8,//00523HZ 2# //15
0x87,0xF9, //0XF3,0XF8,//00554HZ 3 //16
0xE4,0xF9, //0X59,0XF9,//00587HZ 4 //17
0x3D,0xFA, //0X05,0XFA,//00622HZ 4# //18
0x90,0xFA, //0X13,0XFA,//00659HZ 5 //19
0xDE,0xFA, //0X66,0XFA,//00698HZ 5# //20
0x29,0xFB, //0XB9,0XFA,//00740HZ 6 //21
0x6F,0xFB, //0X05,0XFB,//00784HZ 6# //22
0xB1,0xFB, //0X4D,0XFB,//00831HZ 7 //23
0xEF,0xFB, //0X90,0XFB,//00880HZ `1
0x2A,0xFC, //0XD0,0XFB,//00932HZ `1#
0x62,0xFC, //0X0C,0XFC,//00988HZ `2
0x95,0xFC, //0X44,0XFC,//01046HZ `2#
0xC7,0xFC, //0X7B,0XFC,//01109HZ `3
0xF6,0xFC, //0XAD,0XFC,//01175HZ `4
0x22,0xFD, //0XDD,0XFC,//01244HZ `4#
0x4B,0xFD, //0X0A,0XFD,//01318HZ `5
0x73,0xFD, //0X35,0XFD,//01397HZ `5#
0x98,0xFD, //0X5D,0XFD,//01480HZ `6
0xBB,0xFD, //0X83,0XFD,//01568HZ `6#
0xDC,0xFD, //0XA6,0XFD,//01661HZ `7 //35
};//定时中断0,用于产生唱歌频率
timer0() interrupt 1
{
TL0=tl0_f;TH0=th0_f; //调入预定时值
BEEP=~BEEP; //取反音乐输出IO
}//音乐符号串解释函数
//入口:要解释的音乐符号串,输出的音调串,输出的时长串
changedata(uchar *song,uchar *diao,uchar *jie)
{
uchar i,i1,j;
char gaodi; //高低+/-12音阶
uchar banyin;//有没有半个升音阶
uchar yinchang;//音长
uchar code jie7[8]={0,12,14,16,17,19,21,23}; //C调的7个值
*diao=*song;
for(i=0,i1=0;;)
{
gaodi=0; //高低=0
banyin=0;//半音=0
yinchang=4;//音长1拍
if((*(song+i)=='|') || (*(song+i)==' ')) i++;
//拍子间隔和一个空格过滤
switch(*(song+i))
{
case ',': gaodi=-12;i++;//低音
break;
case '`': gaodi=12;i++; //高音
break;
}
if(*(song+i)==0) //遇到0结束
{
*(diao+i1)=0; //加入结束标志0
*(jie+i1)=0;
return;
}
j=*(song+i)-0x30; i++; //取出基准音
j=jie7[j]+gaodi; //加上高低音
yinc: switch(*(song+i))
{
case '#': //有半音j加一个音阶
i++;j++;
goto yinc;
case '-': //有一个音节加长
yinchang+=4;
i++;
goto yinc;
case '_': //有一个音节缩短
yinchang/=2;
i++;
goto yinc;
case '.': //有一个加半拍
yinchang=yinchang+yinchang/2;
i++;
goto yinc;
}
*(diao+i1)=j; //记录音符
*(jie+i1)=yinchang; //记录音长
i1++;
}
}//奏乐函数
//入口:要演奏的音乐符号串
void play(uchar *songdata)
{
uchar i,c,j=0;
uint n;
uchar diaodata[48]; //音调缓冲
uchar jiedata[48]; //音长缓冲
changedata(songdata,diaodata,jiedata); //解释音乐符号串
TR0=1;
for(i=0;diaodata[i]!=0;i++) //逐个符号演奏
{
tl0_f=freq[diaodata[i]*2]; //取出对应的定时值送给T0
th0_f=freq[diaodata[i]*2+1];
for(c=0;c<jiedata[i];c++) //按照音长延时
{ key = keyscan();
for(n=0;n<29500;n++); // 29500
if(key != 0xff)//((!K1)||(!K2)||(!K3)||(!K4))//发现按键,立即退出播放
{
TR0=0;
return;
}
}
TR0=0;
for(n=0;n<460;n++); //460音符间延时
TR0=1;
}
TR0=0;
}//一分钱
uchar code yifenqian[]={
"5`1|6_`1_5|3_5_2_3_|5-|"
"3_5_6_`1_|5_6_5_3_|1.3__|2-|"
"3_2_1_2_|3-|6_5_3_5_|6-|"
"5_`1_6_5_|3_5_2|5_2_3_2_|1-|"
}; //世上只有妈妈好
uchar code mamahao[]={
"6.5_35|`16_5_6-|35_6_53_2_|1_,6_5_3_2-|"
"2.3_55_6_|321-|5.3_2_1_,6_1_|,5--"
};
//找朋友
uchar code zhaopengyou[]={ "5_6_5_6_|5_6_5|5_`1_7_6_|5 3|"
"5_5_3_4_|5_5_3|1_4_3_2_|1_2_1|"
};
//茉莉花
uchar code molihua[]={"33_5_6_`1_`1_6_|55_6_5-|33_5_6_`1_`1_6_|55_6_5-|"
"5553_5_|665-|32_3_53_2_|11_2_1|"
//"3_2_1_3_2.3_|56_`1_5-|23_5_2_3_1_,6_|,5-,61|"
//"2.3_1_2-1_,6_|,5--"
};
//新年好
uchar code xinnianhao[]={
"1_1_1 ,5|3_3_3 1|1_3_5 5|4_3_2-|"
"2_3_4-|3_2_3 1|1_3_2 ,5|,7_2_1-|"
};
//小星星
uchar code xingxing[]={
"1155|665-|4433|221-|"
"5544|332-|5544|332-|"
"1155|665-|4433|221-|"
};
//外婆的澎湖湾
uchar code waipodephw[]={
"3_5_5_5_6_`1_6_5_|`1_`1_`1_6_5-|"
"`3_`3_`3_`3_`4_`3_`2_`1_|`2_`2_`2_`3_`2-|"
"`3_`3_`3_`3_`4_`3_`2_`1_|6_`1_`1_6_5-|"
};
}
6. 用单片机控制蜂鸣器放音乐
看起来数组应该是音乐数据,其中包括四种数据,就是休止符(每个100毫秒)、歌曲结束符、音阶(给出的是蜂鸣器的震荡周期)、一个音阶持续的时间长度。如果你的晶振是12MHz,按照定时器中断给Count加一来看,音阶持续的时间应该是以10毫秒为单位。
Play_Song函数要求给出参数i,我的看法是这个i是指要播放第几段乐曲,i*217表示每段乐曲都是217个字节。本例中,数组只给出217个字节,所以只有一段乐曲,播放时需要给出参数i=0.
if ( Temp1 == 0xFF ) //休止符
{
TR0 = 0;
Delay_xMs(100);
}
所以休止符每个固定是100毫秒。
如果读到第一个不是休止符或者结束符的字节,那就是音阶,而下一个字节就是这个音阶的长度或者说节拍
while(1)
{
FMQ = ~FMQ;
Delay_xMs(Temp1);
if ( Temp2 == Count )
{
Count = 0;
break;
}
}
这一段是说,如果当前音阶的时间(Temp2次定时器中断,由Count计时)还没完,就每隔Temp1毫秒震动一次蜂鸣器,就形成一个震动频率,这段时间就发出一定音调的乐音。不过我也有疑惑,震荡周期如果以毫秒为单位恐怕只能发出次声波,所以以上对有关Delay_xMs函数时间的估算都可能有问题,还得看Delay_xMs函数的具体定义才行。
7. 怎样把51单片机蜂鸣器响为音乐和闹钟的程序串在一起
把音乐函数作为闹钟的子函数就可以了,在需要闹铃响时调用该子函数。
8. 51单片机蜂鸣器音乐
你需要把青花瓷的谱子变成数字(定时器的时间)