#include<reg52h>
#define uchar unsigned char
#define uint unsigned int
uchar Count;
sbit Speak =P1^2; //蜂鸣器器控制脚
/以下数组是音符编码/
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,
};
/
名称 : Time0_Init()
功能 : 定时器的初始化,定时时间可用光碟中软件计算,110592MZ晶振,10ms
输入 : 无
输出 : 无
/
void Time0_Init()
{
TMOD = 0x01;
IE = 0x82;
TH0 = 0xDC;
TL0 = 0x00;
}
/
名称 : Time0_Int()
功能 : 定时器中断,中断中实现 Count 加一
输入 : 无
输出 : 无
/
void Time0_Int() interrupt 1
{
TH0 = 0xDC;
TL0 = 0x00;
Count++; //长度加1
}
/
名称 : Delay_xMs()
功能 : 延时子程序,经过软件调试,测得延时程序大概为55us
输入 : x
输出 : 无
/
void Delay_xMs(uint x)
{
uint i,j;
for(i=0; i<x; i++)
{
for(j=0; j<3; j++);
}
}
/
名称 : Play_Song()
功能 : 播放蜂鸣器控制程序
输入 : i (选择播放哪首歌曲,0为“祝你平安”,1为“路边的野花你不要采”
输出 : 无
/
void Play_Song(uchar i)
{
uchar Temp1,Temp2;
uint Addr;
Count = 0; //中断计数器清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;
}
}
}
}
}
/
名称 : Main()
功能 : 主函数
输入 : 无
输出 : 无
/
void Main()
{
Time0_Init(); //定时器0中断初始化
while(1)
{
Play_Song(0); //Play_Song(0)为祝你平安 Play_Song(1)为路边的野花你不要裁
}
}
1有没有什么冲击源?这里的无源“源”不是指电源,而是振荡源。也就是说,主动蜂鸣器内部有振动源,所以只要一通电就会发出声音。无源系统内部没有振荡源,如果使用DC信号,就不可能产生啁啾。第二,价格不同。有源蜂鸣器往往比无源蜂鸣器贵,只是因为里面有很多振荡电路。第三,高度不同从图A和图B的外观上看,两个蜂鸣器看起来是一样的,但是仔细看,两个蜂鸣器的高度略有不同。主动蜂鸣器A的高度为9mm,被动蜂鸣器B的高度为8mm。比如把两个蜂鸣器的针脚朝上放,可以看到有绿色电路板的是无源蜂鸣器,没有电路板用乙烯基密封的是有源蜂鸣器。四、万用表电阻差与万用表电阻Rxl档测试:用黑色探针接蜂鸣器“+”引脚,红色探针在另一引脚上来回触碰。如果触发咔哒咔哒声,电阻只有8ω(或16ω),则为无源蜂鸣器;如果能发出连续的声音,且电阻大于几百欧姆,则为有源蜂鸣器。同时,主动蜂鸣器直接接入额定电源(标签上标注新蜂鸣器),可持续发声;无源蜂鸣器和电磁音箱一样,需要连接音频输出电路才能发声。五、优势不同(1)无源蜂鸣器的优点是:1便宜;2声音频率可控,能做出“白云石索拉西”的效果;3在某些特殊情况下,控制端口可以与LED一起重复使用。(2)主动蜂鸣器的优点是:程序控制方便。
//主程序查音符码,将不同的初值装入定时器,定时器每中断一次,P37取反一次,形成不同频率的方波
//11代表低音哆 21代表高音哆 1代表中音哆
//12代表低音来 22代表高音来 2代表中音来,是不是翻译乐谱很方便
#include<reg52h>
sbit SPK = P3^7; //P37外接扬声器,可以改成你所说的P1^5
#define uint unsigned int;
#define uchar unsigned char;
uint x,y;
uint i,j,k;
//以下是根据歌谱转来的音乐代码 3代表中音咪 1个全音被分成4个1/4 音符
//小燕子第一句 3,5,21,6,5,5,5,5, 21是高音哆 ,后面4个5,是因为这个音节占4拍
uchar code MUSIC[]={3,5,21,6,5,5,5,5, 3,5,6,21,5,5,5,5, 21,21,21,23,22,22,21,21, 22,21,6,21,5,5,5,5, 3,3,5,6,6,5,6, 21,21,22,5,6,6,6,6, 3,3,1,1,2,2,2,2,2,
2,2,2,3,5,5,5,5, 21,21,2,3,5,5,5,5, 2,1,16,1,15,15,15,15, 0,0,0,0,16,16,1,1, 15,15,16,16,1,1,1,1, 5,5,6,6,4,4,3,3, 2,2,2,2,2,2,5,5, 5,5,2,2,4,4,3,3, 2,2,1,2,4,4,4,4,
16,16,1,1,2,2,4,4, 2,1,16,1,15,15,15,15, 16,16,16,16,15,15,15,15, 15,15,15,15,15,15,15,15, 0,0,5,5,5,5,0,0, 5,5,5,5,4,4,4,4,
5,5,5,5,2,2,2,2, 2,2,5,5,4,4 ,4,4, 16,16,1,1,2,2,2,2, 4,4,2,2,2,2,4,4, 5,5,5,5,5,5,6,6, 21,21,21,21,6,6,21,21,
21,21,6,6,5,5,5,5, 5,5,6,6,5,5,4,4, 2,2,2,2,2,2,5,5, 5,5,2,2,4,4,3,3, 2,2,1,2,4,4,4,4, 16,16,1,1,2,2,4,4,
2,1,16,1,15,15,15,15, 16,16,16,16,15,15,15,15, 5,5,6,6,5,5,6,6, 5,5,4,4,5,5,5,5, 1,1,2,2,1,1,2,2, 15,15,16,16,1,1,1,1,
5,5,6,6,5,5,6,6, 21,21,6,6,5,5,5,5, 1,1,2,2,1,1,2,2, 15,15,16,16,1,1,1,1, 5,5,6,6,5,5,4,4, 5,5,5,5,1,1,2,2, 15,15,16,16,1,1,1,1,
5,5,6,6,5,5,5,5, 1,1,2,2,1,1,1,1, 5,5,6,6,5,5,5,5, 1,1,2,2,1,1,1,1, 5,5,5,5,1,1,1,1, 5,5,5,5,1,1,1,1, 5,5,5,5,1,1,5,5,
1,1,5,5,1,1,5,5, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 0,0,1,1,1,1,0,0, 5,5,5,5,4,4,4,4, 5,5,5,5,2,2,2,2, 2,2,5,5,4,4,4,4,
6,6,1,1,2,2,2,2, 4,4,2,2,2,2,4,4, 5,5,5,5,5,5,6,6, 21,21,21,21,6,6,21,21, 21,21,6,6,5,5,5,5, 5,5,6,6,5,5,4,4, 2,2,2,2,2,2,5,5,
5,5,2,2,4,4,3,3, 2,2,1,22,4,4,4,4, 16,16,1,1,2,2,4,4, 4,4,6,6,5,5,5,5, 5,5,5,5,5,5,5,5, 6,6,6,6,5,5,5,5};
uchar code table[] = { 0,0xfc,0xfc,0xfd,0xfd,0xfd,0xfd,0xfe,0,0,0, //定时半周期的初始值中音123 4 5 67高8位
//三个0是补位码,为了使表格整齐,便于按规律查询 首个0是休止符11个
0xf8,0xf9,0xfa,0xfa,0xfb,0xfb,0xfc,0,0,0, //低音1 2 3 4 5 67高8位 10个
0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xff, //高音1 2 3 4 5 67高8位 7个
0,0x44,0xad,0,0x34,0x82,0xc8,0x06,0,0,0, //中音123 4 5 67低8位 10个
0x89,0x59,0x13,0x68,0x04,0x90,0x0c,0 ,0,0, //低音1 2 3 4 5 6 7低8位10个
0x22,0x56,0x85,0x9a,0xc1,0xe4,0x03}; //高音1 2 3 4 5 6 7低8位 7个
void delay(i) //晶振12MHZ时延时1ms
{
for(j=i;j>0;j--)
for(k=121;k>0;k--);
}
void TIM0_ISR() interrupt 1
{
SPK=!SPK;//定时取反,形成方波
TH0=table[x]; //重新载入定时半周期的初始值
TL0=table[x+28];//为什么加28 11+10+7=28
}
void main(void)
{
TMOD|=0x01;
EA=1;
ET0=1;
while(1)
{
y=0;
while(y<592)//此乐谱里面有592个音符
{
x=MUSIC[y];//查乐谱表
TH0=table[x]; //查不同音节的定时器初值
TL0=table[x+28];
TR0=1; //打开定时器
if(y>72)//两首曲子不同节奏,延时不同
{
delay(125); //延时125ms
}
else
{
delay(250); //延时250ms
}
y++;
TR0=0;
}
}
}
以上就是关于给一段用BEEP()调用蜂鸣器播放简单乐谱的源代码。。我喜欢通过读代码学程序。。。分数不多,好人一生平安全部的内容,包括:给一段用BEEP()调用蜂鸣器播放简单乐谱的源代码。。我喜欢通过读代码学程序。。。分数不多,好人一生平安、无源蜂鸣器与有源蜂鸣器的区别是什么、使用51单片机蜂鸣器播放音乐 求keil程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)