标题: 51单片机超声波测距-SR04_12864显示 [打印本页]

作者: lyczli    时间: 2020-9-18 21:49
标题: 51单片机超声波测距-SR04_12864显示
51单片机;超声波探头用的SR04;显示用的12864;
各个子程序都写成了单独的文件;可用通用;

单片机源程序如下:
  1. #include <reg52.h>
  2. #include <delay.h>
  3. sbit trig=P1^6;
  4. sbit echo=P1^7;
  5. float const Tcy=12/11.0592;//us
  6. float const V0=349.5;//m/s or mm/ms tempreture=30
  7. unsigned int mesure_val,N;
  8. unsigned char mesure_over;
  9. void ini_mesure()
  10. {
  11.         float tmax=4000*2/V0;//最大距离4000mm;ms
  12.         N=(int)(tmax/Tcy*1000);
  13.         TMOD=0x01;
  14.         //TH0=(65536-N)/256;
  15.         //TL0=(65536-N)%256;
  16.         TH0=0;TL0=0;
  17.         ET0=1;EA=1;
  18.         trig=0;echo=1;//initialize P1
  19.         mesure_val=0;
  20. }

  21. void mesure()
  22. {
  23.         unsigned int th=0,tl=0;
  24.         float time;
  25.         trig=1;        delay(10);trig=0;//start SR04
  26.         while(echo==0);//wait echo high level
  27.         TR0=1; //start T0
  28.         while(echo==1);//echo high level end
  29.         TR0=0;
  30.         th=TH0;tl=TL0;
  31.         //TH0=(65536-N)/256;
  32.         //TL0=(65536-N)%256;//for next
  33.         TH0=0;TL0=0;
  34.         //time=((th*256+tl)-(65536-N))*Tcy/1000;//ms
  35.         time=(th*256+tl)*Tcy/1000;
  36.         mesure_val=(int)(time*V0/2);//mm
  37.                
  38. }

  39. void timer0() interrupt 1//超过最大距离处理
  40. {
  41.         //TH0=(65536-N)/256;
  42.         //TL0=(65536-N)%256;
  43.         TH0=0;TL0=0;
  44.         TR0=0;
  45.         mesure_over=1;
  46. }
复制代码

所有资料51hei提供下载:
SR04.7z (13.2 KB, 下载次数: 72)


作者: daye520    时间: 2020-9-19 09:10
有没有实际做过验证一下?
作者: lyczli    时间: 2020-9-19 14:48
是实际使用的程序




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1