基于51单片机的音乐程序word文档良心出品.docx
《基于51单片机的音乐程序word文档良心出品.docx》由会员分享,可在线阅读,更多相关《基于51单片机的音乐程序word文档良心出品.docx(13页珍藏版)》请在冰豆网上搜索。
基于51单片机的音乐程序word文档良心出品
基于51单片机的按键切换播放音乐
原理图:
引脚说明:
共5个按键,分别接51单片机的P0~P4引脚,前4个按键控制播放设置好的四首音乐,第5个按键用来关闭音乐。
按键采用中断方式,任意时刻按下任意按键则立即进入所按按键的功能;蜂鸣器接单片机的P3.6口。
仿真说明:
使用proteus仿真,晶振:
12MHZ。
程序代码如下:
/*12Mhz晶振工作*/
#include
#defineuintunsignedint
#defineucharunsignedchar
sbitvoice=P3^6;
ucharcodesound1[]={0xff,
0x40,0x80,0x30,0x40,0x2b,0x40,0x26,0x80,0x24,0x10,0x26,0x40,0x30,0x40,
0x2b,0x80,0x30,0x40,0x39,0x40,0x30,0xc0,0x40,0x80,0x30,0x40,0x2b,0x40,
0x26,0x40,0x26,0x20,0x24,0x20,0x20,0x40,0x30,0x40,0x24,0x80,0x26,0x10,
0x20,0x40,0x2b,0x20,0x26,0x20,0x26,0x20,0x2b,0x20,0x2b,0xc0,0x26,0x80,
0x20,0x40,0x19,0x40,0x19,0x80,0x1c,0x10,0x1c,0x80,0x20,0x40,0x20,0x20,
0x1c,0x20,0x19,0x40,0x1c,0x20,0x20,0x20,0x26,0xc0,0x24,0x80,0x24,0x10,
0x20,0x40,0x1c,0x40,0x20,0x40,0x24,0x20,0x26,0x20,0x2b,0x80,0x33,0x40,
0x33,0x20,0x39,0x20,0x40,0x40,0x39,0x40,0x30,0xc0,0x18,0x80,0x1c,0x80,
0x24,0x80,0x20,0x10,0x1c,0x80,0x19,0x40,0x19,0x20,0x19,0x20,0x19,0x40,
0x1c,0x20,0x20,0x20,0x26,0xc0,0x18,0x80,0x1c,0x80,0x24,0x80,0x20,0x10,
0x1c,0x80,0x1c,0x40,0x1c,0x20,0x1c,0x20,0x1c,0x40,0x24,0x20,0x26,0x20,
0xff,0x20,0x00};//同一首歌*/
ucharcodesound2[]={0xff,
0x18,0x40,0x1c,0x20,0x18,0x20,0x13,0x40,0x13,0x20,0x15,0x20,0x13,0x20,
0x15,0x20,0x13,0x20,0x15,0x20,0x18,0x20,0x19,0x20,0x1c,0x20,0x20,0x20,
0x1c,0x40,0x19,0x20,0x18,0x20,0x15,0x40,0x10,0x80,
0x13,0x10,0x10,0x40,0x15,0x10,0x13,0x10,0x18,0x10,0x1c,0x10,0x26,0x10,
0x13,0x10,0x18,0x10,0x1c,0x10,0x26,0x10,0x13,0x10,0x18,0x10,0x1c,0x10,
0x26,0x10,0x13,0x10,0x18,0x10,0x1c,0x10,0x26,0x10,0x15,0x10,0x19,0x10,
0x20,0x10,0x2b,0x10,0x15,0x10,0x19,0x10,0x20,0x10,0x2b,0x10,0x15,0x10,
0x19,0x10,0x20,0x10,0x2b,0x10,0x15,0x10,0x19,0x10,0x20,0x10,0x2b,0x10,
0x18,0x10,0x1c,0x10,0x24,0x10,0x30,0x10,0x18,0x10,0x1c,0x10,0x24,0x10,
0x30,0x10,0x19,0x10,0x20,0x10,0x2b,0x10,0x19,0x10,0x19,0x10,0x20,0x10,
0x2b,0x10,0x19,0x10,0x18,0xc0,0xff,0x40,0x40,0x10,0x39,0x20,0x30,0x20,
0x2b,0x20,0x30,0x20,0x2b,0x20,0x26,0x20,0x26,0x20,0x26,0x20,0x26,0x20,
0x26,0x20,0x2b,0x20,0x30,0x20,0x2b,0x20,0x26,0x20,0x26,0x20,0x26,0x20,
0x26,0x20,0x26,0x20,0x2b,0x20,0x30,0x20,0x2b,0x40,0x30,0x10,0x30,0x20,
0x39,0x20,0x30,0x40,0x2b,0x10,0x2b,0x20,0x26,0x20,0x26,0x80,0x40,0x10,
0x39,0x20,0x30,0x20,0x2b,0x20,0x30,0x20,0x2b,0x20,0x30,0x20,0x30,0x20,
0x20,0x20,0x20,0x20,0x26,0x20,0x2b,0x20,0x26,0x20,0x2b,0x20,0x30,0x20,
0x30,0x20,0x26,0x20,0x26,0x20,0x26,0x20,0x2b,0x20,0x30,0x20,0x2b,0x40,
0x2b,0x10,0x2b,0x20,0x2b,0x20,0x2b,0x40,0x30,0x10,0x30,0x20,0x39,0x20,
0x2b,0x80,0x40,0x10,0x39,0x20,0x30,0x20,0x2b,0x20,0x30,0x20,0x2b,0x20,
0x26,0x20,0x26,0x20,0x20,0x20,0x20,0x20,0x26,0x20,0x2b,0x20,0x30,0x20,
0x2b,0x20,0x26,0x20,0x26,0x20,0x1c,0x20,0x1c,0x20,0x26,0x20,0x2b,0x20,
0x30,0x20,0x2b,0x40,0x30,0x10,0x30,0x20,0x39,0x20,0x30,0x40,0x2b,0x10,
0x2b,0x20,0x26,0x20,0x20,0x80,0x40,0x10,0x39,0x20,0x30,0x20,0x2b,0x20,
0x30,0x20,0x2b,0x20,0x30,0x20,0x30,0x20,0x1c,0x20,0x1c,0x20,0x20,0x20,
0x26,0x20,0x26,0x20,0x2b,0x20,0x30,0x20,0x30,0x20,0x26,0x20,0x26,0x20,
0x26,0x20,0x2b,0x20,0x30,0x20,0x2b,0x40,0x2b,0x10,0x2b,0x20,0x2b,0x20,
0x2b,0x40,0x30,0x10,0x30,0x20,0x39,0x20,0x30,0x80,0x30,0x10,0x18,0x20,
0x19,0x20,0x18,0x20,0x1c,0x20,0x18,0x40,0x1c,0x10,0x1c,0x20,0x20,0x20,
0x1c,0x20,0x20,0x20,0x26,0x20,0x2b,0x20,0x26,0x20,0x20,0x20,0x1c,0x20,
0x20,0x20,0x20,0x20,0x20,0x20,0x1c,0x20,0x20,0x20,0x1c,0x40,0x1c,0x10,
0x1c,0x20,0x1c,0x20,0x1c,0x40,0x20,0x10,0x20,0x20,0x1c,0x20,0x20,0x20,
0x26,0x20,0x26,0x40,0xff,0x20,0x30,0x20,0x2b,0x20,0x26,0x20,0x20,0x20,
0x26,0x20,0x2b,0x20,0x30,0x20,0x30,0x20,0x30,0x20,0x2b,0x20,0x26,0x20,
0x1c,0x20,0x26,0x20,0x2b,0x20,0x30,0x20,0x30,0x20,0x30,0x20,0x30,0x20,
0x30,0x20,0x18,0x40,0x18,0x10,0x18,0x20,0x18,0x20,0x18,0x40,0x1c,0x10,
0x1c,0x20,0x18,0x20,0x1c,0x80,0x20,0x10,0x20,0x20,0x30,0x20,0x18,0x20,
0x19,0x20,0x18,0x20,0x19,0x20,0x1c,0x20,0x20,0x20,0x1c,0x20,0x20,0x20,
0x26,0x20,0x2b,0x20,0x26,0x20,0x20,0x20,0x1c,0x20,0x20,0x20,0x20,0x20,
0x20,0x20,0x1c,0x20,0x20,0x20,0x1c,0x40,0x1c,0x10,0x1c,0x20,0x1c,0x20,
0x1c,0x40,0x20,0x10,0x20,0x20,0x1c,0x20,0x20,0x20,0x26,0x20,0x26,0x40,
0xff,0x20,0x30,0x20,0x2b,0x20,0x26,0x20,0x20,0x20,0x26,0x20,0x2b,0x20,
0x30,0x20,0x30,0x20,0x30,0x20,0x2b,0x20,0x26,0x20,0x1c,0x20,0x26,0x20,
0x2b,0x20,0x30,0x20,0x18,0x80,0xff,0x40,0xff,0x40,0xff,0x20,0x18,0x20,
0x1c,0x20,0x18,0x20,0x1c,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x1c,0x20,
0x20,0x20,0x26,0x20,0x2b,0x20,0x26,0x20,0x2b,0x20,0x2b,0x20,0x30,0x20,
0x39,0x20,0x30,0x20,0x30,0x20,0x2b,0x20,0x30,0xc0,0x30,0x80,0x30,0x20,
0x18,0x20,0x1c,0x20,0x18,0x20,0x15,0x20,0x18,0x20,0x18,0x20,0x18,0x20,
0x15,0x20,0x18,0x20,0x18,0x20,0x18,0x20,0x15,0x20,0x18,0x20,0x1c,0x20,
0x18,0x20,0x13,0x20,0x15,0x20,0x15,0x20,0x18,0x20,0x1c,0x20,0x18,0x20,
0x15,0x40,0x30,0xc0,0xff,0x40,0x00};//爱情转移*/
ucharcodesound3[]={0xff,
0x40,0x10,0x39,0x10,0x30,0x10,0x2b,0x10,0x26,0x20,0x20,0x10,0x20,0x10,
0x20,0x30,0x18,0x10,0x19,0x20,0x1c,0x20,0x20,0x20,0x20,0x40,0x1c,0x20,
0x30,0x20,0x30,0x20,0x30,0x20,0x1c,0x20,0x20,0x20,0x24,0x10,0x26,0x10,
0x26,0x10,0x2b,0x20,0x30,0x10,0x39,0x20,0x30,0x10,0x30,0x10,0x30,0x20,
0x20,0x20,0x24,0x10,0x26,0x10,0x2b,0x10,0x30,0x10,0x33,0x10,0x40,0x10,
0x39,0x20,0x39,0xc0,0xff,0x20,0x26,0x10,0x20,0x10,0x1c,0x10,0x20,0x10,
0x1c,0x10,0x20,0x10,0x1c,0x10,0x18,0x30,0x19,0x10,0x1c,0x10,0x1c,0x10,
0x20,0x10,0x1c,0x20,0x20,0x20,0x1c,0x20,0x1c,0x10,0x18,0x10,0x19,0x20,
0x1c,0x10,0x20,0x10,0x1c,0x60,0x26,0x20,0x20,0x20,0x1c,0x10,0x20,0x10,
0x1c,0x10,0x20,0x10,0x1c,0x10,0x18,0x30,0x15,0x10,0x18,0x10,0x15,0x10,
0x18,0x10,0x18,0x10,0x15,0x30,0x13,0x20,0x13,0x10,0x15,0x10,0x18,0x10,
0x15,0x20,0x13,0x10,0x13,0x60,0x15,0x20,0x13,0x20,0x15,0x10,0x18,0x10,
0x15,0x10,0x18,0x10,0x15,0x20,0x18,0x10,0x15,0x10,0x15,0x60,0x15,0x20,
0x13,0x20,0x15,0x10,0x18,0x10,0x18,0x10,0x18,0x10,0x18,0x20,0x19,0x10,
0x1c,0x10,0x1c,0x60,0xff,0x20,0x13,0x20,0x15,0x10,0x18,0x10,0x15,0x10,
0x18,0x10,0x15,0x20,0x18,0x10,0x15,0x10,0x15,0x20,0x18,0x20,0x18,0x10,
0x10,0x30,0x10,0xc0,0x18,0x20,0x15,0x20,0x13,0x10,0x15,0x10,0x13,0x10,
0x15,0x10,0x13,0x10,0x15,0x10,0x13,0x10,0x15,0x10,0x15,0x10,0x10,0x30,
0x10,0x20,0x13,0x10,0x10,0x10,0x0e,0x10,0x0e,0x10,0x0e,0x10,0x0e,0x10,
0x10,0x20,0x13,0x10,0x15,0x10,0x13,0x60,0x15,0x20,0x13,0x20,0x15,0x20,
0x18,0x10,0x1c,0x10,0x1c,0x40,0x18,0x20,0x19,0x10,0x1c,0x10,0x1c,0x40,
0xff,0x20,0x0e,0x20,0x10,0x20,0x13,0x20,0x15,0x40,0x18,0x20,0x15,0x20,
0xff,0x20,0x26,0x20,0x20,0x20,0x26,0x20,0x15,0x20,0x13,0x40,0x15,0x20,
0x18,0x20,0x1c,0xc0,0x30,0x40,0x2b,0x30,0x26,0x10,0x39,0x40,0xff,0x20,
0x30,0x10,0x39,0x10,0x33,0x10,0x30,0x10,0x2b,0x20,0x2b,0x20,0x30,0x10,
0x2b,0x10,0x26,0x10,0x2b,0x10,0x2b,0x20,0x1c,0x10,0x20,0x10,0x2b,0x10,
0x26,0x10,0x26,0x80,0x18,0x10,0x19,0x10,0x1c,0x10,0x1c,0x10,0x19,0x20,
0x26,0x20,0xff,0x20,0x26,0x20,0x20,0x20,0x26,0x20,0x15,0x20,0x13,0x40,
0x15,0x20,0x18,0x20,0x1c,0xc0,0xff,0x20,0x26,0x20,0x20,0x20,0x26,0x20,
0x15,0x20,0x13,0x40,0x15,0x20,0x18,0x20,0x1c,0x40,0x1c,0xc0,0x00
};//最后一次的温柔*/
ucharcodesound4[]={0xff,
0x40,0x30,0x30,0x80,0x30,0x30,0x30,0x80,0x26,0x40,0x2b,0x80,0x30,0x30,
0x2b,0x80,0x26,0x30,0x30,0x80,0x30,0x30,0x26,0x80,0x20,0x30,0x1c,0xc0,
0x1c,0x80,0x1c,0x30,0x20,0x80,0x26,0x30,0x26,0x80,0x30,0x30,0x2b,0x80,
0x30,0x30,0x2b,0x80,0x26,0x30,0x30,0x80,0x39,0x30,0x39,0x80,0x40,0x30,
0x30,0xc0,0x30,0x80,0x1c,0x30,0x20,0x80,0x26,0x30,0x26,0x80,0x30,0x30,
0x2b,0x80,0x30,0x30,0x2b,0x80,0x1c,0x30,0x20,0x80,0x26,0x30,0x26,0x80,
0x20,0x30,0x1c,0xc0,0x1c,0x80,0x18,0x30,0x20,0x80,0x26,0x30,0x26,0x80,
0x30,0x30,0x2b,0x80,0x30,0x30,0x2b,0x80,0x26,0x30,0x30,0x80,0x39,0x30,
0x39,0x80,0x40,0x30,0x30,0xc0,0x30,0xc0,0x00
};//友谊地久天长*/
/*ucharcodesound[]={0xff,
0x20,0x60,0x26,0x20,0x20,0x40,0x20,0x20,0x1c,0x20,0x18,0x40,0x18,0x20,
0x1c,0x20,0x20,0x80,0x20,0x60,0x1c,0x20,0x20,0x20,0x1c,0x10,0x20,0x10,
0x26,0x20,0x2b,0x20,0x20,0x30,0x1c,0x10,0x20,0x20,0x26,0x20,0x2b,0x80,
0xff,0x20,0x20,0x20,0xff,0x20,0x20,0x20,0x1c,0x40,0x20,0x40,0x18,0x30,
0x1c,0x10,0x20,0x20,0x26,0x20,0x2b,0x80,0xff,0x20,0x20,0x20,0xff,0x20,
0x20,0x20,0x26,0x40,0x2b,0x20,0x30,0x20,0x30,0xc0,0xff,0x40,0x00
};//中华民谣*/
ucharzdjs=0,jp,p,a;
uintdpjs;
voiddel(ucharyj)
{
ucharyj2=2;
while(yj!
=0)
{
while(yj2!
=0)
{
yj2--;
}
yj2=2;
yj--;
}
}
voiddelay0()
{
ucharu=20;
while(u--);
}
voidplay()
{
ucharyj;
dpjs=0;
while(a)
{
zdjs=0;
dpjs++;
if(p==1)
{
yj=sound1[dpjs];
dpjs++;
jp=sound1[dpjs];
}
if(p==2)
{
yj=sound2[dpjs];
dpjs++;
jp=sound2[dpjs];
}
if(p==3)
{
yj=sound3[dpjs];
dpjs++;
jp=sound3[dpjs];
}
if(p==4)
{
yj=sound4[dpjs];
dpjs++;
jp=sound4[dpjs];
}
while(zdjs!
=jp)
{
if(yj!
=0xff)
{
if(yj!
=0)
{
voice=!
voice;
del(yj);
}
else
{
dpjs=0;
break;
}
}
else
{
voice=0;
del(jp);
}
}
}
}
voidinit()
{
TMOD=0x01;
IE=0x82;
TH0=0xd8;
TL0=0xef;
TR0=1;
EX0=1;
IT0=1;
EA=1;
}
voidmain(void)
{
init();
a=1;
p=1;
while
(1)
{
play();
}
}
voidinit_0(void)interrupt0
{
ucharq;
q=P1;
if(q!
=0xff)
{
delay0();
q=P1;
if(q!
=0xff)
{
zdjs=0;
dpjs=0;
jp=0;
switch(q)
{
case0xfe:
a=1;p=1;break;
case0xfd:
a=1;p=2;break;
case0xfb:
a=1;p=3;break;
case0xf7:
a=1;p=4;break;
case0xef:
a=0;break;
}
}
}
}
voidtime0(void)interrupt1
{
TH0=0xd8;
TL0=0xef;
zdjs++;
}
声明:
欢迎学习与制作;转载请注明出处!