51单片机_音乐
Posted clam-hao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了51单片机_音乐相关的知识,希望对你有一定的参考价值。
#include "reg52.h" //此文件中定义了单片机的一些特殊功能寄存器 #include "SoundPlay.h" #include"music.h" typedef unsigned int u16; //对数据类型进行声明定义 typedef unsigned char u8; void Delay1ms(unsigned int count) { unsigned int i,j; for(i=0;i<count;i++) for(j=0;j<120;j++); } void main() { InitialSound(); while(1) { Play(Music_Girl,0,3,360); Delay1ms(500); Play(Music_Same,0,3,360); Delay1ms(500); Play(Music_Two,0,3,360); Delay1ms(500); } }
1 #ifndef __MUSIC_H__ 2 #define __MUSIC_H__ 3 //*****************************Music****************************************************** 4 //挥着翅膀的女孩 5 unsigned char code Music_Girl[]={ 0x17,0x02, 0x17,0x03, 0x18,0x03, 0x19,0x02, 0x15,0x03, 6 0x16,0x03, 0x17,0x03, 0x17,0x03, 0x17,0x03, 0x18,0x03, 7 0x19,0x02, 0x16,0x03, 0x17,0x03, 0x18,0x02, 0x18,0x03, 8 0x17,0x03, 0x15,0x02, 0x18,0x03, 0x17,0x03, 0x18,0x02, 9 0x10,0x03, 0x15,0x03, 0x16,0x02, 0x15,0x03, 0x16,0x03, 10 0x17,0x02, 0x17,0x03, 0x18,0x03, 0x19,0x02, 0x1A,0x03, 11 0x1B,0x03, 0x1F,0x03, 0x1F,0x03, 0x17,0x03, 0x18,0x03, 12 0x19,0x02, 0x16,0x03, 0x17,0x03, 0x18,0x03, 0x17,0x03, 13 0x18,0x03, 0x1F,0x03, 0x1F,0x02, 0x16,0x03, 0x17,0x03, 14 0x18,0x03, 0x17,0x03, 0x18,0x03, 0x20,0x03, 0x20,0x02, 15 0x1F,0x03, 0x1B,0x03, 0x1F,0x66, 0x20,0x03, 0x21,0x03, 16 0x20,0x03, 0x1F,0x03, 0x1B,0x03, 0x1F,0x66, 0x1F,0x03, 17 0x1B,0x03, 0x19,0x03, 0x19,0x03, 0x15,0x03, 0x1A,0x66, 18 0x1A,0x03, 0x19,0x03, 0x15,0x03, 0x15,0x03, 0x17,0x03, 19 0x16,0x66, 0x17,0x04, 0x18,0x04, 0x18,0x03, 0x19,0x03, 20 0x1F,0x03, 0x1B,0x03, 0x1F,0x66, 0x20,0x03, 0x21,0x03, 21 0x20,0x03, 0x1F,0x03, 0x1B,0x03, 0x1F,0x66, 0x1F,0x03, 22 0x1B,0x03, 0x19,0x03, 0x19,0x03, 0x15,0x03, 0x1A,0x66, 23 0x1A,0x03, 0x19,0x03, 0x19,0x03, 0x1F,0x03, 0x1B,0x03, 24 0x1F,0x00, 0x1A,0x03, 0x1A,0x03, 0x1A,0x03, 0x1B,0x03, 25 0x1B,0x03, 0x1A,0x03, 0x19,0x03, 0x19,0x02, 0x17,0x03, 26 0x15,0x17, 0x15,0x03, 0x16,0x03, 0x17,0x03, 0x18,0x03, 27 0x17,0x04, 0x18,0x0E, 0x18,0x03, 0x17,0x04, 0x18,0x0E, 28 0x18,0x66, 0x17,0x03, 0x18,0x03, 0x17,0x03, 0x18,0x03, 29 0x20,0x03, 0x20,0x02, 0x1F,0x03, 0x1B,0x03, 0x1F,0x66, 30 0x20,0x03, 0x21,0x03, 0x20,0x03, 0x1F,0x03, 0x1B,0x03, 31 0x1F,0x66, 0x1F,0x04, 0x1B,0x0E, 0x1B,0x03, 0x19,0x03, 32 0x19,0x03, 0x15,0x03, 0x1A,0x66, 0x1A,0x03, 0x19,0x03, 33 0x15,0x03, 0x15,0x03, 0x17,0x03, 0x16,0x66, 0x17,0x04, 34 0x18,0x04, 0x18,0x03, 0x19,0x03, 0x1F,0x03, 0x1B,0x03, 35 0x1F,0x66, 0x20,0x03, 0x21,0x03, 0x20,0x03, 0x1F,0x03, 36 0x1B,0x03, 0x1F,0x66, 0x1F,0x03, 0x1B,0x03, 0x19,0x03, 37 0x19,0x03, 0x15,0x03, 0x1A,0x66, 0x1A,0x03, 0x19,0x03, 38 0x19,0x03, 0x1F,0x03, 0x1B,0x03, 0x1F,0x00, 0x18,0x02, 39 0x18,0x03, 0x1A,0x03, 0x19,0x0D, 0x15,0x03, 0x15,0x02, 40 0x18,0x66, 0x16,0x02, 0x17,0x02, 0x15,0x00, 0x00,0x00}; 41 //同一首歌 42 const unsigned char code Music_Same[]={ 0x0F,0x01, 0x15,0x02, 0x16,0x02, 0x17,0x66, 0x18,0x03, 43 0x17,0x02, 0x15,0x02, 0x16,0x01, 0x15,0x02, 0x10,0x02, 44 0x15,0x00, 0x0F,0x01, 0x15,0x02, 0x16,0x02, 0x17,0x02, 45 0x17,0x03, 0x18,0x03, 0x19,0x02, 0x15,0x02, 0x18,0x66, 46 0x17,0x03, 0x19,0x02, 0x16,0x03, 0x17,0x03, 0x16,0x00, 47 0x17,0x01, 0x19,0x02, 0x1B,0x02, 0x1B,0x70, 0x1A,0x03, 48 0x1A,0x01, 0x19,0x02, 0x19,0x03, 0x1A,0x03, 0x1B,0x02, 49 0x1A,0x0D, 0x19,0x03, 0x17,0x00, 0x18,0x66, 0x18,0x03, 50 0x19,0x02, 0x1A,0x02, 0x19,0x0C, 0x18,0x0D, 0x17,0x03, 51 0x16,0x01, 0x11,0x02, 0x11,0x03, 0x10,0x03, 0x0F,0x0C, 52 0x10,0x02, 0x15,0x00, 0x1F,0x01, 0x1A,0x01, 0x18,0x66, 53 0x19,0x03, 0x1A,0x01, 0x1B,0x02, 0x1B,0x03, 0x1B,0x03, 54 0x1B,0x0C, 0x1A,0x0D, 0x19,0x03, 0x17,0x00, 0x1F,0x01, 55 0x1A,0x01, 0x18,0x66, 0x19,0x03, 0x1A,0x01, 0x10,0x02, 56 0x10,0x03, 0x10,0x03, 0x1A,0x0C, 0x18,0x0D, 0x17,0x03, 57 0x16,0x00, 0x0F,0x01, 0x15,0x02, 0x16,0x02, 0x17,0x70, 58 0x18,0x03, 0x17,0x02, 0x15,0x03, 0x15,0x03, 0x16,0x66, 59 0x16,0x03, 0x16,0x02, 0x16,0x03, 0x15,0x03, 0x10,0x02, 60 0x10,0x01, 0x11,0x01, 0x11,0x66, 0x10,0x03, 0x0F,0x0C, 61 0x1A,0x02, 0x19,0x02, 0x16,0x03, 0x16,0x03, 0x18,0x66, 62 0x18,0x03, 0x18,0x02, 0x17,0x03, 0x16,0x03, 0x19,0x00, 63 0x00,0x00 }; 64 //两只蝴蝶 65 const unsigned char code Music_Two[] ={ 0x17,0x03, 0x16,0x03, 0x17,0x01, 0x16,0x03, 0x17,0x03, 66 0x16,0x03, 0x15,0x01, 0x10,0x03, 0x15,0x03, 0x16,0x02, 67 0x16,0x0D, 0x17,0x03, 0x16,0x03, 0x15,0x03, 0x10,0x03, 68 0x10,0x0E, 0x15,0x04, 0x0F,0x01, 0x17,0x03, 0x16,0x03, 69 0x17,0x01, 0x16,0x03, 0x17,0x03, 0x16,0x03, 0x15,0x01, 70 0x10,0x03, 0x15,0x03, 0x16,0x02, 0x16,0x0D, 0x17,0x03, 71 0x16,0x03, 0x15,0x03, 0x10,0x03, 0x15,0x03, 0x16,0x01, 72 0x17,0x03, 0x16,0x03, 0x17,0x01, 0x16,0x03, 0x17,0x03, 73 0x16,0x03, 0x15,0x01, 0x10,0x03, 0x15,0x03, 0x16,0x02, 74 0x16,0x0D, 0x17,0x03, 0x16,0x03, 0x15,0x03, 0x10,0x03, 75 0x10,0x0E, 0x15,0x04, 0x0F,0x01, 0x17,0x03, 0x19,0x03, 76 0x19,0x01, 0x19,0x03, 0x1A,0x03, 0x19,0x03, 0x17,0x01, 77 0x16,0x03, 0x16,0x03, 0x16,0x02, 0x16,0x0D, 0x17,0x03, 78 0x16,0x03, 0x15,0x03, 0x10,0x03, 0x10,0x0D, 0x15,0x00, 79 0x19,0x03, 0x19,0x03, 0x1A,0x03, 0x1F,0x03, 0x1B,0x03, 80 0x1B,0x03, 0x1A,0x03, 0x17,0x0D, 0x16,0x03, 0x16,0x03, 81 0x16,0x0D, 0x17,0x01, 0x17,0x03, 0x17,0x03, 0x19,0x03, 82 0x1A,0x02, 0x1A,0x02, 0x10,0x03, 0x17,0x0D, 0x16,0x03, 83 0x16,0x01, 0x17,0x03, 0x19,0x03, 0x19,0x03, 0x17,0x03, 84 0x19,0x02, 0x1F,0x02, 0x1B,0x03, 0x1A,0x03, 0x1A,0x0E, 85 0x1B,0x04, 0x17,0x02, 0x1A,0x03, 0x1A,0x03, 0x1A,0x0E, 86 0x1B,0x04, 0x1A,0x03, 0x19,0x03, 0x17,0x03, 0x16,0x03, 87 0x17,0x0D, 0x16,0x03, 0x17,0x03, 0x19,0x01, 0x19,0x03, 88 0x19,0x03, 0x1A,0x03, 0x1F,0x03, 0x1B,0x03, 0x1B,0x03, 89 0x1A,0x03, 0x17,0x0D, 0x16,0x03, 0x16,0x03, 0x16,0x03, 90 0x17,0x01, 0x17,0x03, 0x17,0x03, 0x19,0x03, 0x1A,0x02, 91 0x1A,0x02, 0x10,0x03, 0x17,0x0D, 0x16,0x03, 0x16,0x01, 92 0x17,0x03, 0x19,0x03, 0x19,0x03, 0x17,0x03, 0x19,0x03, 93 0x1F,0x02, 0x1B,0x03, 0x1A,0x03, 0x1A,0x0E, 0x1B,0x04, 94 0x17,0x02, 0x1A,0x03, 0x1A,0x03, 0x1A,0x0E, 0x1B,0x04, 95 0x17,0x16, 0x1A,0x03, 0x1A,0x03, 0x1A,0x0E, 0x1B,0x04, 96 0x1A,0x03, 0x19,0x03, 0x17,0x03, 0x16,0x03, 0x0F,0x02, 97 0x10,0x03, 0x15,0x00, 0x00,0x00 }; 98 //*********************************************************************************** 99 #endif
1 /****************************************************************************** 2 曲谱存贮格式 unsigned char code MusicName{音高,音长,音高,音长...., 0,0}; 末尾:0,0 表示结束(Important) 3 4 音高由三位数字组成: 5 个位是表示 1~7 这七个音符 6 十位是表示音符所在的音区:1-低音,2-中音,3-高音; 7 百位表示这个音符是否要升半音: 0-不升,1-升半音。 8 9 音长最多由三位数字组成: 10 个位表示音符的时值,其对应关系是: 11 |数值(n): |0 |1 |2 |3 | 4 | 5 | 6 12 |几分音符: |1 |2 |4 |8 |16 |32 |64 音符=2^n 13 十位表示音符的演奏效果(0-2): 0-普通,1-连音,2-顿音 14 百位是符点位: 0-无符点,1-有符点 15 16 调用演奏子程序的格式 17 Play(乐曲名,调号,升降八度,演奏速度); 18 |乐曲名 : 要播放的乐曲指针,结尾以(0,0)结束; 19 |调号(0-11) : 是指乐曲升多少个半音演奏; 20 |升降八度(1-3) : 1:降八度, 2:不升不降, 3:升八度; 21 |演奏速度(1-12000): 值越大速度越快; 22 23 ***************************************************************************/ 24 #ifndef __SOUNDPLAY_H_REVISION_FIRST__ 25 #define __SOUNDPLAY_H_REVISION_FIRST__ 26 27 //************************************************************************** 28 #include"music.h" 29 30 #define SYSTEM_OSC 12000000 //定义晶振频率12000000HZ 31 #define SOUND_SPACE 4/5 //定义普通音符演奏的长度分率,//每4分音符间隔 32 sbit BeepIO = P1^5; //定义输出管脚 33 unsigned char value=0; 34 unsigned int code FreTab[12] = { 262,277,294,311,330,349,369,392,415,440,466,494 }; //原始频率表 35 unsigned char code SignTab[7] = { 0,2,4,5,7,9,11 }; //1~7在频率表中的位置 36 unsigned char code LengthTab[7]= { 1,2,4,8,16,32,64 }; 37 unsigned char Sound_Temp_TH0,Sound_Temp_TL0; //音符定时器初值暂存 38 unsigned char Sound_Temp_TH1,Sound_Temp_TL1; //音长定时器初值暂存 39 //************************************************************************** 40 void InitialSound(void) 41 { 42 BeepIO = 0; 43 Sound_Temp_TH1 = (65535-(1/1200)*SYSTEM_OSC)/256; // 计算TL1应装入的初值 (10ms的初装值) 44 Sound_Temp_TL1 = (65535-(1/1200)*SYSTEM_OSC)%256; // 计算TH1应装入的初值 45 TH1 = Sound_Temp_TH1; 46 TL1 = Sound_Temp_TL1; 47 TMOD |= 0x11; 48 ET0 = 1; 49 ET1 = 0; 50 TR0 = 0; 51 TR1 = 0; 52 EA = 1; 53 } 54 55 void BeepTimer0(void) interrupt 1 //音符发生中断 56 { 57 static unsigned char i; 58 BeepIO = !BeepIO; 59 TH0 = Sound_Temp_TH0; 60 TL0 = Sound_Temp_TL0; 61 i++; 62 if(i==100) 63 { 64 i=0; 65 value++; 66 if(value==5)value=0; 67 } 68 69 } 70 //************************************************************************** 71 void Play(unsigned char *Sound,unsigned char Signature,unsigned Octachord,unsigned int Speed) 72 { 73 unsigned int NewFreTab[12]; //新的频率表 74 unsigned char i,j; 75 unsigned int Point,LDiv,LDiv0,LDiv1,LDiv2,LDiv4,CurrentFre,Temp_T,SoundLength; 76 unsigned char Tone,Length,SL,SH,SM,SLen,XG,FD; 77 for(i=0;i<12;i++) // 根据调号及升降八度来生成新的频率表 78 { 79 j = i + Signature; 80 if(j > 11) 81 { 82 j = j-12; 83 NewFreTab[i] = FreTab[j]*2; 84 } 85 else 86 NewFreTab[i] = FreTab[j]; 87 88 if(Octachord == 1) 89 NewFreTab[i]>>=2; 90 else if(Octachord == 3) 91 NewFreTab[i]<<=2; 92 } 93 94 SoundLength = 0; 95 while(Sound[SoundLength] != 0x00) //计算歌曲长度 96 { 97 SoundLength+=2; 98 } 99 100 Point = 0; 101 Tone = Sound[Point]; 102 Length = Sound[Point+1]; // 读出第一个音符和它时时值 103 104 LDiv0 = 12000/Speed; // 算出1分音符的长度(几个10ms) 105 LDiv4 = LDiv0/4; // 算出4分音符的长度 106 LDiv4 = LDiv4-LDiv4*SOUND_SPACE; // 普通音最长间隔标准 107 TR0 = 0; 108 TR1 = 1; 109 while(Point < SoundLength) 110 { 111 SL=Tone%10; //计算出音符 112 SM=Tone/10%10; //计算出高低音 113 SH=Tone/100; //计算出是否升半 114 CurrentFre = NewFreTab[SignTab[SL-1]+SH]; //查出对应音符的频率 115 if(SL!=0) 116 { 117 if (SM==1) CurrentFre >>= 2; //低音 118 if (SM==3) CurrentFre <<= 2; //高音 119 Temp_T = 65536-(50000/CurrentFre)*10/(12000000/SYSTEM_OSC);//计算计数器初值 120 Sound_Temp_TH0 = Temp_T/256; 121 Sound_Temp_TL0 = Temp_T%256; 122 TH0 = Sound_Temp_TH0; 123 TL0 = Sound_Temp_TL0 + 12; //加12是对中断延时的补偿 124 } 125 SLen=LengthTab[Length%10]; //算出是几分音符 126 XG=Length/10%10; //算出音符类型(0普通1连音2顿音) 127 FD=Length/100; 128 LDiv=LDiv0/SLen; //算出连音音符演奏的长度(多少个10ms) 129 if (FD==1) 130 LDiv=LDiv+LDiv/2; 131 if(XG!=1) 132 if(XG==0) //算出普通音符的演奏长度 133 if (SLen<=4) 134 LDiv1=LDiv-LDiv4; 135 else 136 LDiv1=LDiv*SOUND_SPACE; 137 else 138 LDiv1=LDiv/2; //算出顿音的演奏长度 139 else 140 LDiv1=LDiv; 141 if(SL==0) LDiv1=0; 142 LDiv2=LDiv-LDiv1; //算出不发音的长度 143 if (SL!=0) 144 { 145 TR0=1; 146 for(i=LDiv1;i>0;i--) //发规定长度的音 147 { 148 while(TF1==0); 149 TH1 = Sound_Temp_TH1; 150 TL1 = Sound_Temp_TL1; 151 TF1=0; 152 } 153 } 154 if(LDiv2!=0) 155 { 156 TR0=0; BeepIO=0; 157 for(i=LDiv2;i>0;i--) //音符间的间隔 158 { 159 while(TF1==0); 160 TH1 = Sound_Temp_TH1; 161 TL1 = Sound_Temp_TL1; 162 TF1=0; 163 } 164 } 165 Point+=2; 166 Tone=Sound[Point]; 167 Length=Sound[Point+1]; 168 } 169 BeepIO = 0; 170 } 171 //************************************************************************** 172 #endif
音乐
以上是关于51单片机_音乐的主要内容,如果未能解决你的问题,请参考以下文章