51单片机写音乐的一个程序,求详解,以下是程序。

51单片机写音乐的一个程序,求详解,以下是程序。,第1张

SONG_TONE[i]/3

产生一个短延时,延时的时间正好是音符的频率

延时时间越短产生的频率就越高,发出的声音也越高

SONG_LONG[i]20

是用于音符的持续时间。

就相当于长按跟短按一个按键一样

#include <REG52H>

#include "SoundPlayh"

void Delay1ms(unsigned int count)

{

unsigned int i,j;

for(i=0;i<count;i++)

for(j=0;j<120;j++);

}

//Music

//挥着翅膀的女孩

unsigned char code Music_Girl[]={ 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,0x00};

//同一首歌

unsigned char code Music_Same[]={ 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,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 };

//

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);

}

}

#include\r\n#include\r\n//本例采用89C52,晶振为110592MHZ\r\n//关于如何编制音乐代码,其实十分简单,各位可以看以下代码\r\n//频率常数即音乐术语中的音调,而节拍常数即音乐术语中的多少拍;\r\n//所以拿出谱子,试探编吧!\r\nsbitBeep=P1^5;\r\n\r\nunsignedcharn=0;//n为节拍常数变量\r\nunsignedcharcodemusic_tab[]={\r\n0x18,0x30,0x1C,0x10,//格式为:频率常数,节拍常数,频率常数,节拍常数,\r\n0x20,0x40,0x1C,0x10,\r\n0x18,0x10,0x20,0x10,\r\n0x1C,0x10,0x18,0x40,\r\n0x1C,0x20,0x20,0x20,\r\n0x1C,0x20,0x18,0x20,\r\n0x20,0x80,0xFF,0x20,\r\n0x30,0x1C,0x10,0x18,\r\n0x20,0x15,0x20,0x1C,\r\n0x20,0x20,0x20,0x26,\r\n0x40,0x20,0x20,0x2B,\r\n0x20,0x26,0x20,0x20,\r\n0x20,0x30,0x80,0xFF,\r\n0x20,0x20,0x1C,0x10,\r\n0x18,0x10,0x20,0x20,\r\n0x26,0x20,0x2B,0x20,\r\n0x30,0x20,0x2B,0x40,\r\n0x20,0x20,0x1C,0x10,\r\n0x18,0x10,0x20,0x20,\r\n0x26,0x20,0x2B,0x20,\r\n0x30,0x20,0x2B,0x40,\r\n0x20,0x30,0x1C,0x10,\r\n0x18,0x20,0x15,0x20,\r\n0x1C,0x20,0x20,0x20,\r\n0x26,0x40,0x20,0x20,\r\n0x2B,0x20,0x26,0x20,\r\n0x20,0x20,0x30,0x80,\r\n0x20,0x30,0x1C,0x10,\r\n0x20,0x10,0x1C,0x10,\r\n0x20,0x20,0x26,0x20,\r\n0x2B,0x20,0x30,0x20,\r\n0x2B,0x40,0x20,0x15,\r\n0x1F,0x05,0x20,0x10,\r\n0x1C,0x10,0x20,0x20,\r\n0x26,0x20,0x2B,0x20,\r\n0x30,0x20,0x2B,0x40,\r\n0x20,0x30,0x1C,0x10,\r\n0x18,0x20,0x15,0x20,\r\n0x1C,0x20,0x20,0x20,\r\n0x26,0x40,0x20,0x20,\r\n0x2B,0x20,0x26,0x20,\r\n0x20,0x20,0x30,0x30,\r\n0x20,0x30,0x1C,0x10,\r\n0x18,0x40,0x1C,0x20,\r\n0x20,0x20,0x26,0x40,\r\n0x13,0x60,0x18,0x20,\r\n0x15,0x40,0x13,0x40,\r\n0x18,0x80,0x00\r\n};\r\n\r\nvoidint0()interrupt1//采用中断0控制节拍\r\n{TH0=0xd8;\r\nTL0=0xef;\r\nn--;\r\n}\r\n\r\nvoiddelay(unsignedcharm)//控制频率延时\r\n{\r\nunsignedi=3m;\r\nwhile(--i);\r\n}\r\n\r\nvoiddelayms(unsignedchara)//豪秒延时子程序\r\n{\r\nwhile(--a);//采用while(--a)不要采用while(a--);各位可编译一下看看汇编结果就知道了!\r\n}\r\n\r\nvoidmain()\r\n{unsignedcharp,m;//m为频率常数变量\r\nunsignedchari=0;\r\nTMOD&=0x0f;\r\nTMOD|=0x01;\r\nTH0=0xd8;TL0=0xef;\r\nIE=0x82;\r\nplay:\r\nwhile(1)\r\n{\r\na:p=music_tab[i];\r\nif(p==0x00){i=0,delayms(1000);gotoplay;}//如果碰到结束符,延时1秒,回到开始再来一遍\r\nelseif(p==0xff){i=i+1;delayms(100),TR0=0;gotoa;}//若碰到休止符,延时100ms,继续取下一音符\r\nelse{m=music_tab[i++],n=music_tab[i++];}//取频率常数和节拍常数\r\nTR0=1;//开定时器1\r\nwhile(n!=0)Beep=~Beep,delay(m);//等待节拍完成,通过P1口输出音频(可多声道哦!)\r\nTR0=0;//关定时器1\r\n}\r\n}\r\n\r\n你参考这个吧

