Demo entry 6633959

Day2

   

Submitted by DIng on Aug 07, 2017 at 15:51
Language: C. Code size: 2.3 kB.

#include<reg52.h>
//-------右前轮--------
sbit IN1 = P0^0;
sbit IN2 = P0^1;
//-------左前轮--------
sbit IN3 = P0^2;
sbit IN4 = P0^3;
//-------右后轮--------
sbit IN5 = P0^4;
sbit IN6 = P0^5;
//-------左后轮--------
sbit IN7 = P0^6;
sbit IN8 = P0^7;

//------直行------
void GoStraight()
{
    //右前轮向前运动
     
     IN1 = 1;
     IN2 = 0;
    //左前轮向前
     IN3 = 0;
     IN4 = 1;
    //右后轮向前
     IN5 = 1;
     IN6 = 0;
    //左后轮向前
     IN7 = 0;
     IN8 = 1;
}

/*-----左转1-----
通过左轮不动实现
--------------*/
void TurnLeft1(){
     IN1 = 1;
     IN2 = 0;
     IN3 = 0;
     IN4 = 0;
     IN5 = 1;
     IN6 = 0;
     IN7 = 0;
     IN8 = 0;
}

/*-----左转2-----
通过右轮向前,左轮向后实现
--------------*/
void TurnLeft2(){
     IN1 = 1;
     IN2 = 0;
     IN3 = 1;
     IN4 = 0;
     IN5 = 1;
     IN6 = 0;
     IN7 = 1;
     IN8 = 0;
}

/*-----右转1-----
通过右轮不动实现
--------------*/
void TurnRight1(){
     IN1 = 0;
     IN2 = 0;
     IN3 = 0;
     IN4 = 1;
     IN5 = 0;
     IN6 = 0;
     IN7 = 0;
     IN8 = 1;
}

/*-----右转2-----
通过右轮向后,左轮向前实现
--------------*/
void TurnRight2(){
     IN1 = 0;
     IN2 = 1;
     IN3 = 0;
     IN4 = 1;
     IN5 = 0;
     IN6 = 1;
     IN7 = 0;
     IN8 = 1;
}
/*-----后退-----
将直行的位高低电平反置
--------------*/
void GoBack(){
     IN1 = 0;
     IN2 = 1;
     IN3 = 1;
     IN4 = 0;
     IN5 = 0;
     IN6 = 1;
     IN7 = 1;
     IN8 = 0;
}
/*-----停止-----
全部置0
--------------*/
void Stop (){
     IN1 = 0;
     IN2 = 0;
     IN3 = 0;
     IN4 = 0;
     IN5 = 0;
     IN6 = 0;
     IN7 = 0;
     IN8 = 0; 
}
/*-----延时-----
实现停顿效果
--------------*/
void Delayms(long xms){
     long i,j;
     for(i=xms;i>0;i--)
          for(j=150;j>0;j--);
}
/*-----主函数-----

--------------*/
void main (){
     GoStraight();
     Delayms(3200);//直行大概3.2s可以走完两个格的长度
     Stop();
     Delayms(2000);//停顿两秒
     TurnLeft2();
     Delayms(900);//转弯0.9s恰好九十度
     Stop();
     Delayms(2000);//停顿两秒
     GoStraight();
     Delayms(3200);//直行大概3.2s可以走完两个格的长度
     TurnRight2();
     Delayms(900);//转弯0.9s恰好九十度
     Stop();
     Delayms(2000);//停顿两秒
     GoStraight();
     Delayms(3200);//直行大概3.2s可以走完两个格的长度
     Stop();
     Delayms(2000);//停顿两秒
     GoBack();
     Delayms(3200);
     while(1){
          Stop();
     }
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).