|
- #include<reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit motor0=P1^0;//电机定义P1^0到P1^3正转,P1^4到P1^5反转;
- sbit motor1=P1^1;
- sbit motor2=P1^2;
- sbit motor3=P1^3;
- sbit motor4=P1^4;
- sbit motor5=P1^5;
- sbit motor6=P1^6;
- sbit motor7=P1^7;
- sbit pwm=P2^7; //舵机PWM信号输出
- sbit trig=P3^1;//超声波输入口
- sbit echo=P3^2;//超声波输出口
- uchar flag,i,count,jd;
- uchar b,low,k,high,d,e,f,h;//PWM调速高低电平定义
- uint time,s;
- /////////
- void delay(uint z)
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=100;y>0;y--);
- }
- ///////
- void delay1(uchar k)//延时
- {
- uchar i,j;
- for(i=k;i>0;i--)
- for(j=110;j>0;j--);
- }
- ////////
- void delay20us()
- {
- uint a;
- for(a=0;a<100;a++);
- }
- /////////////
- void init()
- {
- TMOD=0x11;
- TH0=0;
- TL0=0;
- TH1=(65536-100)/256;//11.0592MHZ晶振,0.5ms
- TL1=(65536-100)%256;
- TR1=1; //定时起开始
- EA=1;
- //ET0=1;
- //ET1=1;
- }
- void estern() interrupt 0
- {
- flag=1;
- }
- void calculate()//超声波转换
- {
- time=TH0*256+TL0;
- TH0=0;
- TL0=0;
- s=time/58;
- //display();
- }
- ////////
- void qudong()//前后电机PWM调速处理
- {
- uchar i;
- if(low!=0)
- {
- for(i=0;i<low;i++)
- {
- if(b==0)
- {
- motor0=0;
- motor1=0;
- motor2=0;
- motor3=0;
- delay1(5);
- }
- }
- }
- for(i=0;i<high;i++)
- {
- if(b==0)
- {
- motor0=1;
- motor1=1;
- motor2=1;
- motor3=1;
- delay1(5);
- }
- }
- }
- void ultrasonic()//超声波处理
- {
- trig=1;
- delay20us();
- trig=0;
- while(!echo);
- TR0=1;
- while(echo);
- TR0=0;
- calculate();
- delay(20);
- //display();
- }
- void bb()//距离判断
- {
- uchar x=5;
- if((s<10)&&(jd==12))
- { motor0=0;
- motor1=0;
- motor2=0;
- motor3=0;
- motor4=0;
- motor5=0;
- motor6=0;
- motor7=0;
-
- ET1=0;
- ultrasonic();
- while(x--)
- {
- d=s;
- delay(2);
- }
-
- ET1=1;
- jd=5;
- delay(500);
- ET0=0;
- }
- else
- {
- ET0=1;
- }
- if(jd==5)
- { ET1=0;
- ultrasonic();
- while(x--)
- {
- e=s;
- delay(2);
- }
- ET1=1;
- jd=19;
- delay(500);
- ET0=0;
- }
- else
- ET0=1;
-
- if(jd==19)
- { ET1=0;
- ultrasonic();
- while(x--)
- {
-
- f=s;
- delay(2);
- }
-
- ET1=1;
- jd=12;
-
- delay(500);
- ET0=0;
- }
- else
- ET0=1;
-
- }
- void main()
- {
- uchar m=4;
- s=20;
- init();
- echo=0;
- jd=12;
- count=0;
- low=20;high=5;//设置速度
- while(1)
- {
- if(e<f)
- {
- while(m--)
- {
- b=1;
- motor6=0;
- motor7=0;
- motor0=0;
- motor1=0;
- motor4=1;
- motor5=1;
- motor2=1;
- motor3=1;
- delay(1);
- }
- e=0;
- f=0;
- }
- else
- {
- b=0;
- motor4=0;
- motor5=0;
- motor6=0;
- motor7=0;
- }
- if(e>f)
- {
- while(m--)
- {
- b=1;
- motor0=1;
- motor1=1;
- motor6=1;
- motor7=1;
- motor2=0;
- motor3=0;
- motor4=0;
- motor5=0;
- delay(1);
- }
- e=0;
- f=0;
- }
- else
- {
- b=0;
- motor4=0;
- motor5=0;
- motor6=0;
- motor7=0;
- }
- bb();
- //turn();
- //kk();
- qudong();
- ultrasonic();
- h=s;
- }
- }
- //////////
- void Time1_Int() interrupt 3 //舵机中断程序
- {
- TH1=(65536-100)/256;//11.0592MHZ晶振,0.5ms
- TL1=(65536-100)%256;
- if(count<jd)//判断0.5ms次数是否小于角度标识
- pwm=1;//确实小于PWM输出高电平
- else
- pwm=0; //大于则输出低电平
- count=(count+1);//0.5ms次数加1
- count=count%40;// 次数使终保持40周期为20ms
- }
复制代码
|
|