#include
#define uint unsigned int
#defineuchar unsigned char
sbit beep = P1^5;
uchar code SONG_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};
uchar code SONG_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};
//延時(shí)
void DelayMS(uint x)
{
uchar t;
while(x--) for(t=0;t<120;t++);
}
void PlayMusic()
{
uint i=0,j,k;
while(SONG_LONG[i]!=0||SONG_TONE[i]!=0)
{ //播放各個(gè)音符,SONG_LONG 為拍子長(zhǎng)度
for(j=0;j
{
beep=~beep;
//SONG_TONE 延時(shí)表決定了每個(gè)音符的頻率
for(k=0;k
}
DelayMS(10);
i++;
}
}
void main()
{
beep=0;
while(1)
{
PlayMusic(); //播放生日快樂
DelayMS(500); //播放完后暫停一段時(shí)間
}
}