音乐盒设计
#包含“Music.h”
#包含“SoundPlay.h”
#包含“lcd.h”
# Define system _ OSC 1200000//定义120000000 Hz的晶振频率。
#define SOUND_SPACE 4/5 //定义普通音符的长度分数,//每个四分音符音程。
sbit bee Pio = p2^0;//定义输出引脚
无符号字符代码段[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x 90 };//段代码0~9
无符号int码FreTab[12] = { 262,277,294,311,330,349,369,392,415,440,466,494 };//原始频率表
无符号字符代码SignTab[7] = { 0,2,4,5,7,9,11 };//1~7在频率表中的位置
无符号字符代码长度Tab[7]= { 1,2,4,8,16,32,64 };
无符号char Sound_Temp_TH0,Sound _ Temp _ TL0//临时存储音符定时器的初始值
无符号char Sound_Temp_TH1,Sound _ Temp _ TL 1;//暂时存储持续时间计时器的初始值
//*******************************************************************
#define MaxNo 3 //定义歌曲的数量。
无符号字符代码*声音;
无符号char code * code Music[MaxNo]= { Music _ 4,Music_8,Music_7,Music_6,Music_5,Music_9,Music_3,Music_2,Music _ 1 };
有符号字符FlagNo = 0;
静态无符号字符键扫描(void);
静态空哔声(void);
位run stop = 1;//1表示跑步。
sbit keyb =p3^2;//最后一首歌
sbit keyf =p3^3;//下一首歌
sbit keyt =p3^5;//暂停
extern void Delay1ms(无符号int);
void InitialSound(void)
{
bee Pio = 0;
sound _ Temp _ th 1 =(65535-(1/1200)* SYSTEM _ OSC)/256;//计算TL1的初始值(初始值为10ms)。
sound _ Temp _ TL 1 =(65535-(1/1200)* SYSTEM _ OSC)% 256;//计算TH1应该加载的初始值。
th 1 = Sound _ Temp _ th 1;
TL 1 = Sound _ Temp _ TL 1;
TMOD | = 0x 11;//两个定时器都工作在1模式。
ET0 = 1;
et 1 = 0;
TR0 = 0;
tr 1 = 0;
EA = 1;
FlagNo = 0;
声音=音乐[FlagNo];
P1 =段[FlagNo+1];
}
Void蜂鸣定时器0 (void)中断1//音符被中断。
{
BeepIO =!比皮奥;
TH0 = Sound _ Temp _ TH0
TL0 = Sound _ Temp _ TL0
}
//*******************************************************************
无效播放(无符号字符签名、无符号八和弦、无符号整数速度)
{
unsigned int NewFreTab[12];//新频率表
无符号字符I,j;
无符号int Point,LDiv,LDiv0,LDiv1,LDiv2,LDiv4,CurrentFre,Temp_T,SoundLength
无符号字符音调、长度、SL、SH、SM、SLen、XG、FD;
#ifdef使用_LCD
LCD _ init();
lcd_write_string(0,“音乐播放器”);
#endif
再次重申:
delay 1 ms(600);
for(I = 0;我& lt12;I++) //根据调号和八度生成新的频率表。
{
j = i +签名;
if(j & gt;11)
{
j = j-12;
NewFreTab[I]= FreTab[j]* 2;
}
其他
NewFreTab[I]= FreTab[j];
if(八弦== 1)
NewFreTab[I]& gt;& gt=2;
else if(八弦== 3)
NewFreTab[I]& lt;& lt=2;
}
SoundLength = 0;
while(Sound[SoundLength]!= 0x00) //计算歌曲长度
{
声音长度+= 2;
}
点= 0;
声调=声音[点];
长度=声音[点+1];//读取第一个音符及其时间值。
ldiv 0 = 12000/速度;//计算1个分数音符的长度(几个10ms)
ldiv 4 = ldiv 0/4;//计算四分音符的长度。
ldiv 4 = ldi v4-ldi v4 * SOUND _ SPACE;//普通音调的最长音程标准
TR0 = 0;
tr 1 = 1;
while(点& lt声音长度)
{
if(KeyScan())再次goto/*更多键扫描速度*/
SL = Tone % 10;//计算音符
SM = Tone/10% 10;//计算音调的高低。
SH = Tone/100;//计算是否增加一半。
current fre = NewFreTab[sign tab[SL-1]+SH];//找出对应音符的频率。
if(KeyScan())再次goto/*更多键扫描速度*/
如果(SL!=0)
{
if(SM = = 1)current for & gt;& gt= 2;//低音
if(SM = = 3)current for & lt;& lt= 2;//高音
temp _ T = 65536-(50000/current fire)* 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就是中断延迟的补偿。
}
if(KeyScan())再次goto/*更多键扫描速度*/
SLen = Length tab[Length % 10];//算算有多少音符。
XG =长度/10% 10;//计算音符类型(0普通1连奏2吨)
FD =长度/100;
LDiv = LDiv 0/SLen;//计算连音音符的播放长度(多少个10ms)。
if (FD==1)
LDiv = LDiv+LDiv/2;
如果(XG!=1)
If(XG==0) //计算普通音符的播放长度。
如果(SLen & lt=4)
LDiv 1 = LDiv-LDiv 4;
其他
LDiv 1 = LDiv * SOUND _ SPACE;
其他
LDiv 1 = LDiv/2;//计算暂停的播放长度。
其他
LDiv 1 = LDiv;
if(SL = = 0)ldiv 1 = 0;
LDiv 2 = LDiv-LDiv 1;//计算沉默的长度。
if(KeyScan())再次goto/*更多键扫描速度*/
如果(SL!=0)
{
TR0 = 1;
for(I = ldiv 1;我& gt0;I-)//读出指定的长度。
{
while(TF 1 = = 0);
th 1 = Sound _ Temp _ th 1;
TL 1 = Sound _ Temp _ TL 1;
TF 1 = 0;
if(KeyScan())再次goto/*更多键扫描速度*/
}
}
if(KeyScan())再次goto/*更多键扫描速度*/
if(LDiv2!=0)
{
TR0 = 0;bee Pio = 0;
for(I = ldiv 2;我& gt0;I-)//音符之间的音程
{
while(TF 1 = = 0);
th 1 = Sound _ Temp _ th 1;
TL 1 = Sound _ Temp _ TL 1;
TF 1 = 0;
if(KeyScan())再次goto/*更多键扫描速度*/
}
}
点+= 2;
声调=声音[点];
长度=声音[点+1];
if(KeyScan())再次goto/*更多键扫描速度*/
#ifdef使用_LCD
lcd_write_char(12,FlagNo+49);
#endif
}
bee Pio = 0;
flag no++;
if(FlagNo = = MaxNo)FlagNo = 0;
声音=音乐[FlagNo];
P1 =段[FlagNo+1];
}
/**************************************************************
功能:键盘扫描功能
加入我们:viod KeyF的下一首歌
KeyB最后一首歌
按键暂停
返回:无符号字符
1需要跳转。0不跳转。
***************************************************************/
静态无符号字符键扫描(void)
{
无符号字符控制;
If (KeyF == 0){ // Forward
delay 1 ms(5);
if (KeyF == 0){
while(KeyF == 0){
EA = 0;
flag no++;
if(FlagNo = = MaxNo)FlagNo = 0;
P1 =段[FlagNo+1];
beep();
delay 1 ms(600);//可以,长按
EA = 1;
}
if(FlagNo = = MaxNo)FlagNo = 0;
声音=音乐[FlagNo];
P1=段[FlagNo+1];//笔刷显示
while(KeyF = = 0);
返回1;
}
}
If (KeyB == 0){ //后退
delay 1 ms(5);
if (KeyB == 0){
while(KeyB == 0){
EA = 0;
FlagNo-;
if(FlagNo = =-1)FlagNo = MaxNo-1;
P1 =段[FlagNo+1];//笔刷显示
beep();
delay 1 ms(600);//可以,长按
EA = 1;
}
if(FlagNo = =-1)FlagNo = MaxNo-1;
声音=音乐[FlagNo];
P1 =段[FlagNo+1];
while(KeyB = = 0);
返回1;
}
}
If (KeyT == 0){ //暂停
delay 1 ms(20);
cont = 0;
EA = 0;
TR0 = 0;
tr 1 = 0;
if (KeyT == 0){
RunStop = ~ RunStop
while(KeyT = = 0);//等待第一次发布
beep();
如果(!RunStop){ //如果是,中断和定时器将关闭。
EA = 0;
TR0 = 0;
tr 1 = 0;
while(KeyT = = 0);//检测第二次按键并等待释放(如果有)。
While (KeyT){ //第二次释放,等待第三次按下。
P1 =段[cont++ % 10];//第三次不按,暂停几秒0~9。
delay 1 ms(100);
}//在这里暂停,直到调再次变低。
EA = 1;//重启定时器并中断
TR0 = 1;
tr 1 = 1;
声音=音乐[FlagNo];
P1 =段[FlagNo+1];//恢复原始显示
}
}
EA = 1;
TR0 = 1;
tr 1 = 1;
返回0;
}
返回0;
}
无效哔哔声(无效)
{
无符号字符I;
for(I = 0;我& lt200 ;i ++){
BeepIO = ~ BeepIO
delay 1 ms(1);
}
}
播放音乐:
# include & ltREG52。H & gt
#包含“Music.h”
#包含“SoundPlay.h”
void delay 1毫秒(无符号整数计数)
{
无符号int i,j;
for(I = 0;我& lt数数;i++)
for(j = 0;j & lt120;j++);
}
主()
{
initial sound();
while(1)
{
玩(0,3,380);
delay 1 ms(500);
}
}
音乐:
# include“Music . h”/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//挥动翅膀的女孩
无符号字符代码Music_1[]={
0x17,0x02,0x17,0x03,0x18,0x03,0x19,0x02,0x15,0x03,
0x16,0x03,0x17,0x03,0x17,0x03,0x17,0x03,0x18,0x03,
0x19,0x02,0x16,0x03,0x17,0x03,0x18,0x02,0x18,0x03,
0x17,0x03,0x15,0x02,0x18,0x03,0x17,0x03,0x18,0x02,
0x10,0x03,0x15,0x03,0x16,0x02,0x15,0x03,0x16,0x03,
0x17,0x02,0x17,0x03,0x18,0x03,0x19,0x02,0x1A,0x03,
0x1B,0x03,0x1F,0x03,0x1F,0x03,0x17,0x03,0x18,0x03,
0x19,0x02,0x16,0x03,0x17,0x03,0x18,0x03,0x17,0x03,
0x18,0x03,0x1F,0x03,0x1F,0x02,0x16,0x03,0x17,0x03,
0x18,0x03,0x17,0x03,0x18,0x03,0x20,0x03,0x20,0x02,
0x1F,0x03,0x1B,0x03,0x1F,0x66,0x20,0x03,0x21,0x03,
0x20,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x66,0x1F,0x03,
0x1B,0x03,0x19,0x03,0x19,0x03,0x15,0x03,0x1A,0x66,
0x1A,0x03,0x19,0x03,0x15,0x03,0x15,0x03,0x17,0x03,
0x16,0x66,0x17,0x04,0x18,0x04,0x18,0x03,0x19,0x03,
0x1F,0x03,0x1B,0x03,0x1F,0x66,0x20,0x03,0x21,0x03,
0x20,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x66,0x1F,0x03,
0x1B,0x03,0x19,0x03,0x19,0x03,0x15,0x03,0x1A,0x66,
0x1A,0x03,0x19,0x03,0x19,0x03,0x1F,0x03,0x1B,0x03,
0x1F,0x00,0x1A,0x03,0x1A,0x03,0x1A,0x03,0x1B,0x03,
0x1B,0x03,0x1A,0x03,0x19,0x03,0x19,0x02,0x17,0x03,
0x15,0x17,0x15,0x03,0x16,0x03,0x17,0x03,0x18,0x03,
0x17,0x04,0x18,0x0E,0x18,0x03,0x17,0x04,0x18,0x0E,
0x18,0x66,0x17,0x03,0x18,0x03,0x17,0x03,0x18,0x03,
0x20,0x03,0x20,0x02,0x1F,0x03,0x1B,0x03,0x1F,0x66,
0x20,0x03,0x21,0x03,0x20,0x03,0x1F,0x03,0x1B,0x03,
0x1F,0x66,0x1F,0x04,0x1B,0x0E,0x1B,0x03,0x19,0x03,
0x19,0x03,0x15,0x03,0x1A,0x66,0x1A,0x03,0x19,0x03,
0x15,0x03,0x15,0x03,0x17,0x03,0x16,0x66,0x17,0x04,
0x18,0x04,0x18,0x03,0x19,0x03,0x1F,0x03,0x1B,0x03,
0x1F,0x66,0x20,0x03,0x21,0x03,0x20,0x03,0x1F,0x03,
0x1B,0x03,0x1F,0x66,0x1F,0x03,0x1B,0x03,0x19,0x03,
0x19,0x03,0x15,0x03,0x1A,0x66,0x1A,0x03,0x19,0x03,
0x19,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x00,0x18,0x02,
0x18,0x03,0x1A,0x03,0x19,0x0D,0x15,0x03,0x15,0x02,
0x18,0x66,0x16,0x02,0x17,0x02,0x15,0x00,0x00,0x 00 };
//同一首歌
无符号字符代码Music_2[]={
0x0F,0x01,0x15,0x02,0x16,0x02,0x17,0x66,0x18,0x03,
0x17,0x02,0x15,0x02,0x16,0x01,0x15,0x02,0x10,0x02
0x15,0x00,0x0F,0x01,0x15,0x02,0x16,0x02,0x17,0x02,
0x17,0x03,0x18,0x03,0x19,0x02,0x15,0x02,0x18,0x66,
0x17,0x03,0x19,0x02,0x16,0x03,0x17,0x03,0x16,0x00,
0x17,0x01,0x19,0x02,0x1B,0x02,0x1B,0x70,0x1A,0x03,
0x1A,0x01,0x19,0x02,0x19,0x03,0x1A,0x03,0x1B,0x02,
0x1A,0x0D,0x19,0x03,0x17,0x00,0x18,0x66,0x18,0x03,
0x19,0x02,0x1A,0x02,0x19,0x0C,0x18,0x0D,0x17,0x03,
0x16,0x01,0x11,0x02,0x11,0x03,0x10,0x03,0x0F,0x0C,
0x10,0x02,0x15,0x00,0x1F,0x01,0x1A,0x01,0x18,0x66,
0x19,0x03,0x1A,0x01,0x1B,0x02,0x1B,0x03,0x1B,0x03,
0x1B,0x0C,0x1A,0x0D,0x19,0x03,0x17,0x00,0x1F,0x01,
0x1A,0x01,0x18,0x66,0x19,0x03,0x1A,0x01,0x10,0x02,
0x10,0x03,0x10,0x03,0x1A,0x0C,0x18,0x0D,0x17,0x03,
0x16,0x00,0x0F,0x01,0x15,0x02,0x16,0x02,0x17,0x70,
0x18,0x03,0x17,0x02,0x15,0x03,0x15,0x03,0x16,0x66,
0x16,0x03,0x16,0x02,0x16,0x03,0x15,0x03,0x10,0x02,
0x10,0x01,0x11,0x01,0x11,0x66,0x10,0x03,0x0F,0x0C,
0x1A,0x02,0x19,0x02,0x16,0x03,0x16,0x03,0x18,0x66,
0x18,0x03,0x18,0x02,0x17,0x03,0x16,0x03,0x19,0x00,
0x00,0x 00 };
//两只蝴蝶
无符号字符代码Music_3[] ={
0x17,0x03,0x16,0x03,0x17,0x 016,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,0x 016,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,0x 00 };