超声波测距 + 串口发送距离
#include<reg51.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit trig=P1^7;
sbit echo=P3^2;
float tA,dis;
uint distance,timeh,timel;
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=1100;y>0;y--);
}
void delay20us()
{
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
}
void initT1_SCI()
{
SM0 = 0; //
SM1 = 1; //10位异步收发,波特率可变
TMOD = 0x21;
TH1 = 0xfd;
TL1 = 0xfd;
TR1 = 1;
}
void Trig()
{
trig = 0;
echo = 0;
TH0 = 0;
TL0 = 0;
trig = 1;
delay20us();
trig = 0;
while(echo =