freescale

来自「Freescale 系列单片机常用模块与综合系统设计」· 代码 · 共 131 行

TXT
131
字号
#include  "Variables.h"
#include  "Turning.h"
#include  "PWM_5mS.h"

void move_car(void) 
{
     switch (car_state) 
     {    
         case stop:
            if (midRd>80 && leftRd>80 && rightRd>80) // Car is not over a path
            {
                 PWM_5mS_Disable();
                 Turning_PutVal(0x00);//Stops steering motor
            } 
            else 
            {  
                   PWM_5mS_Enable();  //Enable PWM mode
                   car_state = straight;
   		    }
 		    break;
 		      
          case straight:
            if (midRd>80 && leftRd>80 && rightRd>80) 
            {
                 PWM_5mS_Disable();
                 Turning_PutVal(0x00); //Stops steering motor

                 car_state = stop;
            }
            else if (leftRd<40 && rightRd>80)
             {
                 Turning_PutBit(2, 0);  //Turn off other transistors
                 Turning_PutBit(3, 0);
   
                 Turning_PutBit(0, 1);  //Power steering motor left
                 Turning_PutBit(1, 1);

                 car_state =left_turn; 
                 turn = 1;
             }
            else if (rightRd<40 && leftRd>80) 
            {  
                 Turning_PutBit(0, 0);  //Turns off other transistors
                 Turning_PutBit(1, 0);
                 
                 Turning_PutBit(3, 1);   //Power steering motor right
                 Turning_PutBit(2, 1);
                 car_state = right_turn; 
                 turn = 1;
  	      }
          break;

          case left_turn: 
            if (turn_timer >= 1500) 
            {
                if (midRd>80 && leftRd>80 && rightRd>80)  // Car is not over a path
                {                 
                     PWM_5mS_Disable();  //Turn off all motors
                     Turning_PutVal(0x00);
                     car_state = stop;
                }    
                turn_timer = 0;
            }
 
            if (midRd>80 && leftRd<40 && rightRd>80)  //If middle sensor is not over the path 
            {
                 Turning_PutBit(0, 1);   //Power steering motor left
                 Turning_PutBit(1, 1);
                 Turning_PutBit(2, 0);
                 Turning_PutBit(3, 0);
            }
            else if (midRd<40 && leftRd>80 && rightRd>80)  //Car has readjusted itself to course - ie it has turned left
            {           
                 Turning_PutBit(2, 0);   //Left Brake
                 Turning_PutBit(1, 0);
                 Turning_PutBit(0, 1);
                 Turning_PutBit(3, 1);
                 car_state = stop;
            }
            else if (rightRd<40 && leftRd>80)  //If the car overshoots to the left
            {
                 Turning_PutBit(2, 0);  //Left Brake
                 Turning_PutBit(1, 0);
                 Turning_PutBit(0, 1);
                 Turning_PutBit(3, 1);
                 
                car_state = right_turn;
            }
            break;

          case right_turn:  
            if (turn_timer >= 1500) 
            {
      	       if (midRd>80 && leftRd>80 && rightRd>80) 
      	       {
                      PWM_5mS_Disable();   //Turn off all motors
                      Turning_PutVal(0x00);
                  
                      car_state = stop;
      	       }   
      	       turn_timer = 0;
           }
 
            if (midRd>80 && rightRd<40 && leftRd>80)  //If middle sensor is not over the path
            {
                 Turning_PutBit(3, 1);  //Power steering motor right
                 Turning_PutBit(2, 1);
                 Turning_PutBit(1, 0);
                 Turning_PutBit(0, 0);
            }
            else if (midRd<40 && leftRd>80 && rightRd>80)//Car has readjusted itself to course - ie it has turned right 
            {
                Turning_PutBit(2, 0);   //Right Brake
                Turning_PutBit(1, 0);
                Turning_PutBit(0, 1);
                Turning_PutBit(3, 1);
             
                car_state = stop;
            }
            else if (leftRd<40 && rightRd>80)   //If the car overshoots to the right
            {
                Turning_PutBit(2, 0);    //Right Brake
                Turning_PutBit(1, 0);
                Turning_PutBit(0, 1);
                Turning_PutBit(3, 1);
               
                car_state = left_turn;
            }
            break;
     }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?