#include
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
code unsigned char gao[]={
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 di[]={
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,
};
uchar data gepu[55]; //必须要指明长度!!!!!!!!
uchar data time[55];
uchar a;//中间变量 把ram数组的值赋给它,传递给定时器中断
//第一首:生日歌
uchar code gepu1[]={5,0,5,6,5,8,7, 5,0,5,6,5,9,8, 5,0,5,12,10,8,7,6, 11,0,11,10,8,9,8,8};//30
uchar code time1[]={2,0,2,4,4,4,6, 2,0,2,4,4,4,6, 2,0,2,4,4,4,4,6, 2,0,2,4,4,4,4,4 };
//第二首:两只老虎 //大于14是指低音
uchar code gepu2[]={1,2,3,1, 1,2,3,1, 3,4,5,0, 3,4,5,0, 5,6,5,4,3,1, 5,6,5,4,3,1, 2,19,1,0, 2,19,1,0};//36
uchar code time2[]={4,4,4,4, 4,4,4,4, 4,4,4,4, 4,4,4,4, 3,1,3,1,4,4, 3,1,3,1,4,4, 4,4,4,4, 4,4,4,4};//
//第三首:多年以前
uchar code gepu3[]={1,0,1,2,3,0,3,4, 5,6,5,3, 5,4,3,2, 4,3,2,1,0, 1,0,1,2,3,0,3,4, 5,6,5,3, 5,4,3,2,3,2,1,0};//41
uchar code time3[]={4,0,2,2,4,0,2,2, 4,2,2,8, 4,2,2,8, 4,2,2,8,0, 4,0,2,2,4,0,2,2, 4,2,2,8, 4,2,2,4,2,2,10,1};
//第四首:小步舞曲
uchar code gepu4[]={5,1,2,3,4,5,1,0,1, 6,4,5,6,7,8,1,0,1, 4,5,4,3,2,3, 4,3,2,1,2, 3,2,1,21,1};//34
uchar code time4[]={4,2,2,2,2,4,4,0,4, 4,2,2,2,2,4,4,0,4, 4,2,2,2,2,4, 2,2,2,2,4, 2,2,2,2,10};
//第五首:字母歌
uchar code gepu5[]={1,0,1,5,0,5,6,0,6,5, 4,0,4,3,0,3,2,0,2,1, 5,0,5,4,3,0,3,2, 5,0,5,4,0,4,3,0,3,2, 1,3,5,6,8,5, 4,0,4,3,0,3,2,0,2,1,0};//55
uchar code time5[]={2,0,2,2,0,2,2,0,2,4, 2,0,2,2,0,2,2,0,2,4, 2,0,2,4,2,0,2,4, 2,0,2,2,0,2,2,0,2,4, 2,2,4,2,2,4, 2,0,2,2,0,2,2,0,2,4,1};
uchar i,s;
sbit led1=P3^3;
sbit led2=P3^4;
sbit led3=P3^5;
sbit led4=P3^6;
sbit led5=P3^7;
sbit buzzer=P1^0;
sbit key1=P2^0;//开始停止
sbit key2=P2^1;//上一首
sbit key3=P2^2;//下一首
void delay(uchar t);
void delayms(uchar t);
void main()
{
uchar flag=0;
uchar k=5;
uchar stop;
TMOD=0X01;
EA=1;ET0=1;
while(1)
{
while(key2==0 || key3==0 || flag==0)
{
flag=1;
if(key3==0)
{
k++;
delay(5);
if(k>5)
k=k%5;
}
if(key2==0)
{
k--;
delay(5);
if(k==0)
k=5;
}
switch(k%5)
{
case 0:
{
P3=0x7f;
s=30;
for(i=0;i {
gepu[i]=gepu1[i];
time[i]=time1[i];
}
break;
}
case 1:
{
P3=0xbf;
s=36;
for(i=0;i {
gepu[i]=gepu2[i];
time[i]=time2[i];
}
break;
}
case 2:
{
P3=0xdf;
s=41;
for(i=0;i {
gepu[i]=gepu3[i];
time[i]=time3[i];
}
break;
}
case 3:
{
P3=0xef;
s=34;
for(i=0;i {
gepu[i]=gepu4[i];
time[i]=time4[i];
}
break;
}
case 4:
{
P3=0xf0;
s=55;
for(i=0;i {
gepu[i]=gepu5[i];
time[i]=time5[i];
}
break;
}
}
}
while(key2!=0 && key3!=0)
{
for(i=0;i {
TR0=1;
a=gepu[i];
///////////////////////////
P0=(~gepu[i]);
////////////////////////////
if(time[i]>0)
delay(time[i]);
else
delayms(1);
if(key1==0)
{
delay(5);
stop++;
}
if(stop%2==0)
TR0=1;
else
TR0=0;
while(stop%2)
{
if(key1==0)
{
delay(5);
stop++;
}
}
if(key2==0 || key3==0)
{
TR0=0;
break;
}
}
}
}
}
void dingshi0() interrupt 1 using 0
{
if(a<15)
{
TH0=gao[a+6];
TL0=di[a+6];
}
else
{
TH0=gao[a-14-1];
TL0=di[a-14-1];
}
if(a==0)
buzzer=0;
else
buzzer=~buzzer;
}
void delay(uchar t)
{
uint i;
while(t--)
{
for(i = 0; i < 10001; i++);
}
}
void delayms(uchar t)
{
uchar t1; uint t2;
for(t1=0;t1
for(t2=0;t2<800;t2++)
{;}
}
}