用51单片机控制蜂鸣器的程序,用C语言,要完整

2024-12-03 16:58:25
推荐回答(1个)
回答1:

#include
#include
#define uint unsigned int
#define uchar unsigned char
sbit buzzer=P2^0;
uchar temp[4]={4,0,2,0};
uchar n=1;
void delay250uS(void)//延时250us
{
unsigned char a,b;
for(b=19;b>0;b--)
for(a=5;a>0;a--);
}
void main(void)
{
uchar i=0,n0=0;
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
ET0=1;
EA=1;
TR0=1;
while(1)
{
n0=n;
if(n0!=0)
{
if(++i==temp[n])i=1;
if((i==1)|(i==temp[n]/2))buzzer=~buzzer;
delay250uS();
}

}
}
void Timer0Interrupt(void) interrupt 1
{
static uchar temp1=0;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
if(++temp1==10)
{
if(++n==4)n=0;
}
}