Home > other >  Ultrasonic ranging serial print how to write, serial port to print this is not very good, only 64 30
Ultrasonic ranging serial print how to write, serial port to print this is not very good, only 64 30

Time:12-31


#include
#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;

}
  • Related