輸出。
#include<stc11.h>
#define uchar unsigned char
#define uint unsigned int
sbit BEEP=P1^1;
//生日快樂歌的音符頻率表,不同頻率由不同的延時來決定
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};
//延時
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)
{ //播放各個音符,SONG_LONG為拍子長度
for(j=0;j<SONG_LONG[i]*20;j++)
{
BEEP=~BEEP;
//SONG_TONE延時表決定了每個音符的頻率
for(k=0;k<SONG_TONE[i]/3;k++);
}
DelayMS(300);
i++;
}
}
void main()
{
BEEP=0;
while(1)
{
PlayMusic(); //播放生日快樂
DelayMS(300); //播放完後暫停壹段時間
}
}