本帖最后由 673835452 于 2014-4-27 22:56 编辑
- #include <reg51.h>
- //ӽޅ֨ӥ
- sbit trig=P1^0;
- sbit echo=P3^2;
- //endb
- //Ҥ֨ӥ
- unsigned char j,time,timel,timeh;
- //ғʱ20us
- void delayus()
- {
- unsigned int us;
- for(us=0;us<200;us++);
- }
- // end
- //ғʱ
- void delay(unsigned char d)
- {
- unsigned int a,b;
- for(a=d;a>0;a--)
- for(b=110;b>0;b--);
- }
- // end
- void main ()
- {
-
- unsigned char l;
- //IT0=1;
- TMOD = 0x21;
- SCON = 0x50;
- TH1 = 0xFD;
- TL1 = 0xfd;
- TR1 = 1;
- EA=1;
- trig=0;
- echo=0;
-
- while(1)
- {
- EA=0;
- trig=1;
- delayus();
- trig=0;
- while(echo==0);
- j=0; //ԉ٦Ҫ־
- EA=1;
- EX0=1;
- TH0=0;
- TL0=0;
- TF0=0;
- TR0=1;
- delay(60);
- TR0=0;
- //EX0=0;
- if(1==j)
- {
- time=timel*256+timeh;
- l=time*0.0172;
- }
- if(0==j)
- {
- l=0;
-
- }
- SBUF=l;
- while(!TI);
- TI=0;
- }
- }
- void wai () interrupt 0
- {
-
- EX0=0;
- timel=TL0;
- timeh=TH0;
- j=1;
- }
- void ding () interrupt 1
- {
- TH0=0;
- TL0=0;
- }
-
复制代码
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>