//《世上只有妈妈好》51单片机C语言程序和音乐采灯程序

//此程序在硬件上调试通过

//本程序的单片机晶振采用11.0592MHZ

//此程序的流水灯随音乐的节拍的快慢而动

#include <reg51h>

sbit speaker=P2^0; //接音频放大电路,

//sbit speaker=P3^2;

sbit sw=P3^0; //电后位后,sw合上后就开始放音乐,灯开始闪动,sw断开,音乐停 止, 灯也同时停止闪动

unsigned char timer0h,timer0l,time,led=1,j=0;

unsigned char flagd=0;

//世上只有妈妈好数据表

code unsigned char sszymmh[]={ 6,2,3, 5,2,1, 3,2,2, 5,2,2, 1,3,2, 6,2,1, 5,2,1,

6,2,4, 3,2,2, 5,2,1, 6,2,1, 5,2,2, 3,2,2, 1,2,1,

6,1,1, 5,2,1, 3,2,1, 2,2,4, 2,2,3, 3,2,1, 5,2,2,

5,2,1, 6,2,1, 3,2,2, 2,2,2, 1,2,4, 5,2,3, 3,2,1,

2,2,1, 1,2,1, 6,1,1, 1,2,1, 5,1,6, 0,0,0

} ;

// 音阶频率表 高八位

code unsigned char FREQH[]={

0xF2,0xF3,0xF5,0xF5,0xF6,0xF7,0xF8,

0xF9,0xF9,0xFA,0xFA,0xFB,0xFB,0xFC,0xFC, //1,2,3,4,5,6,7,8,i

0xFC,0xFD,0xFD,0xFD,0xFD,0xFE,

0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFF,

} ;

// 音阶频率表 低八位

code unsigned char FREQL[]={

0x42,0xC1,0x17,0xB6,0xD0,0xD1,0xB6,

0x21,0xE1,0x8C,0xD8,0x68,0xE9,0x5B,0x8F, //1,2,3,4,5,6,7,8,i

0xEE,0x44, 0x6B,0xB4,0xF4,0x2D,

0x47,0x77,0xA2,0xB6,0xDA,0xFA,0x16,

};

void delay(unsigned char t)

{

unsigned char t1;

unsigned long t2;

for(t1=0;t1<t;t1++)

{

for(t2=0;t2<8000;t2++)

{

;

}

}

TR0=0;

}

void t0int() interrupt 1

{

TR0=0;

speaker=!speaker;

TH0=timer0h;

TL0=timer0l;

TR0=1;

}

void song()

{

TH0=timer0h;

TL0=timer0l;

TR0=1;

delay(time);

}

void main(void)

{

unsigned char k,i;

TMOD=1; //置CT0定时工作方式1

EA=1;ET0=1;//IE=0x82 //CPU开中断,CT0开中断

while(1)

{

i=0;

time=1;

sw=1;

while(time)

{

if(sw)

{P1=0;i=0;continue;}

if(j==8)

{

//led=1;

j=0;flagd=~flagd;

if(flagd)

{

led=0x80;

}

else

{

led=1;

}

}

else

{

P1=~led;

if(flagd)

{

led=led>>1;

}

else

{

led=le d<<1;

}

j++;

}

k=sszymmh[i]+7sszymmh[i+1]-1;

timer0h=FREQH[k];

timer0l=FREQL[k];

time=sszymmh[i+2];

i=i+3;

song();

}

}

}

以上就是关于51单片机写音乐的一个程序,求详解,以下是程序。全部的内容,包括:51单片机写音乐的一个程序,求详解,以下是程序。、谁能给我几首51单片机流行歌曲 的源代码!、怎样用stc89c52单片机控制蜂鸣器发出音乐的c语言程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/9439482.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-28
下一篇 2023-04-28

发表评论

登录后才能评论

评论列表(0条)

保存