1.我的小车在遇到左边的黑线时停不下来,慢慢开是能停的。。是我的程序太臃肿了嘛
2.我想在我的小车上实现,在条件达成之后在冲一段时间,这在程序中怎么实现
附程序,希望大佬们帮帮我这个菜鸟,第一次做小车
#include <reg52.h>
#define uint unsigned int
unsigned char zifuchuan[]="abcd"; //待显示字符
volatile unsigned char sending;
int add_left1,add_left2,add_right1,add_right2;
int flag=0;
int flag2=0;
int r,l,z=0;
/*******PWM波调节参数********/
#define push_left1 1
#define push_left2 1
#define push_right1 1
#define push_right2 1
/*******黑白检测传感器输出********/
sbit sign_left1=P2^0;
sbit sign_left2=P2^1;
sbit sign_left3=P2^2;
//sbit sign_right1=P2^2;
sbit sign_left4=P2^3;
sbit sign_right1=P2^4;
sbit sign_right2=P2^5;
sbit sign_right3=P2^6;
sbit sign_back2=P2^7;
sbit sign_back=P3^1;
/*******四轮电机信号输入********/
sbit left_moto1_in1=P0^0;
sbit left_moto1_in2=P0^1;
sbit left_moto2_in1=P0^2;
sbit left_moto2_in2=P0^3;
sbit right_moto1_in1=P0^4;
sbit right_moto1_in2=P0^5;
sbit right_moto2_in1=P0^6;
sbit right_moto2_in2=P0^7;
/*******四轮电机信号使能********/
sbit Left_moto1_pwm_ENA=P1^0;
sbit Left_moto2_pwm_ENB=P1^1;
sbit Right_moto1_pwm_ENA=P1^2;
sbit Right_moto2_pwm_ENB=P1^3;
/*******电机占空比参量定义********/
unsigned char pwm_val_left1 =0; //变量定义
unsigned char push_val_left1 =0; //左1电机占空比N/10
unsigned char pwm_val_left2 =0; //变量定义
unsigned char push_val_left2 =0; //左2电机占空比N/10
unsigned char pwm_val_right1 =0; //变量定义
unsigned char push_val_right1=0; //右电机占空比N/10
unsigned char pwm_val_right2 =0; //变量定义
unsigned char push_val_right2=0; //右电机占空比N/10
bit Left_moto1_stop =1;
bit Left_moto2_stop =1;
bit Right_moto1_stop=1;
bit Right_moto2_stop=1;
unsigned int time=0;
/*******左1电机前进********/
void left_moto1_go()
{
left_moto1_in1=1;
left_moto1_in2=0;
}
/*******左1电机后退********/
void left_moto1_back()
{
left_moto1_in1=0;
left_moto1_in2=1;
}
/*******左1电机停止********/
void left_moto1_stp()
{
left_moto1_in1=0;
left_moto1_in2=0;
}
/*******左2电机前进********/
void left_moto2_go()
{
left_moto2_in1=1;
left_moto2_in2=0;
}
/*******左2电机后退********/
void left_moto2_back()
{
left_moto2_in1=0;
left_moto2_in2=1;
}
/*******左2电机停止********/
void left_moto2_stp()
{
left_moto2_in1=0;
left_moto2_in2=0;
}
/*******右1电机前进********/
void right_moto1_go()
{
right_moto1_in1=1;
right_moto1_in2=0;
}
/*******右1电机后退********/
void right_moto1_back()
{
right_moto1_in1=0;
right_moto1_in2=1;
}
/*******右1电机停止********/
void right_moto1_stp()
{
right_moto1_in1=0;
right_moto1_in2=0;
}
/*******右2电机前进********/
void right_moto2_go()
{
right_moto2_in1=1;
right_moto2_in2=0;
}
/*******右2电机后退********/
void right_moto2_back()
{
right_moto2_in1=0;
right_moto2_in2=1;
}
/*******右2电机停止********/
void right_moto2_stp()
{
right_moto2_in1=0;
right_moto2_in2=0;
}
/*******小车停止********/
void car_stp()
{
push_val_left1 = push_left1+add_left1; //PWM 调节参数1-10 1为最慢,10是最快 改这个值可以改变其速度
push_val_left2 = push_left2+add_left2;
push_val_right1 = push_right1+add_right1; //PWM 调节参数1-10 1为最慢,10是最快 改这个值可以改变其速度
push_val_right2 = push_right2+add_right2;
left_moto1_stp();
left_moto2_stp();
right_moto1_stp();
right_moto2_stp();
}
/*******小车直线前进********/
void car_run_straight()
{
push_val_left1 = push_left1+add_left1; //PWM 调节参数1-10 1为最慢,10是最快 改这个值可以改变其速度
push_val_left2 = push_left2+add_left2;
push_val_right1 = push_right1+add_right1; //PWM 调节参数1-10 1为最慢,10是最快 改这个值可以改变其速度
push_val_right2 = push_right2+add_right2;
left_moto1_go();
left_moto2_go();
right_moto1_go();
right_moto2_go();
}
/*********小车右转**********/
void car_turn_left()
{
push_val_left1 = push_left1+add_left1; //PWM 调节参数1-10 1为最慢,10是最快 改这个值可以改变其速度
push_val_left2 = push_left2+add_left2;
push_val_right1 = push_right1+add_right1; //PWM 调节参数1-10 1为最慢,10是最快 改这个值可以改变其速度
push_val_right2 = push_right2+add_right2;
right_moto1_go();
right_moto2_go();
left_moto1_stp();
left_moto2_stp();
}
/*********小车左转**********/
void car_turn_right()
{
push_val_left1 = push_left1+add_left1; //PWM 调节参数1-10 1为最慢,10是最快 改这个值可以改变其速度
push_val_left2 = push_left2+add_left2;
push_val_right1 = push_right1+add_right1; //PWM 调节参数1-10 1为最慢,10是最快 改这个值可以改变其速度
push_val_right2 = push_right2+add_right2;
left_moto1_stp();
left_moto2_stp();
right_moto1_go();
right_moto2_go();
}
void car_translate_right()
{
push_val_left1 = push_left1+add_left1; //PWM 调节参数1-10 1为最慢,10是最快 改这个值可以改变其速度
push_val_left2 = push_left2+add_left2;
push_val_right1 = push_right1+add_right1; //PWM 调节参数1-10 1为最慢,10是最快 改这个值可以改变其速度
push_val_right2 = push_right2+add_right2;
left_moto1_go();
left_moto2_back();
right_moto1_back();
right_moto2_go();
}
/*******左1电机调速(调节push_val_left的值改变电机转速,占空比)********/
void pwm_out_left_moto1(void)
{
if(Left_moto1_stop)
{
if(pwm_val_left1<=push_val_left1)
Left_moto1_pwm_ENA=1;
else
Left_moto1_pwm_ENA=0;
if(pwm_val_left1>=10)
pwm_val_left1=0;
}
else
Left_moto1_pwm_ENA=0;
}
/*******左2电机调速(调节push_val_left的值改变电机转速,占空比)********/
void pwm_out_left_moto2(void)
{
if(Left_moto2_stop)
{
if(pwm_val_left2<=push_val_left2)
Left_moto2_pwm_ENB=1;
else
Left_moto2_pwm_ENB=0;
if(pwm_val_left2>=10)
pwm_val_left2=0;
}
else
Left_moto2_pwm_ENB=0;
}
/*******右1电机调速(调节push_val_left的值改变电机转速,占空比)********/
void pwm_out_right_moto1(void)
{
if(Right_moto1_stop)
{
if(pwm_val_right1<=push_val_right1)
Right_moto1_pwm_ENA=1;
else
Right_moto1_pwm_ENA=0;
if(pwm_val_right1>=10)
pwm_val_right1=0;
}
else
Right_moto1_pwm_ENA=0;
}
/*******右2电机调速(调节push_val_left的值改变电机转速,占空比)********/
void pwm_out_right_moto2(void)
{
if(Right_moto2_stop)
{
if(pwm_val_right2<=push_val_right2)
Right_moto2_pwm_ENB=1;
else
Right_moto2_pwm_ENB=0;
if(pwm_val_right2>=10)
pwm_val_right2=0;
}
else
Right_moto2_pwm_ENB=0;
}
/********************************************************************
* 名称 : Delay()
* 功能 : 延时,延时时间为 10ms * del
***********************************************************************/
void Delay(uint del)
{
uint i,j;
for(i=0; i<del; i++)
for(j=0; j<1827; j++)
;
}
/*******串口初始化********/
void initcom(void)
{
EA=0; //暂时关闭中断
AUXR=0x15;
BRT=0xdc;
SCON=0x50; //串口工作在模式1
ES=1; //串行中断允许
REN=1; //允许接收
EA=1; //允许中断
}
/*******定时器0和定时器1初始化********/
void initime(void)
{
EA=0; //暂时关闭中断
TMOD=0x11; //模式1设置(M1=0,M0=1)
TR0=1; //打开定时器
TH0=0XFC; //1Ms定时定时器设置,每隔1Ms发起一次中断。
TL0=0X18;
ET0=1; //开定时器0中断
TR1=1;
TH1=0Xfd; //定时器设置,每隔100微秒发起一次中断。
TL1=0xfd;
ET1=1; //开定时器1中断
EA=1; //允许中断
}
/*********发送一个字节的数据,形参d即为待发送数据**********/
void send(unsigned char d)
{
SBUF=d; //将数据写入到串口缓冲
sending=1; //设置发送标志
while(sending); //等待发送完毕
}
/**********发送字符串,直到遇到0才结束**********/
void sendc(unsigned char * pd)
{
int n;
/* while((*pd)!='\0')
{
send(*pd); //发送一个字符
pd++; //移动到下一个字符
} */
for(n=0;n<5;n++)
{
ES=0;
SBUF=pd[n];
while(!TI);
TI=0;
}
// while((*pd)!='\0')
// {
// send(*pd); //发送一个字符
// pd++; //移动到下一个字符
// }
}
/**********主程序**********/
void main()
{
initcom(); //串口初始化
initime(); //定时器0和定时器1初始化
IPH=IPH|0x10;
IP=IP|0x10;
sign_left1=1;
sign_left2=1;
sign_left3=1;
add_left1=3;
add_left2=3;
add_right1=3;
add_right2=3;
flag=1;
sign_back=1;
while(1)
{
if(flag==1&&sign_left1==0&&sign_left2==1&&sign_left3==0&&sign_left4==0&&sign_right2==0&&sign_right3==0)//中间灯扫描
{
add_left1=2;
add_left2=2;
add_right1=2;
add_right2=2;
car_run_straight();
}
if(flag==1&&sign_left1==0&&sign_left2==0&&sign_left3==1&&sign_left4==0&&sign_right2==0&&sign_right3==0)//中间灯扫描
{
add_left1=2;
add_left2=2;
add_right1=2;
add_right2=2;
car_run_straight();
}
if(flag==1&&sign_left1==0&&sign_left2==1&&sign_left3==1&&sign_left4==0&&sign_right2==0&&sign_right3==0)//中间灯扫描
{
add_left1=2;
add_left2=2;
add_right1=2;
add_right2=2;
car_run_straight();
}
if(flag==1&&sign_left1==1&&sign_left2==0&&sign_left3==0&&sign_left4==0&&sign_right2==0&&sign_right3==0)//左边碰黑线一个灯 右转
{
add_right1=2;
add_right2=2;
add_left1=3;
add_left2=3;
left_moto1_go();
left_moto2_go();
right_moto1_back();
right_moto2_back();
}
if(flag==1&&sign_left1==0&&sign_left2==0&&sign_left3==0&&sign_left4==1&&sign_right2==0&&sign_right3==0)//右边边碰黑线一个灯 左转
{
add_right1=3;
add_right2=3;
add_left1=2;
add_left2=2;
left_moto1_back();
left_moto2_back();
right_moto1_go();
right_moto2_go();
}
if(flag==1&&sign_left1==1&&sign_left2==1&&sign_left3==0&&sign_left4==0)//左边碰黑线两个灯 右转
{
add_right1=2;
add_right2=2;
add_left1=3;
add_left2=3;
left_moto1_go();
left_moto2_go();
right_moto1_back();
right_moto2_back();
}
if(flag==1&&sign_left1==0&&sign_left2==0&&sign_left3==1&&sign_left4==1&&sign_right2==0&&sign_right3==0)//右边碰黑线两个灯 左转
{
add_left1=2;
add_left2=2;
car_turn_right();
}
if(flag==1&&sign_left1==0&&sign_left2==0&&sign_left3==0&&sign_left4==0&&sign_right2==0&&sign_right3==0) // 左转
{
add_right1=3;
add_right2=3;
add_left1=2;
add_left2=2;
left_moto1_back();
left_moto2_back();
right_moto1_go();
right_moto2_go();
}
if(flag==1&&sign_left1==1&&sign_left2==1&&sign_left3==1&&sign_left4==0&&sign_right2==0&&sign_right3==0)
{
add_left1=0;
add_left2=0;
add_right1=0;
add_right2=0;
car_run_straight();
}
}
}
/********串口中断程序**********/
void uart(void) interrupt 4
{
if(RI) //收到数据
{
RI=0; //清中断请求
}
else //发送完一字节数据
{
TI=0;
sending=0; //清正在发送标志
}
}
/********定时器0中断程序**********/
void time0() interrupt 1
{
if((flag2==0)&&(sign_right2==1||sign_right3==1))
{
left_moto1_stp();
left_moto2_stp();
right_moto1_stp();
right_moto2_stp();
}
if(sign_back2==0) //光电开关扫描到//
{
flag=0;
}
if(flag==0)
{
add_left1=9;
add_left2=9;
add_right1=5;
add_right2=5;
left_moto1_go();
left_moto2_go();
right_moto1_back();
right_moto2_back();
flag2=1;
r++;
if(r>=2000)
{
if(sign_left1==0&&sign_left2==0&&sign_left3==0&&sign_left4==0)
{
add_left1=2;
add_left2=2;
add_right1=2;
add_right2=2;
left_moto1_go();
left_moto2_go();
right_moto1_go();
right_moto2_go();
}
if(sign_left1==1||sign_left2==1||sign_left3==1||sign_left4==1)
{ z++;
if(z>=5000)
{
flag2=0;
z=0;
}
flag=1;
r=0;
}
}
}
TH0=0XFC; //1Ms定时
TL0=0X18;
time++;
pwm_val_left1++;
pwm_val_left2++;
pwm_val_right1++;
pwm_val_right2++;
pwm_out_left_moto1();
pwm_out_left_moto2();
pwm_out_right_moto1();
pwm_out_right_moto2();
}
/********定时器1中断程序**********/
void time1() interrupt 3
{
TH1=0Xfd; //定时器设置
TL1=0Xfd;
}
|