| 
 
| #include<reg52.h> #include<intrins.h>
 #define uint unsigned int
 #define uchar unsigned char
 **it trig=P3^3; //触发控制信号输入
 **it echo=P3^2; //回响信号输出
 **it pwm=P3^4; //输出PWM信号
 **it P0_0=P0^0; //驱动控制
 **it P0_1=P0^1;
 **it P0_2=P0^2;
 **it P0_3=P0^3;
 uchar count,value;
 uint time=0,timer=0;
 bit flag =0;
 unsigned long s=0,zs=0,ys=0;
 
 
 
 
 
 
 /********************************************************/
 void delay(uint x) //延时函数
 { uint i,j;
 for(i=x;i>0;i--)
 for(j=110;j>0;j--);
 }
 /********************************************************/
 
 void stop() //停止
 {
 P0_0=0;
 P0_1=0;
 P0_2=0;
 P0_3=0;
 }
 
 
 void forward() //前进
 {
 P0_0=1;
 P0_1=0;
 P0_2=1;
 P0_3=0;
 }
 
 
 
 
 
 
 void backword() //后退
 {
 P0_0=0;
 P0_1=1;
 P0_2=0;
 P0_3=1;
 delay(100);
 stop();
 }
 
 
 
 
 void turn_left() //左转
 {
 P0_0=0;
 P0_1=0;
 P0_2=1;
 P0_3=0;
 delay(100);
 stop();
 }
 
 
 
 
 
 
 void turn_right() //右转
 {
 P0_0=1;
 P0_1=0;
 P0_2=0;
 P0_3=0;
 delay(100);
 stop();
 
 }
 
 
 
 /********************************************************/
 void ceju(void) //测距函数
 {
 while(!echo); //当echo为零时等待
 TR0=1; //开启计数
 while(echo); //当echo为1计数并等待
 TR0=0; //关闭计数
 time=TH0*256+TL0;
 TH0=0;
 TL0=0;
 s=(time*1.7)/100; //算出来是CM }
 
 
 }
 /********************************************************/
 
 
 
 
 void clean() //清零
 {
 timer=0;
 TH1=65036/256;
 TL1=65036%256;
 count=0;
 }
 
 
 
 
 
 
 
 /********************************************************/
 void zd0() interrupt 1 //T0中断用来计数器溢出,超过测距范围
 {
 flag=1; //中断溢出标志
 }
 /********************************************************/
 
 
 
 
 void zd1() interrupt 3
 {
 TH1=65036/256;
 TL1=65036%256;
 if(count<value)
 pwm=1;
 else
 pwm=0;
 count++;
 count=count%40;
 timer++;
 if(timer>=800)
 {
 timer=0;
 trig=1;
 _nop_();
 _nop_();
 _nop_();
 _nop_();
 _nop_();
 _nop_();
 _nop_();
 _nop_();
 _nop_();
 _nop_();
 _nop_();
 _nop_();
 _nop_();
 _nop_();
 _nop_();
 _nop_();
 _nop_();
 _nop_();
 trig=0;
 }
 }
 
 
 
 /*********************************************************/
 void main( void )
 { int i,j;
 value=1; //初始化计数器
 count=0;
 TMOD=0x11; //设T0,T1为方式1,GATE=0;
 TH0=0;
 TL0=0;
 TH1=30000/256; //计数器1定时0.5ms
 TL1=30000%256;
 IE=0x8a;//总中断开,允许计数器0,1中断开
 
 
 
 
 
 
 
 
 while(1)
 {
 TR1=1;
 ceju();
 forward();
 if(s<=10)
 {
 stop(); //停止
 for(i=6;i>0;i--)
 for(j=30;j>0;j--)
 {
 stop();
 
 }
 
 
 }
 }
 }
 
 
 
 
 
 
 
 
 
 
 
 
 程序可以运行,, 但是速度太快了 怎么调解pwm那
 65036这个数字又是什么意思
 有大佬解答一下嘛?
 
 
 
 
 
 
 萌新求助那
 
 
 | 
 |