#includereg52.h#defineucharunsignedchar#defineuintunsignedintsbitbeep=P1^5;ucharcodeSONG_TONE[]={212,212,190,212,159,169,212,212,190,212,142,159,212,212,106,126,159,169,190,119,119,126,159,142,159,0};//生日快乐歌的音符频率表,不同频率由不同的延时来决定ucharcodeSONG_LONG[]={9,3,12,12,12,24,9,3,12,12,12,24,9,3,12,12,12,12,12,9,3,12,12,12,24,0};//生日快乐歌节拍表,节拍决定每个音符演奏长短voiddelay(uintx){uchart;while(x--)for(t=0;t120;t++);}voidplaymusic(){uinti=0,j,k;while(SONG_TONE[i]!=0||SONG_LONG[i]!=0){for(j=0;jSONG_LONG[i]*20;j++)//播放个个音符,SONG_LONG为音拍个个长度{beep=~beep;for(k=0;kSONG_TONE[i]/3;k++);//SONG_TONE延时决定了每个音符的频率}delay(10);i++;}}voidmain(){beep=0;while(1){playmusic();delay(500);}}