# include
# include
# define uchar unsigned char
# define uint unsigned int
Sbit TRIGGER=P0 ^ 0;
Sbit ECH0=P0 ^ 1;
Void setup ();
Void the trigger ();
Void calc ();
void delay();
Void loop ();
Void Uart1_SendChar (unsigned char Udat);
Char putchar (char c);
Void main ()
{
The setup ();
While (1)
{
Loop ();
}
}
Void setup ()
{
TMOD=0 x21;//timer 1 working baud rate in model ii, timer 0 work in mode 1, 00100001,
SCON=0 x40;//8 bits serial port, baud rate variable 01000000
TH1=0 XFD;
TCON=0 x40;
TH0=0;
TL0=0;
ET0=1;
EA=1;
}
Void the trigger ()
{
The TRIGGER=0;
The TRIGGER=1;
_nop_(); _nop_(); _nop_(); _nop_();
_nop_(); _nop_(); _nop_(); _nop_();
_nop_(); _nop_(); _nop_(); _nop_();
_nop_(); _nop_(); _nop_(); _nop_();
The TRIGGER=0;
}
Void calc ()
{
Unsigned int duration, short;
Duration=TH0 * 256 + TL0;
Duration=duration * (12/11.0592);//count the number of times a machine cycle time
//340 m/s=0.034 cm/us
Short=(duration * 0.034/2);//distance
Printf (" short: % DCM \ n ", short).
TH0=0;
TL0=0;
}
Void delay ()
{
Unsigned long int I;
For (I=10000; i> 0; I -) {}
}
Void loop ()
{
The trigger ();
While (ECH0==1);
{
TR0=1;
}
While (ECH0==0)
{
TR0=0;
}
Calc ();//calculate distance
Delay ();//delay (greater than 200 ms)
}
Void Uart1_SendChar (unsigned char Udat)
{
SBUF=Udat;//will send data into the serial data buffer
While (TI==1)//waiting to send complete
{
TI=0;//reset TI send complete tag
}
}
Char putchar (char c)
{
Uart1_SendChar (c);
return c;
}