#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit key1=P3^2;//按key1可切换花样
sbit key2=P3^3;//按key2可切换歌曲
sbit key3=P3^1;//总开关,用于总体控制花样灯跟歌曲。
sbit fm=P3^7;//蜂鸣器连续到I/O口P3^7上
//---------------------六种花样灯闪烁代码----------------------------------
uchar code huayang1[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,
0xfd,0xfb,0xf7,0xef,0xdf,0xbf};//花样灯1
uchar code huayang2[]={0x7f,0xfe,0xbf,0xfd,0xdf,0xfb,0xef,0xf7,
0xef,0xfb,0xdf,0xfd,0xbf,0xfe};//花样灯2
uchar code huayang3[]={0x7f,0x3f,0x1f,0x0f,0x07,0x03,0x01,0x0,
0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff};//花样灯3
uchar code huayang4[]={0x3f,0x9f,0xcf,0xe7,0xf3,0xf9,0xfc,
0xf9,0xf3,0xe7,0xcf,0x9f,0x3f}; //花样灯4
uchar code huayang5[]={0x3f,0xfc,0x9f,0xf9,0xcf,0xf3,0xe7,
0xcf,0xf3,0x9f,0xf9,0x3f,0xfc}; //花样灯5
uchar code huayang6[]={0x55,0xaa,0xcc,0x33,0x99,0x66,0x0f,0xf0}; //花样灯6
uchar count1;//花样标志
uchar count2;//歌曲标志
uchar
timeh,timel,i;
//---------------------------音乐代码---------------------------------------
//音乐代码中,字节高位控制频率,即为音调。字节低位控制该音调持续的时间,
//代表多少个十六分音符。
//1-7代表中央C调,8-E代表高八度,0代表停顿
//最后的0是结束标志即0xff。
uchar code yldbwdx[]={ //月亮代表我的心
0x02,0x82,
0x16,0x32,0x54,0x02,0x52,
0xA6,0x32,0x54,0x02,0x52,
0x64,0x74,0xB6,0x64,
0x52,0x5C,0x32,0x22,
0x16,0x12,0x14,0x32,0x22,
0x16,0x12,0x14,0x22,0x32,
0x26,0x12,0x94,0x22,0x32,
0x2C,
0x32,0x52,
0x36,0x22,0x14,0x54,
0xAC,0x92,0xA2,
0x96,0xA2,0x96,0x82,
0x3C,0x54,
0x36,0x22,0x14,0x54,
0xAC,0x92,0xA2,
0x16,0x12,0x14,0x22,0x32,
0x2C,0x02,0x82,
0x16,0x32,0x56,0x12,
0xA6,0x32,0x56,0x52,
0x66,0x72,0xB6,0x62,
0x1C,
0xff//结束标志
};
uchar code kdqg[]={//康定情歌
0x64,0x84,0x92,0x91,0x81,0x93,0x61,0x54,0x62,
0x82,0x92,0x91,0x81,0x92,0x64,0x62,0x82,0x92,0x91,0x81,0x93,0x61,
0x54,0x82,0x62,0x51,0x61,0x51,0x41,0x52,0x24,0x92,0x54,0x82,0x64,0x52,
0x42,0x94,0x82,0x62,0x51,0x61,0x51,0x41,0x52,0x24,
0xff
};
uchar code lzh[]={//国歌
0x03,
0x04,0x04,0xc2,0x14,0x12,0x12,0x12,0x11,0x11,0xc2,0xd1,
0xe1,0x14,0x02,0x32,0x12,0x21,0x31,0x52,0x51,0x51,0x54,
0x32,0x31,0x31,0x12,0x11,0x31,0x52,0x51,0x31,0x24,0x24,
0x24,0x64,0x54,0x24,0x34,0x52,0x32,0x21,0x31,0x12,0x11,0x21,0x34,0x04,
0x52,0x51,0xd1,0x12,0x12,0x32,0x31,0x31,0x52,0x51,0x51,0x22,0x22,0x22,
0xd2,0xd1,0x24,0x22,0xc2,0x14,0x12,0x34,0x32,0x32,0x54,0x54,0x54,0x54,
0x12,0x11,0x31,0x52,0x51,0x51,0x64,0x54,0x32,0x31,0x11,0x52,0x52,0x52,
0x32,0x02,0x12,0x02,0xc4,0x14,
0xa2,0xa1,0x11,0x52,0x52,0x52,0x32,0x02,0x12,0x02,
0xc4,0x14,0xc4,0x14,0xc4,0x14,0x14,0x04,
0x04,0x04,0x04,0x04,
0xff
};
uchar code jmszl[]={ //寂寞沙洲冷
0x12,0x12,0x22,0x32,0x31,0x22,0x21,0x22,
0x21,0x31,0x51,0x52,0x31,0x52,0x61,0x15,0x14,
0x51,0x52,0x31,0x52,0x62,0x13,0x11,0x13,0x32,0x28,0x08,0x28,
0x31,0x32,0x31,0x32,0x11,0x21,0x51,0x52,0x51,0x52,
0x51,0x51,0x31,0x32,0x31,0x32,0x81,0x72,0x63,
0x62,0x71,0x81,0x72,0x61,0x61,0x52,0x31,0x21,0x32,0x51,0x54,
0x22,0x12,0x11,0x12,0x11,0x12,0x12,0x14,0x26,0x32,0x26,
0x32,0x61,0x51,0x51,0x31,0x31,0x21,0x31,0x51,0x61,0x51,0x31,0x51,
0x02,0x32,0x81,0x81,0x81,0x81,0x62,0x52,0x34,
0x31,0x81,0x81,0x81,0x61,0x91,0x82,
0x51,0x51,0x51,0x51,0x31,0x61,0x53,
0x21,0x11,0x21,0x11,0x22,0x11,0x21,0x26,
0x32,0x61,0x51,0x51,0x31,0x31,0x21,0x31,0x51,0x61,0x51,0x31,0x51,0x52,
0x31,0x31,0x81,0x81,0x81,0x61,0x91,0x81,0x61,0x31,0x56,
0x32,0x32,0x81,0x81,0x81,0x81,0x91,0x81,0x61,0x81,0x61,0x51,0x31,0x51,0x34,
0x21,0x31,0x51,0x31,0x21,0x11,0x61,0x21,0x16,
0xff};
uchar code qnzl[]={ //千年之恋
0x12,0x22,0x34,0x84,0x74,0x54,0x38,0x42,0x32,0x22,0x42,0x34,0x84,0x72,0x82,0x94,0xA8,0x08,
0x32,0x31,0x21,0x32,0x52,0x32,0x31,0x21,0x32,0x62,
0x32,0x31,0x21,0x32,0x82,0x71,0x81,0x71,0x51,0x32,0x22,
0x32,0x31,0x21,0x32,0x52,0x32,0x31,0x21,0x32,0x62,
0x32,0x31,0x21,0x32,0x83,0x82,0x71,0x72,0x02,
0x63,0xA1,0xA2,0x62,0x92,0x82,0x52,
0x31,0x51,0x63,0x51,0x63,0x51,0x63,0x51,0x62,0x82,0x7C,0x02,
0x61,0x71,0x82,0x71,0x62,0xA2,0x71,0x76,
0x61,0x71,0x82,0x71,0x62,0x52,0x31,0x36,
0x61,0x71,0x82,0x71,0x62,0xA3,0x73,0x62,0x53,
0x42,0x63,0x83,0x83,0x91,0x91,
0x61,0x71,0x82,0x71,0x62,0x0A2,0x71,0x76,
0x61,0x71,0x82,0x71,0x62,0x52,0x31,0x36,
0x61,0x71,0x82,0x71,0x62,0xA3,0x73,0x62,0x53,
0x42,0x82,0x88,0x02,0x74,0x93,0x89,
0xff};
//----------------------------不同音调对应的定时器初值---------------------------
//适合11.0592M的晶振
uchar code chuzhi[]={
0xff,0xff,//占位
0xFC,0x8E,//中央C调1-7
0xFC,0xED,
0xFD,0x43,
0xFD,0x6A,
0xFD,0xB3,
0xFD,0xF3,
0xFE,0x2D,
0xFE,0x47, //高八度1-7
0xFE,0x76,
0xFE,0xA1,
0xFE,0xC7,
0xFE,0xD9,
0xFE,0xF9,
0xFF,0x16
};
uchar yinyue[]={0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0x0,0x0};
//将音调转化为对应的LED样式
void delay1(uint z); //延时1MS
void delay(uint z); //延时165MS,即十六分音符
void song();
void beep();//蜂鸣器叫一声
//------------------------主函数---------------------------------------
main()
{ uchar x;
count1=0;//流水灯无花样
count2=1;//唱第一首歌
EA=1;//开总中断
EX0=1;//开外部中断0
IT0=1;//外部中断0下降沿触发方式
EX1=1;//开外部中断1
IT1=1;//外部中断1下降沿触发方式
TMOD=0x01;//定时器0工作在方式1
TH0=0;
TL0=0;
ET0=1;
while(1)
{
if(count1!=0)
{
switch(count1)
{
case 1:
for(x=0;x<14;x++)
{ if(key3==1)
{P1=0xff;
fm=0;
break;}
P1=huayang1[x];
beep();
delay1(300);
if(count1!=1)
break;
}
break;
case 2:
for(x=0;x<14;x++)
{ if(key3==1)
{P1=0xff;
fm=0;
break;}
P1=huayang2[x];
beep();
delay1(300);
if(count1!=2)
break;
}
break;
case 3:
for(x=0;x<16;x++)
{ if(key3==1)
{P1=0xff;
fm=0;
break;}
P1=huayang3[x];
beep();
delay1(300);
if(count1!=3)
break;
}
break;
case 4:
for(x=0;x<13;x++)
{ if(key3==1)
{P1=0xff;
fm=0;
break;}
P1=huayang4[x];
beep();
delay1(300);
if(count1!=4)
break;
}
break;
case 5:
for(x=0;x<13;x++)
{ if(key3==1)
{P1=0xff;
fm=0;
break;}
P1=huayang5[x];
beep();
delay1(300);
if(count1!=5)
break;
}
break;
case 6:
for(x=0;x<8;x++)
{ if(key3==1)
{P1=0xff;
fm=0;
break;}
P1=huayang6[x];
beep();
delay1(300);
if(count1!=6)
break;
} break;
}
}
//else
if(count1==0)
{
song();
delay1(1000);
}
}
}
void int0() interrupt 0
{
EA=0;//关总中断
delay1(1);//去抖
if(key1==0)
{
count2=0;//不让蜂鸣器唱歌
TR0=0;
count1++;
if(count1==7)
count1=1;
}
EA=1;//开总中断
}
void int1() interrupt 2
{
EA=0;//关总中断
delay1(1);//去抖
if(key2==0)
{
count1=0;//流水灯无花样
TR0=1;
i=0;//从头开始唱
count2++;
if(count2==6)
count2=1;
}
EA=1;//开总中断
}
void timer0() interrupt 1 //用于产生各种音调
{
TH0=timeh;
TL0=timel;
fm=~fm;
}
//-----------------------子函数--------------------------------------
void beep() //蜂鸣器叫一声
{ uchar i;
for(i=0;i<50;i++)
{ fm=~fm;
delay1(1);
}
fm=1;
}
void delay(uint z) //延时165MS,即十六分音符
{ uint x,y;
for(x=z;x>0;x--)
for(y=19000;y>0;y--);
}
void delay1(uint z) //延时1MS
{ uint x,y;
for(x=z;x>0;x--)
for(y=112;y>0;y--);
}
void song()
{
uint temp;
uchar jp;//jp是简谱
i=0;
while(1)
{ if(count2==0)
{
break;
}
if(key3==1)
{P1=0xff;
fm=0;
break;}
if(count2==1) //conut2==1选择月亮代表我的心
temp=yldbwdx[i];
if(count2==2) //count2==2选择寂寞沙洲冷
temp=jmszl[i];
if(count2==3) //count2==3选择千年之恋
temp=qnzl[i];
if(count2==4) //count2==4康定情歌
temp=kdqg[i];
if(count2==5) //count2==5国歌
temp=lzh[i];
if(temp==0xff) //到0xff一首歌曲结束
break;
jp=temp/16; //取数的高4位
P1=yinyue[jp];
if(jp!=0)
{
timeh=chuzhi[jp*2];
timel=chuzhi[jp*2+1];
}
else
{
TR0=0;
fm=0;//关蜂鸣器
}
delay(temp%16); //取数的低4位
TR0=0;
fm=1;
delay1(10); //唱完一个音停10MS
TR0=1;
i++;
}
TR0=0;
fm=1;
}
可以帮忙把花样灯去掉么,把音乐播放和切换作为主程序
一周热门 更多>