#ifndef _robot_car_H_
#define_robot_car__H_
#include "delay.h"
#define uchar unsigned char //0~255
#define uint unsigned int //0-65536
/*
void motor_r_z(void);//右边电动机正转
void motor_l_z(void);//左边电动机正转
void motor_r_f(void);//右边电动机反转
void motor_l_f(void);//左边电动机反转
void backr(uchar,uchar); //小车后退
void go(uchar,uchar);//小车前进
void stop(void);//小车停止
*/
sbit PWM1=P1^5;
sbit PWM2=P1^4;
sbit PWM3=P1^1;
sbit PWM4=P1^2;
sbit EN1=P1^0;
sbit EN2=P1^3;
uchar data t_0;
uchar data motor_r;
uchar data motor_l;
uchar data Value;
uchar data mid;
void motor_r_z(void)//右边电动机正转
{
motor_r=0x64+Value;
EN1=1;
}
void motor_l_z(void)//左边电动机正转
{
motor_l=0x64-Value;
EN2=1;
}
void motor_r_f(void)//右边电动机反转
{
motor_r=0x64-Value;
EN1=1;
}
void motor_l_f(void)//左边电动机反转
{
motor_l=0x64+Value;
EN2=1;
}
void car_go(uchar left_motor,uchar right_motor)//直行
{
Value=right_motor;
motor_r_z();
Value=left_motor;
motor_l_z();
}
void car_back(uchar left_motor,uchar right_motor)//后退
{
Value=right_motor;
motor_r_f();
Value=left_motor;
motor_l_f();
}
void car_stop(void)//停止
{
EN2=0;
EN1=0;
}
void car_init(void)//小车初始化函数
{
////T0初始化///
TMOD=0x10; //T1工作在方式1
TH1=0xff; //装入T1初值
TL1=0xf6;
TR1=1;//开T0中断
ET1=1;//T0允许中断
EA=1;
//////////////////////
t_0=0;
/////////////////////
EN1=EN2=0;
}
void time1(void) interrupt 3 using 2
{
TR1=0;
TH1=0xff;
TL1=0xf6;
++t_0;
ACC=t_0;
CY=0;
ACC-=motor_r;
if(CY==1)
{
PWM1=1;
PWM2=0;
goto PWM_2;
}
PWM1=0;
PWM2=1;
PWM_2:
ACC=t_0;
CY=0;
ACC-=motor_l;
if(CY==1)
{
PWM3=1;
PWM4=0;
goto HIGHT;
}
PWM3=0;
PWM4=1;
HIGHT:
ACC=t_0;
if(ACC!=0xc9)
goto EXIT;
ACC=0;
t_0=ACC;
EXIT:
TR1=1;
}
#endif
#define_robot_car__H_
#include "delay.h"
#define uchar unsigned char //0~255
#define uint unsigned int //0-65536
/*
void motor_r_z(void);//右边电动机正转
void motor_l_z(void);//左边电动机正转
void motor_r_f(void);//右边电动机反转
void motor_l_f(void);//左边电动机反转
void backr(uchar,uchar); //小车后退
void go(uchar,uchar);//小车前进
void stop(void);//小车停止
*/
sbit PWM1=P1^5;
sbit PWM2=P1^4;
sbit PWM3=P1^1;
sbit PWM4=P1^2;
sbit EN1=P1^0;
sbit EN2=P1^3;
uchar data t_0;
uchar data motor_r;
uchar data motor_l;
uchar data Value;
uchar data mid;
void motor_r_z(void)//右边电动机正转
{
motor_r=0x64+Value;
EN1=1;
}
void motor_l_z(void)//左边电动机正转
{
motor_l=0x64-Value;
EN2=1;
}
void motor_r_f(void)//右边电动机反转
{
motor_r=0x64-Value;
EN1=1;
}
void motor_l_f(void)//左边电动机反转
{
motor_l=0x64+Value;
EN2=1;
}
void car_go(uchar left_motor,uchar right_motor)//直行
{
Value=right_motor;
motor_r_z();
Value=left_motor;
motor_l_z();
}
void car_back(uchar left_motor,uchar right_motor)//后退
{
Value=right_motor;
motor_r_f();
Value=left_motor;
motor_l_f();
}
void car_stop(void)//停止
{
EN2=0;
EN1=0;
}
void car_init(void)//小车初始化函数
{
////T0初始化///
TMOD=0x10; //T1工作在方式1
TH1=0xff; //装入T1初值
TL1=0xf6;
TR1=1;//开T0中断
ET1=1;//T0允许中断
EA=1;
//////////////////////
t_0=0;
/////////////////////
EN1=EN2=0;
}
void time1(void) interrupt 3 using 2
{
TR1=0;
TH1=0xff;
TL1=0xf6;
++t_0;
ACC=t_0;
CY=0;
ACC-=motor_r;
if(CY==1)
{
PWM1=1;
PWM2=0;
goto PWM_2;
}
PWM1=0;
PWM2=1;
PWM_2:
ACC=t_0;
CY=0;
ACC-=motor_l;
if(CY==1)
{
PWM3=1;
PWM4=0;
goto HIGHT;
}
PWM3=0;
PWM4=1;
HIGHT:
ACC=t_0;
if(ACC!=0xc9)
goto EXIT;
ACC=0;
t_0=ACC;
EXIT:
TR1=1;
}
#endif