#include
#define uchar unsigned char
#define uint unsigned int
sbit k3=P3^5;
sbit sound=P0^4;
bit FY=0; //放乐曲时FY=1, 电子琴演奏时FY=0
uchar Song_Index=0,Tone_Index=0; //放音乐的参数
uchar k, key,flag;
uchar code DSY_CODE[]= {0xc0,0xf9,0xa4,0xb0,
{13,12,10,12,15,13,12,13,10,12,13,12,10,8,1,12,10,9,9,10,12,12,13,10,9,8,12,10,9,8,6,8,5,-1};
char code Len[]= //旋律每个音符对应
{3,1,2,2,2,1,1,4,2,1,1,2,2,1,1,1,1,4,3,1,2,1,1,2,2,4,3,1,1,1,1,1,6,-1};
uint code tab[]= {0,63628,63835,64021,64103,64260,64400,64524, //0,低1,低2,低3,低4,低5,低6,
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x–)
for(y=114;y>0;y–);
}
void main()
{
TMOD=0x01;//设置定时器0工作方式1
TH0=(tab[0])/256;
TL0=(tab[0])%6;//赋初值
EA=1;//开总中止
ET0=1;//开定时器中止0
P2=0X00;
while(1)
{
Tone_Index=0;
P0=DSY_CODE;
TH0=(tab[Song[Tone_Index]])/256;
TR0 = 1;
delay(250*Len[Tone_Index]);
Tone_Index ;
TR0=0;
}
}
}
void timer0() interrupt 1
{
TH0=(tab[Song[Tone_Index]])/256;
TL0=(tab[Song[Tone_Index]])%6;
sound=!sound;
}