单片机数字音乐盒制作proteus仿真原理图:
电路图
单片机源程序:
- #include<reg51.h>
- #define uchar unsigned char
- #define uchar unsigned char
- #define uint unsigned int
- #define LCDPORT P0
- uchar code tab[]="Press key";
- uchar code tab1[]="Select song";
- uchar code tab2[]="First Song" ;
- uchar code tab3[]="butterfly";
- uchar code tab4[]="Second Song" ;
- uchar code tab5[]="Jingle bell ";
- uchar code tab6[]="Third Song" ;
- uchar code tab7[]="Yesterday";
- uchar code tab8[]="Forth Song" ;
- uchar code tab9[]="Youth League";
- sbit LCDE=P2^2;
- sbit LCDRW=P2^1;
- sbit LCDRS=P2^0;
- #define SYSTEM_OSC 12000000 //定义晶振频率12000000HZ
- #define SOUND_SPACE 4/5 //定义普通音符演奏的长度分率,//每4分音符间隔
- sbit BeepIO = P3^7; //定义输出管脚
- sbit K1=P1^7;
- unsigned int code FreTab[12] = { 262,277,294,311,330,349,369,392,415,440,466,494 }; //原始频率表
- unsigned char code SignTab[7] = { 0,2,4,5,7,9,11 }; //1~7在频率表中的位置
- unsigned char code LengthTab[7]= { 1,2,4,8,16,32,64 };
- unsigned char Sound_Temp_TH0,Sound_Temp_TL0; //音符定时器初值暂存
- unsigned char Sound_Temp_TH1,Sound_Temp_TL1;
- unsigned char code Music_Girl[]=
- { 0x1B,0x02, 0x1A,0x02, 0x1B,0x02, 0x19,0x66, 0x1A,0x03,
- 0x18,0x02, 0x17,0x02, 0x16,0x0D, 0x17,0x03, 0x18,0x0D,
- 0x17,0x03, 0x19,0x66, 0x17,0x03, 0x16,0x0D, 0x17,0x03,
- 0x19,0x0D, 0x16,0x03, 0x17,0x0D, 0x18,0x03, 0x17,0x0D,
- 0x16,0x03, 0x15,0x00, 0x19,0x02, 0x11,0x02, 0x16,0x02,
- 0x10,0x02, 0x15,0x02, 0x0F,0x00, 0x10,0x03, 0x15,0x03,
- 0x0F,0x00, 0x0D,0x01, 0x0F,0x66, 0x10,0x03, 0x15,0x66,
- 0x16,0x03, 0x10,0x0D, 0x15,0x03, 0x0F,0x03, 0x19,0x66,
- 0x1F,0x03, 0x1A,0x0D, 0x19,0x03, 0x17,0x03, 0x19,0x03,
- 0x16,0x00, 0x16,0x66, 0x17,0x03, 0x11,0x0C, 0x10,0x02,
- 0x19,0x66, 0x10,0x03, 0x15,0x02, 0x16,0x02, 0x0D,0x02,
- 0x15,0x02, 0x10,0x0D, 0x0F,0x03, 0x10,0x0D, 0x15,0x03,
- 0x0F,0x00, 0x17,0x66, 0x19,0x03, 0x11,0x02, 0x16,0x02,
- 0x10,0x0D, 0x15,0x03, 0x0F,0x15, 0x0D,0x03, 0x0F,0x02,
- 0x0D,0x03, 0x0F,0x0D, 0x10,0x03, 0x11,0x0D, 0x16,0x03,
- 0x10,0x00, 0x0F,0x0D, 0x10,0x03, 0x15,0x66, 0x16,0x03,
- 0x19,0x02, 0x17,0x02, 0x16,0x02, 0x17,0x0D, 0x16,0x03,
- 0x15,0x02, 0x10,0x0D, 0x0F,0x03, 0x0D,0x01, 0x15,0x01,
- 0x10,0x03, 0x15,0x04, 0x10,0x0D, 0x0F,0x03, 0x0D,0x0D,
- 0x0F,0x03, 0x10,0x0D, 0x15,0x03, 0x0F,0x00, 0x17,0x0D,
- 0x19,0x03, 0x16,0x0D, 0x17,0x03, 0x16,0x0D, 0x15,0x03,
- 0x11,0x02, 0x10,0x02, 0x0F,0x00, 0x00,0x00 };
- unsigned char code Music_Jingle[]={
- 0x0F,0x03, 0x0F,0x03, 0x17,0x03, 0x16,0x03, 0x15,0x03,
- 0x0F,0x66, 0x19,0x04, 0x19,0x04, 0x0F,0x03, 0x17,0x03,
- 0x16,0x03, 0x15,0x03, 0x10,0x02, 0x10,0x03, 0x10,0x03,
- 0x18,0x03, 0x17,0x03, 0x16,0x03, 0x10,0x66, 0x10,0x03,
- 0x19,0x03, 0x19,0x03, 0x18,0x03, 0x16,0x03, 0x17,0x02,
- 0x15,0x03, 0x0F,0x03, 0x0F,0x03, 0x17,0x03, 0x16,0x03,
- 0x15,0x03, 0x0F,0x66, 0x0F,0x03, 0x0F,0x03, 0x17,0x03,
- 0x16,0x03, 0x15,0x03, 0x10,0x66, 0x10,0x03, 0x10,0x03,
- 0x18,0x03, 0x17,0x03, 0x16,0x03, 0x19,0x03, 0x19,0x03,
- 0x19,0x03, 0x19,0x03, 0x1A,0x03, 0x19,0x03, 0x18,0x03,
- 0x16,0x03, 0x15,0x66, 0x17,0x03, 0x17,0x03, 0x17,0x02,
- 0x17,0x03, 0x17,0x03, 0x17,0x02, 0x17,0x03, 0x19,0x03,
- 0x15,0x03, 0x16,0x03, 0x17,0x01, 0x18,0x03, 0x18,0x03,
- 0x18,0x67, 0x18,0x04, 0x18,0x03, 0x17,0x03, 0x17,0x03,
- 0x17,0x04, 0x17,0x04, 0x17,0x03, 0x16,0x03, 0x16,0x03,
- 0x15,0x03, 0x16,0x03, 0x19,0x66, 0x19,0x03, 0x19,0x03,
- 0x18,0x03, 0x16,0x03, 0x15,0x02, 0x00,0x00 };
- unsigned char code Music_Two[] ={
- 0x17,0x03, 0x16,0x03, 0x17,0x01, 0x16,0x03, 0x17,0x03,
- 0x16,0x03, 0x15,0x01, 0x10,0x03, 0x15,0x03, 0x16,0x02,
- 0x16,0x0D, 0x17,0x03, 0x16,0x03, 0x15,0x03, 0x10,0x03,
- 0x10,0x0E, 0x15,0x04, 0x0F,0x01, 0x17,0x03, 0x16,0x03,
- 0x17,0x01, 0x16,0x03, 0x17,0x03, 0x16,0x03, 0x15,0x01,
- 0x10,0x03, 0x15,0x03, 0x16,0x02, 0x16,0x0D, 0x17,0x03,
- 0x16,0x03, 0x15,0x03, 0x10,0x03, 0x15,0x03, 0x16,0x01,
- 0x17,0x03, 0x16,0x03, 0x17,0x01, 0x16,0x03, 0x17,0x03,
- 0x16,0x03, 0x15,0x01, 0x10,0x03, 0x15,0x03, 0x16,0x02,
- 0x16,0x0D, 0x17,0x03, 0x16,0x03, 0x15,0x03, 0x10,0x03,
- 0x10,0x0E, 0x15,0x04, 0x0F,0x01, 0x17,0x03, 0x19,0x03,
- 0x19,0x01, 0x19,0x03, 0x1A,0x03, 0x19,0x03, 0x17,0x01,
- 0x16,0x03, 0x16,0x03, 0x16,0x02, 0x16,0x0D, 0x17,0x03,
- 0x16,0x03, 0x15,0x03, 0x10,0x03, 0x10,0x0D, 0x15,0x00,
- 0x19,0x03, 0x19,0x03, 0x1A,0x03, 0x1F,0x03, 0x1B,0x03,
- 0x1B,0x03, 0x1A,0x03, 0x17,0x0D, 0x16,0x03, 0x16,0x03,
- 0x16,0x0D, 0x17,0x01, 0x17,0x03, 0x17,0x03, 0x19,0x03,
- 0x1A,0x02, 0x1A,0x02, 0x10,0x03, 0x17,0x0D, 0x16,0x03,
- 0x16,0x01, 0x17,0x03, 0x19,0x03, 0x19,0x03, 0x17,0x03,
- 0x19,0x02, 0x1F,0x02, 0x1B,0x03, 0x1A,0x03, 0x1A,0x0E,
- 0x1B,0x04, 0x17,0x02, 0x1A,0x03, 0x1A,0x03, 0x1A,0x0E,
- 0x1B,0x04, 0x1A,0x03, 0x19,0x03, 0x17,0x03, 0x16,0x03,
- 0x17,0x0D, 0x16,0x03, 0x17,0x03, 0x19,0x01, 0x19,0x03,
- 0x19,0x03, 0x1A,0x03, 0x1F,0x03, 0x1B,0x03, 0x1B,0x03,
- 0x1A,0x03, 0x17,0x0D, 0x16,0x03, 0x16,0x03, 0x16,0x03,
- 0x17,0x01, 0x17,0x03, 0x17,0x03, 0x19,0x03, 0x1A,0x02,
- 0x1A,0x02, 0x10,0x03, 0x17,0x0D, 0x16,0x03, 0x16,0x01,
- 0x17,0x03, 0x19,0x03, 0x19,0x03, 0x17,0x03, 0x19,0x03,
- 0x1F,0x02, 0x1B,0x03, 0x1A,0x03, 0x1A,0x0E, 0x1B,0x04,
- 0x17,0x02, 0x1A,0x03, 0x1A,0x03, 0x1A,0x0E, 0x1B,0x04,
- 0x17,0x16, 0x1A,0x03, 0x1A,0x03, 0x1A,0x0E, 0x1B,0x04,
- 0x1A,0x03, 0x19,0x03, 0x17,0x03, 0x16,0x03, 0x0F,0x02,
- 0x10,0x03, 0x15,0x00, 0x00,0x00 };
- unsigned char code Music_Yesterday[]={
- 0x15,0x03, 0x15,0x03, 0x16,0x03, 0x17,0x02, 0x19,0x02,
- 0x19,0x03, 0x17,0x03, 0x19,0x03, 0x17,0x03, 0x1A,0x02,
- 0x19,0x66, 0x17,0x02, 0x17,0x03, 0x19,0x03, 0x1A,0x02,
- 0x16,0x02, 0x17,0x03, 0x19,0x02, 0x1A,0x15, 0x17,0x03,
- 0x19,0x03, 0x1A,0x02, 0x21,0x02, 0x20,0x03, 0x1F,0x02,
- 0x1B,0x03, 0x1B,0x66, 0x19,0x03, 0x17,0x03, 0x19,0x02,
- 0x17,0x02, 0x16,0x14, 0x15,0x03, 0x15,0x03, 0x16,0x03,
- 0x17,0x03, 0x19,0x02, 0x19,0x03, 0x19,0x03, 0x17,0x03,
- 0x19,0x03, 0x17,0x03, 0x1A,0x03, 0x19,0x02, 0x17,0x03,
- 0x17,0x02, 0x17,0x03, 0x19,0x03, 0x1A,0x02, 0x1B,0x02,
- 0x17,0x03, 0x19,0x02, 0x1A,0x03, 0x1A,0x01, 0x1B,0x02,
- 0x20,0x02, 0x1F,0x03, 0x1B,0x03, 0x1A,0x66, 0x1B,0x03,
- 0x1F,0x03, 0x1B,0x03, 0x1F,0x03, 0x1B,0x02, 0x1A,0x66,
- 0x1A,0x03, 0x1B,0x03, 0x1F,0x02, 0x1F,0x02, 0x1A,0x03,
- 0x1F,0x02, 0x20,0x03, 0x20,0x16, 0x1F,0x02, 0x20,0x02,
- 0x21,0x03, 0x21,0x03, 0x21,0x03, 0x21,0x02, 0x20,0x03,
- 0x1F,0x03, 0x1B,0x03, 0x1F,0x03, 0x1B,0x03, 0x1A,0x66,
- 0x17,0x03, 0x19,0x03, 0x19,0x14, 0x1F,0x03, 0x20,0x03,
- 0x21,0x03, 0x21,0x03, 0x21,0x03, 0x21,0x03, 0x21,0x02,
- 0x20,0x03, 0x1F,0x03, 0x1F,0x03, 0x1B,0x03, 0x1A,0x66,
- 0x17,0x03, 0x19,0x03, 0x19,0x14, 0x1A,0x03, 0x1B,0x03,
- 0x1F,0x03, 0x1B,0x03, 0x1F,0x03, 0x1B,0x03, 0x1F,0x03,
- 0x20,0x66, 0x1F,0x03, 0x1B,0x03, 0x1F,0x03, 0x1B,0x02,
- 0x1F,0x03, 0x20,0x66, 0x1F,0x03, 0x20,0x03, 0x21,0x02,
- 0x21,0x02, 0x20,0x03, 0x1F,0x02, 0x1A,0x03, 0x1A,0x02,
- 0x17,0x02, 0x17,0x03, 0x1A,0x03, 0x17,0x03, 0x19,0x03,
- 0x19,0x14, 0x17,0x03, 0x17,0x03, 0x16,0x04, 0x15,0x04,
- 0x15,0x03, 0x16,0x03, 0x17,0x01, 0x17,0x03, 0x18,0x02,
- 0x16,0x03, 0x16,0x14, 0x17,0x03, 0x17,0x03, 0x17,0x03,
- 0x18,0x02, 0x16,0x03, 0x16,0x16, 0x15,0x03, 0x16,0x03,
- 0x17,0x03, 0x16,0x01, 0x1F,0x02, 0x20,0x02, 0x21,0x03,
- 0x21,0x03, 0x21,0x03, 0x21,0x02, 0x20,0x03, 0x1F,0x03,
- 0x1B,0x03, 0x1F,0x03, 0x1B,0x03, 0x1A,0x03, 0x1A,0x02,
- 0x17,0x03, 0x19,0x03, 0x19,0x14, 0x1F,0x02, 0x20,0x02,
- 0x21,0x03, 0x21,0x03, 0x21,0x03, 0x21,0x66, 0x20,0x03,
- 0x1F,0x03, 0x1B,0x03, 0x1F,0x03, 0x1B,0x03, 0x1A,0x66,
- 0x17,0x03, 0x19,0x03, 0x19,0x00, 0x00,0x00 };
- uchar k;
- void delay1(void) //延时程序
- {
- uchar i,j;
- for(i=20;i>0;i--)
- for(j=248;j>0;j--);
- }
- void Getch ( )
- { uchar X,Y,Z;
- P1=0xff;
- P1=0xf3; //先对P0置数 行扫描
- if(P1!=0xf3) //判断是否有键按下
- { delay1(); //延时,软件去干扰
- if(P1!=0xf3) //确认按键按下X = P3;
- {
- X=P1; //保存行扫描时有键按下时状态
- P1=0xfc; //列扫描
- Y=P1; //保存列扫描时有键按下时状态
- Z=X|Y;
- //取出键值
- switch ( Z ) //判断键值(那一个键按下)
- {
- case 0xf9: k=0; break; //对键值赋值
- case 0xf5: k=1; break;
- case 0xfa: k=2; break;
- case 0xf6: k=3; break;
-
- }
- }
- }
- }
- void Delay()
- { uint uiCount;
- for(uiCount=0;uiCount<250;uiCount++);
- }
- void WriteCMD(uchar Commond)//LCD写指令函数
- {
- Delay(); //先延时。
- LCDE=1; //然后把LCD改为写入命令状态。
- LCDRS=0;
- LCDRW=0;
- LCDPORT=Commond; //再输出命令。
- LCDE=0; //最后执行命令。
- }
- void WriteData(uchar dat)//LCD写数据函数
- { Delay(); //先延时。
- LCDE=1; //然后把LCD改为写入数据状态。
- LCDRS=1;
- LCDRW=0;
- LCDPORT=dat; //再输出数据。
- LCDE=0; //最后显示数据。
- }
- void init()
- {
- LCDRW=0;
- LCDE=0;
- WriteCMD(0x38);
- WriteCMD(0x0c);
- WriteCMD(0x06);
- WriteCMD(0x01);
- WriteCMD(0x80);
- }
- void mydelay(int x)
- { int i,j;
- for(i=x;i>0;i--)
- for(j=255;j>0;j--);
- }
- void InitialSound(void)
- {
- BeepIO = 0;
- Sound_Temp_TH1 = (65535-(1/1200)*SYSTEM_OSC)/256; // 计算TL1应装入的初值 (10ms的初装值)
- Sound_Temp_TL1 = (65535-(1/1200)*SYSTEM_OSC)%256; // 计算TH1应装入的初值
- TH1 = Sound_Temp_TH1;
- TL1 = Sound_Temp_TL1;
- TMOD |= 0x11;
- ET0 = 1;
- ET1 = 0;
- TR0 = 0;
- TR1 = 0;
- EA = 1;
- }
- void BeepTimer0(void) interrupt 1 //音符发生中断
- {
- BeepIO = !BeepIO;
- TH0 = Sound_Temp_TH0;
- TL0 = Sound_Temp_TL0;
- }
- void Play(unsigned char *Sound,unsigned char Signature,unsigned Octachord,unsigned int Speed)
- {
- unsigned int NewFreTab[12]; //新的频率表
- unsigned char i,j;
- unsigned int Point,LDiv,LDiv0,LDiv1,LDiv2,LDiv4,CurrentFre,Temp_T,SoundLength;
- unsigned char Tone,Length,SL,SH,SM,SLen,XG,FD;
- for(i=0;i<12;i++) // 根据调号及升降八度来生成新的频率表
- {
- j = i + Signature;
- if(j > 11)
- {
- j = j-12;
- NewFreTab[i] = FreTab[j]*2;
- }
- else
- NewFreTab[i] = FreTab[j];
- if(Octachord == 1)
- NewFreTab[i]>>=2;
- else if(Octachord == 3)
- NewFreTab[i]<<=2;
- }
-
- SoundLength = 0;
- while(Sound[SoundLength] != 0x00) //计算歌曲长度
- {
- SoundLength+=2;
- }
- Point = 0;
- Tone = Sound[Point];
- Length = Sound[Point+1]; // 读出第一个音符和它时时值
-
- LDiv0 = 12000/Speed; // 算出1分音符的长度(几个10ms)
- LDiv4 = LDiv0/4; // 算出4分音符的长度
- LDiv4 = LDiv4-LDiv4*SOUND_SPACE; // 普通音最长间隔标准
- TR0 = 0;
- TR1 = 1;
- while(Point < SoundLength&&K1==1) //歌曲代码未放完且暂停键未被按下
- {
- SL=Tone%10; //计算出音符
- SM=Tone/10%10; //计算出高低音
- SH=Tone/100; //计算出是否升半
- CurrentFre = NewFreTab[SignTab[SL-1]+SH]; //查出对应音符的频率
- if(SL!=0)
- {
- if (SM==1) CurrentFre >>= 2; //低音
- if (SM==3) CurrentFre <<= 2; //高音
- Temp_T = 65536-(50000/CurrentFre)*10/(12000000/SYSTEM_OSC);//计算计数器初值
- Sound_Temp_TH0 = Temp_T/256;
- Sound_Temp_TL0 = Temp_T%256;
- TH0 = Sound_Temp_TH0;
- TL0 = Sound_Temp_TL0 + 12; //加12是对中断延时的补偿
- }
- SLen=LengthTab[Length%10]; //算出是几分音符
- XG=Length/10%10; //算出音符类型(0普通1连音2顿音)
- FD=Length/100;
- LDiv=LDiv0/SLen; //算出连音音符演奏的长度(多少个10ms)
- if (FD==1)
- LDiv=LDiv+LDiv/2;
- if(XG!=1)
- if(XG==0) //算出普通音符的演奏长度
- if (SLen<=4)
- LDiv1=LDiv-LDiv4;
- else
- LDiv1=LDiv*SOUND_SPACE;
- else
- LDiv1=LDiv/2; //算出顿音的演奏长度
- else
- LDiv1=LDiv;
- if(SL==0) LDiv1=0;
- LDiv2=LDiv-LDiv1; //算出不发音的长度
- if (SL!=0)
- {
- TR0=1;
- for(i=LDiv1;i>0;i--) //发规定长度的音
- {
- while(TF1==0);
- TH1 = Sound_Temp_TH1;
- TL1 = Sound_Temp_TL1;
- TF1=0;
- }
- }
- if(LDiv2!=0)
- {
- TR0=0; BeepIO=0;
- for(i=LDiv2;i>0;i--) //音符间的间隔
- {
- while(TF1==0);
- TH1 = Sound_Temp_TH1;
- TL1 = Sound_Temp_TL1;
- TF1=0;
- }
- }
- Point+=2;
- Tone=Sound[Point];
- Length=Sound[Point+1];
- }
- BeepIO = 0;
- }
- main()
- { uint i;
- k=8;
- init();
- for(i=0;i<15;i++)
- {
- WriteData(tab[i]); //LCD写数据函数写第一行
- mydelay(50);
- }
- mydelay(1000);
- WriteCMD(0x80+0x40+2); //将指针设到第二行空两字符
- for(i=0;i<6;i++)
- {
- WriteData(tab1[i]);
- mydelay(50);
- }
- InitialSound(); //发音初始化程序
- while(1)
- { Getch(); //扫描键盘,获得键值
- if(k==0) //若键值为0
- { uint i;
- init();
- for(i=0;i<10;i++)
- {
- WriteData(tab2[i]); //写第一首歌序号
- mydelay(50);
- }
- mydelay(1000);
- WriteCMD(0x80+0x40); //转到第二行
- for(i=0;i<9;i++)
- { WriteData(tab3[i]); //写歌名
- mydelay(50);
- }
- Play(Music_Girl,0,3,360); //播放第一首歌
- mydelay(500);
- break;
- }
- else if (k==1) //若键值为1
- { uint i;
- init();
- for(i=0;i<11;i++)
- { WriteData(tab4[i]); //写第二首歌序号
- mydelay(50);
- }
- mydelay(1000);
- WriteCMD(0x80+0x40); //转到第二行
- for(i=0;i<11;i++)
- {
- WriteData(tab5[i]); //写歌名
- mydelay(50);
- }
- Play(Music_Jingle,0,3,360); //播放第二首歌
- mydelay(500);
- break;
- }
- else if (k==2) //若键值为2
- { uint i;
- init();
- for(i=0;i<10;i++)
- { WriteData(tab6[i]); //写第三首歌序号
- mydelay(50);
- }
- mydelay(1000);
- //WriteCMD(0x01);
- WriteCMD(0x80+0x40); //转到第二行
- for(i=0;i<6;i++)
- {
- WriteData(tab7[i]); //写歌名
- mydelay(50);
- }
- Play(Music_Two,0,3,360); //播放第三首歌
- mydelay(500);
-
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
1 概述
传统的音乐盒多是机械音乐盒,其工作原理是通过齿轮带动一个带有铁钉的铁桶转动,铁桶上的铁钉撞击铁片制成的琴键,从而发出声音。但是,机械式的音乐盒体积比较大,比较笨重,且发音单调。水、灰尘等外在因素,容易使内部金属发音条变形,从而造成发音跑调。另外,机械音乐盒放音时为了让音色稳定,必须放平不能动摇,而且价格昂贵,不能实现大批量生产。基于单片机设计制作的电子式音乐盒。与传统的机械式音乐盒相比更小巧,音质更优美且能演奏和弦音乐。电子式音乐盒动力来源是电池,制作工艺简单,可进行批量生产,所以价格便宜。基于单片机制作的电子式音乐盒,控制功能强大,可根据需要选歌,使用方便。所放歌曲的节奏可以根据需要进行设置,根据存储容量的大小,可以尽可能多的存储歌曲。另外,可以设计彩灯外观效果,增设放歌时间、序号显示灯功能,使音乐盒的功能更加丰富。
1.1单片机数字音乐盒有关介绍 电子音乐已广泛地应用于社会生活的各个领域。其类型从音乐卡片到CD、MP3 等多种多样,制作原理也各不相同。声音是通过振动产生的。单片机对某一I/O引脚以一定的频率循环置1和清0,这一引脚便产生一定频率的方波,该方波通过放大后作用于扬声器便产生一定频率的声音。若改变输出方波的频率,产生的声音也就改变了。通过控制输出方波的时间长短,声音的长短也就得到控制。因此,根据乐谱,单片机就可产生电子音乐。音乐中最关键的两个要素是音符和节拍。单片机控制的音乐发生器系统由硬件电路和软件两部分构成。利用单片机控制的电子音乐发生器软硬件上具有独特的优点,系统的开发周期短,成本低,电路制作容易。更换歌曲时,硬件电路无需作任何修改,只需修改软件即可实现。软件编程时,可用51系列单片机的汇编语言或C51语言实现。同时还可根据个人的习好通过软件改变节拍的延时时间,增加电子音乐的趣味性。
1.1.1发音原理介绍
发音原理:播放一段音乐需要的是两个元素,一个是音调,另一个是音符。首先要了解对应的音调,音调主要由声音的频率决定,同时也与声音强度有关。对一定强度的纯音,音调随频率的升降而升降;对一定频率的纯音、低频纯音的音调随声强增加而下降,高频纯音的音调却随强度增加而上升。另外,音符的频率有所不同。基于上面的内容,这样就对发音的原理有了一些初步的了解。
音符的发音主要靠不同的音频脉冲。利用单片机的内部定时器/计数器0,使其工作在模式1,定时中断,然后控制P3.7引脚的输出音乐。只要算出某一音频的周期(1/频率),然后将此周期除以2,即为半周期的时间,利用定时器计时这个半周期时间,每当计时到后就将输出脉冲的I/O反相,然后重复计时此半周期时间再对I/O反相,就可在I/O脚上得到此频率的脉冲。
1.1.2 音符频率的产生
音符及定时器初始值:
例如:中音1(do)的音频=523HZ,周期T=1/523s=1912
定时器/计数器0的定时时间为:T/2=1912/2
定时器956的计数值=定时时间/机器周期=956/1=956(时钟频率=12MHZ)
装入T0计数器初值为65536-956=64580 将64580装入T0寄存器中,启动T0工作后,每计数956次时将产生溢出中断,进入中断服务时,每次对P3.0引脚的输出值进行取反,就可得到中音DO(523HZ)的音符音频。将51单片机内部定时器工作在计数器模式1下,改变计数初值TH0,TL0以产生不同的频率。
若该设计使用数字电路完成,所设计的电路相当复杂,大概需要十几片数字集成块,其功能也主要依赖于数字电路的各功能模块的组合来实现,焊接的过程比较复杂,成本也非常高。若用单片机来设计制作完成,由于其功能的实现主要通过软件编程来完成,那么就降低了硬件电路的复杂性,而且其成本也有所降低,所以在该设计中采用单片机利用AT89C51,它是低功耗、高性能的CMOS型8位单片机。片内带有4KB的Flash存储器,且允许在系统内改写或用编程器编程。另外, AT89C51的指令系统和引脚与8051完全兼容,片内有128B 的RAM、32条I/O口线、2个16位定时计数器、5个中断源、一个全双工串行口等.
完整的音乐盒制作资料下载:
音乐盒(1).zip
(53.96 KB, 下载次数: 827)
|