《基于51單片機的音樂程序.doc》由會員分享,可在線閱讀,更多相關(guān)《基于51單片機的音樂程序.doc(10頁珍藏版)》請在裝配圖網(wǎng)上搜索。
基于51單片機的按鍵切換播放音樂
原理圖:
引腳說明:共5個按鍵,分別接51單片機的P0~P4引腳,前4個按鍵控制播放設(shè)置好的四首音樂,第5個按鍵用來關(guān)閉音樂。按鍵采用中斷方式,任意時刻按下任意按鍵則立即進入所按按鍵的功能;蜂鳴器接單片機的P3.6口。
仿真說明:使用proteus仿真,晶振:12MHZ。
程序代碼如下:
/*12Mhz晶振工作*/
#include
#define uint unsigned int
#define uchar unsigned char
sbit voice=P3^6;
uchar code sound1[]={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};//同一首歌*/
uchar code sound2[]={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}; //愛情轉(zhuǎn)移*/
uchar code sound3[]={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
};//最后一次的溫柔*/
uchar code sound4[]={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
};//友誼地久天長*/
/*uchar code sound[]={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
};//中華民謠*/
uchar zdjs=0,jp,p,a;
uint dpjs;
void del(uchar yj)
{
uchar yj2=2;
while(yj!=0)
{
while(yj2!=0)
{
yj2--;
}
yj2=2;
yj--;
}
}
void delay0()
{
uchar u=20;
while(u--);
}
void play()
{
uchar yj;
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);
}
}
}
}
void init()
{
TMOD=0x01;
IE=0x82;
TH0=0xd8;
TL0=0xef;
TR0=1;
EX0=1;
IT0=1;
EA=1;
}
void main(void)
{
init();
a=1;
p=1;
while(1)
{
play();
}
}
void init_0(void) interrupt 0
{
uchar q;
q=P1;
if(q!=0xff)
{
delay0();
q=P1;
if(q!=0xff)
{
zdjs=0;
dpjs=0;
jp=0;
switch(q)
{
case 0xfe: a=1;p=1;break;
case 0xfd: a=1;p=2;break;
case 0xfb: a=1;p=3;break;
case 0xf7: a=1;p=4;break;
case 0xef: a=0;break;
}
}
}
}
void time0(void) interrupt 1
{
TH0=0xd8;
TL0=0xef;
zdjs++;
}
聲明:歡迎學(xué)習(xí)與制作;轉(zhuǎn)載請注明出處!
鏈接地址:http://www.3dchina-expo.com/p-6639855.html