51單片機;超聲波探頭用的SR04;顯示用的12864;
各個子程序都寫成了單獨的文件;可用通用;
單片機源程序如下:
- #include <reg52.h>
- #include <delay.h>
- sbit trig=P1^6;
- sbit echo=P1^7;
- float const Tcy=12/11.0592;//us
- float const V0=349.5;//m/s or mm/ms tempreture=30
- unsigned int mesure_val,N;
- unsigned char mesure_over;
- void ini_mesure()
- {
- float tmax=4000*2/V0;//最大距離4000mm;ms
- N=(int)(tmax/Tcy*1000);
- TMOD=0x01;
- //TH0=(65536-N)/256;
- //TL0=(65536-N)%256;
- TH0=0;TL0=0;
- ET0=1;EA=1;
- trig=0;echo=1;//initialize P1
- mesure_val=0;
- }
- void mesure()
- {
- unsigned int th=0,tl=0;
- float time;
- trig=1; delay(10);trig=0;//start SR04
- while(echo==0);//wait echo high level
- TR0=1; //start T0
- while(echo==1);//echo high level end
- TR0=0;
- th=TH0;tl=TL0;
- //TH0=(65536-N)/256;
- //TL0=(65536-N)%256;//for next
- TH0=0;TL0=0;
- //time=((th*256+tl)-(65536-N))*Tcy/1000;//ms
- time=(th*256+tl)*Tcy/1000;
- mesure_val=(int)(time*V0/2);//mm
-
- }
- void timer0() interrupt 1//超過最大距離處理
- {
- //TH0=(65536-N)/256;
- //TL0=(65536-N)%256;
- TH0=0;TL0=0;
- TR0=0;
- mesure_over=1;
- }
復制代碼
所有資料51hei提供下載:
SR04.7z
(13.2 KB, 下載次數: 72)
2020-9-18 21:49 上傳
點擊文件名下載附件
|