/*****************
小車運動
*****************/
#include<reg52.h>
#include<..\CONFIG\XIAOCHE51.h>
#define uint unsigned int
#define uchar unsigned char
void delay(uint z)
{
uint x,y;
for(x = z;x > 0;x++)
for(y = 114;y > 0;y--);
}
/*小車前進*/
void forward()
{
LEFT_MOTOR_EN;//左電機使能
RIGHT_MOTOR_EN;//右電機使能
LEFT_MOTOR_GO;// 左電機正轉
RIGHT_MOTOR_GO;//右電機正轉
}
/*小車后退*/
void backward()
{
LEFT_MOTOR_EN;
RIGHT_MOTOR_EN;
LEFT_MOTOR_BACK;
RIGHT_MOTOR_BACK;
}
/*小車左轉*/
void left()
{
LEFT_MOTOR_STOPS;
RIGHT_MOTOR_EN;
RIGHT_MOTOR_GO;
}
/*小車右轉*/
void right()
{
RIGHT_MOTOR_STOPS;
LEFT_MOTOR_EN;
LEFT_MOTOR_GO;
}
/**************
小車初始化
**************/
#ifndef _XIAOCHE51_H_
#define _XIAOCHE51_H_
/*電機驅動IO定義*/
sbit ENA = P2^2; //為1左電機使能
sbit IN1 = P2^3; //為1左電機反轉
sbit IN2 = P2^4; //為1左電機正轉
sbit IN3 = P2^5; //為1右電機使能
sbit IN4 = P2^6; //為1右電機正轉
sbit ENB = P2^7; //為1右電機反轉
#define LEFT_MOTOR_EN ENA = 1;//左電機使能
#define LEFT_MOTOR_STOPS ENA = 0;//左電機停止
#define RIGHT_MOTOR_EN ENB = 1;//右電機使能
#define RIGHT_MOTOR_STOPS ENB = 0;//右電機停止
#define LEFT_MOTOR_GO IN1 = 1,IN2 = 0;//左電機正轉
#define LEFT_MOTOR_BACK IN1 = 0,IN2 = 1;//左電機反轉
#define RIGHT_MOTOR_GO IN3 = 0,IN4 = 1;//右電機正轉
#define RIGHT_MOTOR_BACK IN3 = 1,IN4 = 0;//右電機反轉
#endif
|