#include <reg52.h>
#include <function.h> //详见第六章第8讲
#include <timer.h> //详见第八章第11讲
#include <intrins.h>
sbit TRIG = P2^0;
sbit ECHO = P2^1;
u8 FLAG = 0;
void
delay_20us()
{
_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();
}
void
main()
{
u16 time_data,CM_data;
TRIG = 0;
ECHO = 0;
LED_Init();
EA = 1;
TIM1_Init(1000,0);
TMOD &= 0xF0;
TMOD |= 0x01;
ET0 = 1;
while
(1)
{
TH0 = 0;
TL0 = 0;
FLAG = 0;
TRIG = 1;
delay_20us();
TRIG = 0;
while
(!ECHO);
TR0 = 1;
while
(ECHO==1 && FLAG==0);
TR0 = 0;
time_data =TH0;
time_data=(time_data<<8)|TL0;
CM_data = time_data/54;
if
(FLAG==1){ShowNumber(999); delay_ms(100);}
else
{ShowNumber(CM_data); delay_ms(100);}
}
}
void
TIM0_IRQHandler() interrupt 1
{
FLAG=1;
}
void
TIM1_IRQHandler() interrupt 3
{
TH1 = T1RH;
TL1 = T1RL;
SEG_Scan();
}