超聲波測距原理是什么(超聲波測距原理)

導讀今天小出來為大家解答以上問題。超聲波測距原理是什么,超聲波測距原理很多人還不知道,現在讓我們一起來看看吧!1、STC89C52:2、(1)芯片的

今天小出來為大家解答以上問題。超聲波測距原理是什么,超聲波測距原理很多人還不知道,現在讓我們一起來看看吧!

1、STC89C52:

2、(1)芯片的工作條件

3、時鐘電路。

4、電源電路。

5、復位電路。

6、這個單片機的20號引腳和40號引腳是電源引腳,18號引腳和19號引腳是晶振電路,18號引腳和19號引腳通過兩個電容接地幫助晶振啟動,9號引腳連接按鈕,電容連接VCC,當按鍵按下時,9號引腳變為高電平,即單片機復位。當電源接通時,電容導通,即9腳變為高電平。充電完成后,由于引腳9的一端閉合,交流和DC隔離,電容關閉。

7、超聲波模塊:

8、這個超聲波模塊是在淘寶買的。

9、它主要有四個引腳,電源、接地、發送和接收。

10、電路圖

11、軟件部分:

12、#包含“lcd.h”

13、sbittrig=p2^1;//定義P2.1端口

14、sbitecho=p2^0;//定義P2.0端口

15、unsignedcharPuZh[]=' PechinScience ';//以指針模式顯示。

16、' unsignedcharcodeASCII[15]={'0 ',' 1 ',' 2 ',' 3 ',' 4 ',' 5 ',' 6 ',' 7 ',' 8 ',' 9 ',','-',' M ' };

17、staticunsignedcharDisNum=0;

18、unsignedinttime=0;

19、unsignedlongS=0;

20、bit flat=0;

21、unsignedcharbuff[4]={0,0,0,0,};

22、voidConut

23、{

24、時間=TH0 * 256 TL0

25、TH0=0;

26、TL0=0;

27、S=(時間* 1.7)/100;//計算出的值是厘米。

28、If((S=700)||flat==1)//超出測量范圍“-”

29、{

30、平坦=0;

31、display(0,1,ASCII[11]);

32、display(1,1,ASCII[10]);//顯示點

33、display(2,1,ASCII[11]);

34、display(3,1,ASCII[11]);

35、display(4,1,ASCII[12]);//顯示儀表

36、}

37、其他

38、{

39、buff[0]=s00/100;

40、buff[1]=s000/10;

41、buff[2]=s00;

42、display(0,1,ASCII[buff[0]]);

43、display(1,1,ASCII[10]);//顯示點

44、顯示(2,1,ASCII[buff[1]]);

45、顯示(3,1,ASCII[buff[2]]);

46、display(4,1,ASCII[12]);//顯示儀表

47、}

48、}

49、voizd1()中斷3//t0中斷用于計算和啟動模塊。

50、{

51、平坦=1;//標志位

52、}

53、VoidStartModule()//啟動模塊函數

54、{

55、trig=1;//啟動模塊。

56、_ nop _();//一微秒的延遲

57、_ nop _();//一微秒的延遲

58、_ nop _();//一微秒的延遲

59、_ nop _();//一微秒的延遲

60、_ nop _();//一微秒的延遲

61、_ nop _();//一微秒的延遲

62、_ nop _();//一微秒的延遲

63、_ nop _();//一微秒的延遲

64、_ nop _();//一微秒的延遲

65、_ nop _();//一微秒的延遲

66、_ nop _();//一微秒的延遲

67、_ nop _();//一微秒的延遲

68、_ nop _();//一微秒的延遲

69、_ nop _();//一微秒的延遲

70、_ nop _();//一微秒的延遲

71、_ nop _();//一微秒的延遲

72、_ nop _();//一微秒的延遲

73、_ nop _();//一微秒的延遲

74、_ nop _();//一微秒的延遲

75、_ nop _();//一微秒的延遲

76、_ nop _();//一微秒的延遲

77、trig=0;//啟動模塊。

78、}

79、void elayms(unsigned intms)//延遲函數

