# include & lt; Reg51. H>
# define u8 unsigned char
# define under-16 unsigned int
Sbit BEEP=P2 ^ 3;
Void delay_ms (under-16 x)//millisecond delay function
{under-16 I, j;
for(i=0; ifor(j=0; j<115; J++);
}
//happy birthday note frequency table, different frequency is decided by different time delay to
U8 code SONG_TONE []={212212190212159169212212190212142159212212106126159169190119119126159142159, 0};
//happy birthday beat table, decided to each note playing length
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 u8 code SONG_LONG []={};
Void PlayMusic ()
{
Under-16 I=0, j, k;
While (SONG_LONG [I]!=0 | | SONG_TONE [I]!=0)
{//play each note, SONG_LONG to beat length
for(j=0; j{
BEEP=~ BEEP;
//SONG_TONE delay table determines the frequency of each note
for(k=0; K}
delay_ms(10);
i++;
}
}
Void main ()
{
PlayMusic (); Happy birthday//play
while(1);//play after stop
}
CodePudding user response:
By controlling the P2 ^ 3 pin high time and frequency to control the output voice, similar to the output PWM control