从硬件到软件的循迹小车
硬件模块
1、驱动: 驱动用法:参考此链接
2、循迹模块: 原理可参考:参考链接
3、电池盒
4、51单片机
5、小车板子:
软件代码
#include<AT89X52.H>
#include<ZY-4WD_PWM.H>
sbit duoji=P1^0;
void main(void)
{
unsigned char i;
P1=0X00;
TMOD=0X01;
TH0= 0XFc;
TL0= 0X18;
TR0= 1;
ET0= 1;
EA = 1;
while(1)
{
if(Left_1_led==0&&Right_1_led==0)
run();
else
{
if(Left_1_led==1&&Right_1_led==0)
{
leftrun();
}
if(Right_1_led==1&&Left_1_led==0)
{
rightrun();
}
if(Right_1_led==1&&Left_1_led==1)
{
stop();
}
}
}
}
#ifndef _LED_H_
#define _LED_H_
sbit L293D_IN1=P1^2;
sbit L293D_IN2=P1^3;
sbit L293D_IN3=P1^4;
sbit L293D_IN4=P1^5;
sbit L293D_EN1=P1^6;
sbit L293D_EN2=P1^7;
sbit BUZZ=P2^3;
#define Left_1_led P3_7
#define Right_1_led P3_6
#define Left_moto_pwm P1_6
#define Right_moto_pwm P1_7
#define Left_moto_go {P1_2=1,P1_3=0;}
#define Left_moto_back {P1_2=0,P1_3=1;}
#define Left_moto_Stop {P1_2=0,P1_3=0;}
#define Right_moto_go {P1_4=1,P1_5=0;}
#define Right_moto_back {P1_4=0,P1_5=1;}
#define Right_moto_Stop {P1_4=0,P1_5=0;}
unsigned char pwm_val_left =0;
unsigned char push_val_left =0;
unsigned char pwm_val_right =0;
unsigned char push_val_right=0;
bit Right_moto_stop=1;
bit Left_moto_stop =1;
unsigned int time=0;
void delay(unsigned int k)
{
unsigned int x,y;
for(x=0;x<k;x++)
for(y=0;y<2000;y++);
}
void run(void)
{
push_val_left=18;
push_val_right=18;
Left_moto_go ;
Right_moto_go ;
}
void run1(void)
{
push_val_left=10;
push_val_right=8;
Left_moto_go ;
Right_moto_back ;
}
void backrun(void)
{
push_val_left=5;
push_val_right=5;
Left_moto_back;
Right_moto_back;
}
void leftrun(void)
{
push_val_left=5;
push_val_right=9;
Right_moto_go ;
Left_moto_back ;
}
void rightrun(void)
{
push_val_left=9;
push_val_right=5;
Left_moto_go;
Right_moto_back;
}
void stop(void)
{
Right_moto_Stop ;
Left_moto_Stop ;
}
void pwm_out_left_moto(void)
{
if(Left_moto_stop)
{
if(pwm_val_left<=push_val_left)
{
Left_moto_pwm=1;
}
else
{
Left_moto_pwm=0;
}
if(pwm_val_left>=20)
pwm_val_left=0;
}
else
{
Left_moto_pwm=0;
}
}
void pwm_out_right_moto(void)
{
if(Right_moto_stop)
{
if(pwm_val_right<=push_val_right)
{
Right_moto_pwm=1;
}
else
{
Right_moto_pwm=0;
}
if(pwm_val_right>=20)
pwm_val_right=0;
}
else
{
Right_moto_pwm=0;
}
}
void timer0()interrupt 1 using 2
{
TH0=0XFc;
TL0=0X18;
time++;
pwm_val_left++;
pwm_val_right++;
pwm_out_left_moto();
pwm_out_right_moto();
}
#endif
注意事项:
1、代码当中可以通过这个数字调速度大小,这个在循迹过程中重要,太快容易冲出去; 2、循迹模块的灵敏度可以通过按钮调节
|