#include<reg52.h>
sbit Beep = P1^4;
unsigned char n=0;
// || 中 音 || || 低 音 || || 高 音 ||
// 0 1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7 1
unsigned char code music_tab0[]={ 1 ,153,136,121,114,102, 91, 81,0,0,0, 0 , 0 ,242,228,204,182,162,0,0,0, 76, 68, 61, 57, 51, 46, 41,0,0,0, 38}; //音调延时表
unsigned char code music_tab1[]={15,15,16,15, 1,17,15,15,16,15, 2, 1,15,15, 5, 3, 1,17, 6, 4, 4, 3, 1, 2, 1, 0};
unsigned char code music_tab2[]={ 2, 2, 4, 4, 4, 8, 2, 2, 4, 4, 4, 8, 2, 2, 4, 4, 4, 4, 4, 2, 2, 4, 4, 4, 8, 0}; //生日快乐
void delay ( unsigned char m )
{
unsigned char i;
for ( i=m ; i>0 ; i--);
}
void main()
{
unsigned char m;
unsigned char i;
TMOD=0x01;
TH0=0xd8;//11011000
TL0=0xef; //11101111
EA=1;
ET0=1;
TR0=1;
i=0;
while(1)
{
m = music_tab0[music_tab1[i]];
n = 15 * music_tab2[i];
i++;
if ( 1==m ) break;
if ( m )
{
while(n!=0)
{
Beep=~Beep;
delay(m);
}
}
else
{
while(n!=0)
{
delay(m);
}
}
delay (200);
}
for ( i=0 ; i<255 ; i++ )
{
delay (200);
}
}
void timer0() interrupt 1
{
TH0=0xd8;
TL0=0xef;
n--;
}