#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit BEEP=P3^7;
uint n,nn;
uint j;
void delay500(void)
{
uchar i;
for(i=230;i>0;i--);
}
void beep_beep()
{
for(j=200;j>0;j--)
{
BEEP=~BEEP; //输出频率1KHz
delay500(); //延时500us
}
for(j=200;j>0;j--)
{
BEEP=~BEEP; //输出频率500Hz
delay500(); //延时1ms
delay500();
}
}
main()
{
TMOD=0x01;
TH0=0xfc; TL0=0x66; //1ms定时
EA=1; ET0=1;
TR0=1;
while(1)
{
beep_beep();
}
}
void Time0(void) interrupt 1 using 0
{
TH0=0xfc; //1ms定时
TL0=0x66;
n++;
if(n==100)
{
n=0;
nn++;
}
if(nn==5)
{
nn=0;
BEEP=1; //关闭蜂鸣器
}
} |