-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path超声波测距.c
137 lines (124 loc) · 1.7 KB
/
超声波测距.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include<reg52.h>
#include<1602.h>
#define VELOCITY_30C 349.5 //30摄氏度时的声速,声速V= 331.5 + 0.6*温度;
#define VELOCITY_23C 345.3 //23摄氏度时的声速,声速V= 331.5 + 0.6*温度;
uchar code table[]=" Ce Ju";
uchar code table1[]="distance:";
uint distance,count;
sbit input=P1^6;
sbit output=P1^7;
sbit beep=P2^3;
sbit jinbao=P3^2;
void timeinti()
{
TMOD=0x01;
TH0=0Xfc;
TL0=0x6a;
EA=1;
EX0=1;
}
void write_string(uchar *p,uchar adress)
{
uchar i;
write_com(0x80+adress);
for(i=0;p[i]!=0;i++)
{
write_dat(p[i]);
}
}
void delayt(uint x)
{
uchar j;
while(x-- > 0)
{
for(j = 0;j < 125;j++)
{
;
}
}
}
void sheng_bo()
{
output=1;
delayt(1);
output=0;
}
void inti_MCU()
{
input=0;
output=0;
distance=0;
count=0;
}
void write_shu()
{
uchar a,b,c,d;
a=distance/1000;
b=distance%1000/100;
c=distance%100/10;
d=distance%10;
write_string(table1,0x40);
write_dat(0x30+a);
write_dat(0x30+b);
write_dat(0x30+c);
write_dat('.');
write_dat(0x30+d);
write_dat('c');
write_dat('m');
}
void measure()
{
uchar x,y;
float t;
TR0=1;
while(input){}
TR0=0;
x=TH0;
y=TL0;
t=count*1000+((x-0xfc)*256+(y-0x6a))*1.085;
distance=VELOCITY_30C*t/2000;
if(distance>4000)
{
distance=0;
}
}
void main()
{
inti();
inti_MCU();
timeinti();
write_string(table,0x00);
while(1)
{
sheng_bo();
while(!input){}
measure();
write_shu();
if(distance>1000)
{
jinbao=0;
}
TH0=0xfc;
TL0=0x6a;
inti_MCU();
delayt(130);
}
}
void timer0() interrupt 1
{
count++;
TH0=0xfc;
TL0=0x6a;
if(count==18)
{
TR0=0;
count=0;
}
}
void baojin() interrupt 0
{
beep=0;
delayt(100);
beep=1;
jinbao=1;
}