单片机音乐设计.docx
《单片机音乐设计.docx》由会员分享,可在线阅读,更多相关《单片机音乐设计.docx(15页珍藏版)》请在冰豆网上搜索。
![单片机音乐设计.docx](https://file1.bdocx.com/fileroot1/2022-12/16/111c361c-801b-43d5-8bba-e98c63772e41/111c361c-801b-43d5-8bba-e98c63772e411.gif)
单片机音乐设计
单片机音乐设计
题目:
龙的传人
作者:
焦亚琪___
一、龙的传人的简谱
二、龙的传人的硬件结构:
三、龙的传人的程序如下:
#include"AT89X52.H"
#defineF110000//1/4
#defineF220000//1/2
#defineF340000//1/1
unsignedchart0_high=0x00;//T0高8位
unsignedchart0_low=0x00;//T0低8位
unsignedcharYH[21]=
{
0xF7,0xF8,0xF9,0xF9,0xFA,0xFB,0xFB,
0xFB,0xFC,0xFC,0xFC,0xFD,0xFD,0xFD,
0xFD,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE
};
unsignedcharYL[21]=
{
0xD8,0xBC,0x85,0xE5,0x92,0x29,0xB0,
0xEF,0x62,0xC7,0xF5,0x4C,0x97,0xDC,
0xFB,0x34,0x67,0x7D,0xA8,0xCF,0xF1
};
sbitQ=P2^7;
stop1(void)
{
unsignedinti;
TR0=0;
for(i=0;i<=4700;i++);
TR0=1;
}
stop2(void)
{
unsignedinti;
TR0=0;
for(i=0;i<=11400;i++);
TR0=1;
}
delay(n)
{
unsignedinti;
for(i=0;i<=n;i++);
}
voidtimer0()interrupt1//用于产生音符的T0中断服务程序
{
TH0=t0_high;
TL0=t0_low;
Q=~Q;//取反beep引脚,发声
}
voidmain(void)
{
TMOD=0X01;//T0方式1
TR0=0;//关闭T0(不发声)
TH0=0xFF;
TL0=0x00;
ET0=1;//允许T0中断
EA=1;//允许总中断
TR0=1;
while
(1)
{
t0_high=YH[13];
t0_low=YL[13];
delay(F3);
stop1();
t0_high=YH[14];
t0_low=YL[14];
delay(F2);
stop1();
t0_high=YH[15];
t0_low=YL[15];
delay(F2);
stop1();
t0_high=YH[16];
t0_low=YL[16];
delay(F3);
stop1();
t0_high=YH[17];
t0_low=YL[17];
delay(F2);
stop1();
t0_high=YH[16];
t0_low=YL[16];
delay(F2);
stop1();
stop2();
//第一节
t0_high=YH[15];
t0_low=YL[15];
delay(F3);
stop1();
t0_high=YH[15];
t0_low=YL[15];
delay(F2);
stop1();
t0_high=YH[14];
t0_low=YL[14];
delay(F2);
stop1();
t0_high=YH[13];
t0_low=YL[13];
delay(F3+F3+F3);
stop1();
stop2();
//第二节
t0_high=YH[13];
t0_low=YL[13];
delay(F3);
stop1();
t0_high=YH[14];
t0_low=YL[14];
delay(F2);
stop1();
t0_high=YH[15];
t0_low=YL[15];
delay(F2);
stop1();
t0_high=YH[16];
t0_low=YL[16];
delay(F3);
stop1();
t0_high=YH[17];
t0_low=YL[17];
delay(F2);
stop1();
t0_high=YH[16];
t0_low=YL[16];
delay(F2);
stop1();
stop2();
//第三节
t0_high=YH[15];
t0_low=YL[15];
delay(F2);
stop1();
t0_high=YH[14];
t0_low=YL[14];
delay(F2);
stop1();
t0_high=YH[15];
t0_low=YL[15];
delay(F2);
stop1();
t0_high=YH[16];
t0_low=YL[16];
delay(F2);
stop1();
t0_high=YH[17];
t0_low=YL[17];
delay(F3+F3+F3);
stop1();
stop2();
//第四节
t0_high=YH[13];
t0_low=YL[13];
delay(F3);
stop1();
t0_high=YH[14];
t0_low=YL[14];
delay(F2);
stop1();
t0_high=YH[15];
t0_low=YL[15];
delay(F2);
stop1();
t0_high=YH[16];
t0_low=YL[16];
delay(F3);
stop1();
t0_high=YH[17];
t0_low=YL[17];
delay(F2);
stop1();
t0_high=YH[16];
t0_low=YL[16];
delay(F2);
stop1();
stop2();
//第五节
t0_high=YH[15];
t0_low=YL[15];
delay(F3);
stop1();
t0_high=YH[15];
t0_low=YL[15];
delay(F2);
stop1();
t0_high=YH[14];
t0_low=YL[14];
delay(F2);
stop1();
t0_high=YH[13];
t0_low=YL[13];
delay(F3+F3+F3);
stop1();
stop2();
//第六节
t0_high=YH[14];
t0_low=YL[14];
delay(F3);
stop1();
t0_high=YH[14];
t0_low=YL[14];
delay(F3);
stop1();
t0_high=YH[14];
t0_low=YL[14];
delay(F2);
stop1();
t0_high=YH[15];
t0_low=YL[15];
delay(F3);
stop1();
t0_high=YH[14];
t0_low=YL[14];
delay(F2);
stop1();
stop2();
//第七节
t0_high=YH[13];
t0_low=YL[13];
delay(F3);
stop1();
t0_high=YH[13];
t0_low=YL[13];
delay(F2);
stop1();
t0_high=YH[12];
t0_low=YL[12];
delay(F2);
stop1();
t0_high=YH[13];
t0_low=YL[13];
delay(F3+F3+F3);
stop1();
stop2();
//第八节
t0_high=YH[17];
t0_low=YL[17];
delay(F3);
stop1();
t0_high=YH[17];
t0_low=YL[17];
delay(F3);
stop1();
t0_high=YH[17];
t0_low=YL[17];
delay(F3);
stop1();
t0_high=YH[16];
t0_low=YL[16];
delay(F2);
stop1();
t0_high=YH[15];
t0_low=YL[15];
delay(F2);
stop1();
stop2();
//第九节
t0_high=YH[16];
t0_low=YL[16];
delay(F2);
stop1();
t0_high=YH[16];
t0_low=YL[16];
delay(F2);
stop1();
t0_high=YH[17];
t0_low=YL[17];
delay(F2);
stop1();
t0_high=YH[16];
t0_low=YL[16];
delay(F3+F3+F3);
stop1();
stop2();
//第十节
t0_high=YH[15];
t0_low=YL[15];
delay(F3);
stop1();
t0_high=YH[15];
t0_low=YL[15];
delay(F3);
stop1();
t0_high=YH[15];
t0_low=YL[15];
delay(F3);
stop1();
t0_high=YH[16];
t0_low=YL[16];
delay(F2);
stop1();
t0_high=YH[15];
t0_low=YL[15];
delay(F2);
stop1();
stop2();
//第十一节
t0_high=YH[14];
t0_low=YL[14];
delay(F3);
stop1();
t0_high=YH[14];
t0_low=YL[14];
delay(F2);
stop1();
t0_high=YH[15];
t0_low=YL[15];
delay(F2);
stop1();
t0_high=YH[14];
t0_low=YL[14];
delay(F3+F3+F3);
stop1();
stop2();
//第十二节
t0_high=YH[17];
t0_low=YL[17];
delay(F3);
stop1();
t0_high=YH[17];
t0_low=YL[17];
delay(F3);
stop1();
t0_high=YH[17];
t0_low=YL[17];
delay(F3);
stop1();
t0_high=YH[16];
t0_low=YL[16];
delay(F2);
stop1();
t0_high=YH[15];
t0_low=YL[15];
delay(F2);
stop1();
stop2();
//第十三节
t0_high=YH[16];
t0_low=YL[16];
delay(F2);
stop1();
t0_high=YH[16];
t0_low=YL[16];
delay(F2);
stop1();
t0_high=YH[17];
t0_low=YL[17];
delay(F2);
stop1();
t0_high=YH[16];
t0_low=YL[16];
delay(F3+F3+F3);
stop1();
stop2();
//第十四节
t0_high=YH[15];
t0_low=YL[15];
delay(F3);
stop1();
t0_high=YH[15];
t0_low=YL[15];
delay(F3);
stop1();
t0_high=YH[14];
t0_low=YL[14];
delay(F2);
stop1();
t0_high=YH[15];
t0_low=YL[15];
delay(F3);
stop1();
t0_high=YH[14];
t0_low=YL[14];
delay(F2);
stop1();
stop2();
//第十五节
t0_high=YH[13];
t0_low=YL[13];
delay(F2);
stop1();
t0_high=YH[13];
t0_low=YL[13];
delay(F2);
stop1();
t0_high=YH[12];
t0_low=YL[12];
delay(F2);
stop1();
t0_high=YH[13];
t0_low=YL[13];
delay(F3+F3+F3);
stop1();
stop2();
//第十六节
TR0=0;
while
(1);
}
}
四、单片机发声原理
1、用单片机产生音频脉冲音频脉冲激音符发出各种不同的声音,通过计算音频的周期可以得出频率的脉冲。
首先算出某一音频的周期(1/频率),将此音频周期除以2,即为半周期的时间,利用定时器计时这半个周期,如到半个周期时间就将输出脉冲的I/O反相。
重复此计时操作,每当到半周期时间就输出脉冲的I/O反相,这样就可以得到音频的脉冲。
2、用单片机实现音乐的节拍音符节拍依靠延时程序来实现,各调1/4节拍的延时为多少。
3、于蜂鸣器并联的发光二极管会与蜂鸣器同步的亮灭,表明蜂鸣器的工作状态。