單片機(jī)驅(qū)動蜂鳴器原理與設(shè)計
END
#include
unsigned char obuf1;
unsigned char obuf2;
unsigned int obuf3;
bit stopb;
bit flagb;
void main(void)
{
unsigned char i,j;
TMOD=0x02; //定時器T0初始化
TH0=0x06;
TL0=0x06;
ET0=1;
EA=1; //允許總中斷
while(1)
{
if(P3_2==0) //檢測K1按鍵
{
P1=0x00;
for(i=10;i>0;i--)
for(j=248;j>0;j--);
if(P3_2==0)
{
obuf1=0;
obuf2=0;
obuf3=0;
flagb=0;
stopb=0;
TR0=1; //啟動定時器T0,發(fā)出“叮咚”聲
while(stopb==0);
P1=0xff;
}
}
}
}
void t0(void) interrupt 1 using 0
{
obuf3++;
if(obuf3==2000)
{
obuf3=0;
if(flagb==0)
{
flagb=~flagb;
}
else
{
stopb=1;
TR0=0;
}
}
if(flagb==0)
{
obuf2++;
if(obuf2==3)
{
obuf2=0;
P3_7=~P3_7;
}
}
else
{
obuf1++;
if(obuf1==4)
{
obuf1=0;
P3_7=~P3_7;
}
}
}
評論