#include < stdio.h >
#include < reg51.h >
#include < Delay.h >
sbit INTO = P3 ^ 2;
sbit TRG = P3 ^ 3;
sbit Buzzer = P3 ^ 7;
void Beep(unsigned char No, unsigned int Delay);
void tmrInit(void);
void tmrReset(void);
unsigned int L1 = 100, L2 = 20;
unsigned int T0_ISR_count = 0;
unsigned int US = 0;
unsigned int CM = 0;
void T0_ISR(void) interrupt 1 {
T0_ISR_count++;
TF0 = 0;
}
void main(void) {
Beep(3, 20000);
TRG = 0;
tmrInit();
while (1) {
tmrReset();
TRG = 1;
DelayI(1000);
TRG = 0;
while (INT0 == 0)
TR0 = 1;
while (INT0 == 1)
TR0 = 0;
US = (unsigned int)((TH0 << 8) | TL0 | ((unsigned int) T0_ISR_count << 16));
CM = US / 58;
if ((CM <= L1) && (CM >= L2)) {
Beep(1, 50 * CM);
}
if (CM <= L2) {
Buzzer = 0;
} else {
Buzzer = 1;
}
DelayL(2000);
}
void tmrReset(void)
T0_ISR_count = 0;
TH0 = 0;
TL0 = 0;
TR0 = 0;
void tmrInit(void)
TMOD = (TMOD & 0xF0) | 0x09;
void Beep(unsigned char No, unsigned int Delay)
unsigned char tNo;
for (tNo = 1; tNo <= No; tNo++) {
Buzzer = 0;
DelayI(Delay);
Buzzer = 1;
DelayI(Delay);
}
}