
當按下開關SP1,AT89S51單片機產生“叮咚”聲從P1.0端口輸出到LM386,經過放大之后送入喇叭。
電路原理圖


C語言源程序
#include
unsigned char t5hz;
unsigned char t7hz;
unsigned int tcnt;
bit stop;
bit flag;
void main(void)
{
unsigned char i,j;
TMOD=0x02;
TH0=0x06;
TL0=0x06;
ET0=1;
EA=1;
while(1)
{
if(P3_7==0)
{
for(i=10;i>0;i--)
for(j=248;j>0;j--);
if(P3_7==0)
{
t5hz=0;
t7hz=0;
tcnt=0;
flag=0;
stop=0;
TR0=1;
while(stop==0);
}
}
}
}
void t0(void) interrupt 1 using 0
{
tcnt++;
if(tcnt==2000)
{
tcnt=0;
if(flag==0)
{
flag=~flag;
}
else
{
stop=1;
TR0=0;
}
}
if(flag==0)
{
t7hz++;
if(t7hz==3)
{
t7hz=0;
P1_0=~P1_0;
}
}
else
{
t5hz++;
if(t5hz==4)
{
t5hz=0;
P1_0=~P1_0;
}
}
}


為了方便大家更好的學習,您還可以關注暢學電子和EDA的公眾號,每天推送相關知識,希望能對你的學習有所幫助!