80、{

81、unsignedcharx=100,y;

82、for(;ms;女士-

83、{

84、while(--x)//while循環

85、{

86、y=10;

87、while(--y);

88、}

89、}

90、}

91、voidmain(void)//主函數

92、{

93、LED=1;//led測試

94、TMOD=0x10;//設T0為方式1,DATE=1

95、TH1=0;//高八裝入的初值為零

96、TL1=0;//低八裝入的初值為零

97、ET1=1;//允許TO中斷

98、EA=1;//開啟總中斷

99、InitLcd1602();

100、LcdShowStr(0,0,PuZh);

101、while(1)//循環

102、{

103、StartModule();

104、while(!Echo);當Echo為零時等待

105、TR0=1;//開啟計數

106、while(Echo);//設Echo為1計數并等待

107、TR0=0;//關閉計數

108、Conut();//計算距離

109、delayms(80);//延時80毫秒

110、}

111、}

112、頭文件,接口申明2

113、#ifndef__LCD_H_

114、#define__LCD_H_

115、

116、#defineLCD1602_4PINS

117、#include<reg

118、h>//包含頭文件

119、

120、#ifndefuchar

121、#defineucharunsignedchar

122、#endif

123、#ifndefuint

124、#defineuintunsignedint

125、#endif

126、

127、#defineLCD1602_DBP0//總線

128、sbitLCD1602_RS=P2^5;

129、sbitLCD1602_RW=P2^4;

130、sbitLCD1602_EN=P2^3;

131、sbitLED=P1^0;

132、//sbitLCD1602_E=P3^4;

133、//sbitLCD1602_RW=P2^5;

134、//sbitLCD1602_RS=P3^5;

135、voidLcd1602_Delay1ms(uintc);//誤差0

136、voidRead_Busy();//忙檢測函數,判斷bit7是0,允許執行,1禁止

137、voidLcd1602_Write_Cmd(unsignedcharcmd);//寫命令

138、voidLcd1602_Write_Data(unsignedchardat);//寫數據

139、voidLcdSetCursor(unsignedcharx,unsignedchary);//坐標顯示

140、voidLcdShowStr(unsignedcharx,unsignedchary,unsignedchar*str);顯示字符串

141、voidInitLcd1602();//LCD1602初始化

142、voidDisplayOneChar(unsignedcharX,unsignedcharY,unsignedcharDData);

143、#endif

144、LCD.C3

145、#include"lcd.h"

146、voidRead_Busy()//忙檢測函數,判斷bit7是0,允許執行,

147、{

148、unsignedcharsta;

149、LCD1602_DB=0xff;

150、LCD1602_RS=0;

151、LCD1602_RW=1;

152、do

153、{

154、LCD1602_EN=1;

155、sta=LCD1602_DB;

156、LCD1602_EN=0;//使能,用完拉低,釋放總線

157、}while(sta&0x80);

158、}

159、voidLcd1602_Write_Cmd(unsignedcharcmd)//寫命令

160、{

161、Read_Busy();

162、LCD1602_RS=0;

163、LCD1602_RW=0;

164、LCD1602_DB=cmd;

165、LCD1602_EN=1;

166、LCD1602_EN=0;

167、}

168、voidLcd1602_Write_Data(unsignedchardat)//寫數據

169、{

170、Read_Busy();

171、LCD1602_RS=1;

172、LCD1602_RW=0;

173、LCD1602_DB=dat;

174、LCD1602_EN=1;

175、LCD1602_EN=0;

176、}

177、//按指針位置顯示一個字符

178、voidLcdSetCursor(unsignedcharx,unsignedchary)//坐標顯示

179、{

180、unsignedcharaddr;

181、if(y==0)

182、addr=0x00+x;

183、else

184、addr=0x40+x;

185、Lcd1602_Write_Cmd(addr|0x80);

186、}

187、voidDisplayOneChar(unsignedcharX,unsignedcharY,unsignedcharDData)

188、{

189、Y&=0x1;

190、X&=0xF;//限制X不能大于15,Y不能大于1。

191、if(Y)X|=0x40;//當要顯示第二行時地址碼加,0X40。

192、X|=0x80;//算出指令碼

193、Lcd1602_Write_Cmd(X);//發命令

194、Lcd1602_Write_Data(DData);//發數據

195、}

196、voidLcdShowStr(unsignedcharx,unsignedchary,unsignedchar*str)//顯示字符串

197、{

198、LcdSetCursor(x,y);//當前字符的坐標

199、while(*str!='')

200、{

201、Lcd1602_Write_Data(*str++);

202、}

203、}

204、voidInitLcd1602()//LCD1602初始化

205、{

206、

207、Lcd1602_Write_Cmd(0x38);

208、Lcd1602_Write_Cmd(0x0c);

209、Lcd1602_Write_Cmd(0x06);

本文到此結束,希望對大家有所幫助。

免責聲明:本文由用戶上傳,如有侵權請聯系刪除!