源代码
#include
***it PWM1=P0^0;
***it PWM2=P0^1;
***it IN1=P0^2;
***it IN2=P0^3;
***it IN3=P0^4;
***it IN4=P0^5;
***it RP1=P2^0;//左边寻迹
***it RP3=P2^1; //右边
int count1,count2;//用于中断函数计数
void delay(int z)
{
int x,y;
for(x=z;x>0;x--)
for(y=124;y>0;y--);
}
void forward1()//电机1前进
{
IN1=0;
IN2=1;
}
void forward2()
{
IN3=0;
IN4=1;
}
void back1()//电机1后退
{
IN1=1;
IN2=0;
}
void back2()
{
IN3=1;
IN4=0;
}
void speed(int cnt1,int sd1,int cnt2,int sd2)
{
if(cnt1
PWM1=1;
else
PWM1=0;
if(cnt2
PWM2=1;
else
PWM2=0;
}
void turn(int cnt1,int sd1,int cnt2,int sd2)
{
forward1();
forward2();
speed(cnt1,sd1,cnt2,sd2);
}
void back(int cnt1,int sd1,int cnt2,int sd2)
{
back1();
back2();
speed(cnt1,sd1,cnt2,sd2);
}
void main()
{
TMOD=0X01;
EA=1;
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
ET0=1;
TR0=1;
turn(count1,30,count2,35); //2 有 1左
delay(350);
while(1)
{
if(RP1==0&&RP3==1)
turn(count1,60,count2,0);
if(RP1==1&&RP3==1)
back(count1,20,count2,25);
if(RP1==1&&RP3==0)
turn(count1,0,count2,10);
if(RP1==0&&RP3==0)
turn(count1,30,count2,35);
}
}
void time()interrupt 1
{
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
count1++;
count2++;
if(count1>=100)
count1=0;
if(count2>=100)
count2=0;
}
0
|
|
|
|