-
第一步:根据实际情况确定智能小车所需要实现的功能。例如:寻线、测距、避障、寻找静
止物体、寻找运动物体、检测边缘
……
p>
第二步:根据智能小车所需要实现的功能确定相应的硬件部分。如寻线是用红外还是用激
p>
光
……
、
测距是用
超声波还是激光
……
、
避障是用红外还
是超声波
……
寻找物体使用红外还是
超
声波还是雷达还是其他的等等。选取方案的时候既要考虑实际效果还要根据自身能力判
断
……
不要到时候进退两难。
第三步:编写程序。
第四步:实验,并找出漏洞优化程序。
当然在此之前,还有一件很重要的事要做就是选取什么单片机。
51
< br>、
Arm……
硬件连接
***********
**************************************************
***************
/
#include
#include
#define Sevro_moto_pwm
P2_7 //
接舵机信号端输入
PWM
信号调节速度
#define ECHO P2_4
//
超声波接口定义
#define
TRIG P2_5 //
超声波接口定义
#define Left_moto_go
{P1_0=1,P1_1=0,P1_2=1,P1_3=0;}
//
左边两个电机向前走
#define
Left_moto_back
{P1_0=0,P1_1=1,P1_2=0,P1_3=1;}
< br>//
左边两个电机向
后转
#define
Left_moto_Stop
{P1_0=0,P1_1=0,P1_2=0,P1_3=0;}
< br>//
左边两个电机停
转
#define Right_moto_go
{P1_4=1,P1_5=0,P1_6=1,P1_7=0;}
//
右边两个电机向前
走
#define Right_moto_back
{P1_4=0,P1_5=1,P1_6=0,P1_7=1;}
//
右边两个电机向
前走
#define Right_moto_Stop
{P1_4=0,P1_5=0,P1_6=0,P1_7=0;}
//
右边两个电机停
转
unsigned
char
const
discode[]
={
0xC0,0xF9,0xA4,0xB0,0x99,
0x92,0x82,0xF8,0x80,0x90,0xBF,0xff/*-*
/
};
unsigned char const positon[3]={
0xfe,0xfd,0xfb};
unsigned char
disbuff[4] ={ 0,0,0,0,};
unsigned char
posit=0;
unsigned char pwm_val_left =
0;//
变量定义
unsigned
char push_val_left
=14;//
舵机归中,产生约,
1.5MS
信号
unsigned long
S=0;
unsigned long S1=0;
unsigned long S2=0;
unsigned
long S3=0;
unsigned long S4=0;
unsigned int time=0;
//
时间变量
unsigned
int timer=0; //
延时基准变量
unsigned char timer1=0;
//
扫描时间变量
/******
**************************************************
****************/
void delay(unsigned
int k) //
延时函数
{
unsigned int x,y;
for(x=0;x
for(y=0;y<2000;y++);
}
/*******************************************
*****************************/
void
Display(void) //
扫描数码管
{
if(posit==0)
{P
0=(discode[disbuff[posit]])&0x7f;}//
产生点
else
{P0=discode[disbuff[posit]];}
if(posit==0)
{ P2_1=0;P2_2=1;P2_3=1;}
if(posit==1)
{P2_1=1;P2_2=0;P2_3=1;}
if(posit==2)
{P2_1=1;P2_2=1;P2_3=0;}
if(++posit>=3)
posit=0;
}
/*************************
***********************************************/
void StartModule()
//
启动测距信号
{
TRIG=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
TRIG=0;
}
/******
*********************************************/
void Conut(void)
//
计算距离
{
while(!ECHO); //
当
RX
为零时等待
TR0=1;
//
开启计数
while(ECHO); //
当
R
X
为
1
计数并等待
TR0=0; //
关闭计数
time=TH0*256+TL0;
//
读取脉宽长度
TH0=0;
TL0=0;
S=(time*1.7)/100;
//
算出来是
CM
disbuff[0]=S%1000/100;
//
更新显示
disbuff[1]=S%1000%100/10;
disbuff[2]=S%1000%10 %10;
}
/**************************************
**********************************/
//
前速前进
void run(void)
{
Left_moto_go
//
左电机往前走
Right_moto_go
//
右电机往前走
}
/********************************************
****************************/
//
前速后退
void backrun(void)
{
Left_moto_back
//
左电机往前走
Right_moto_back
//
右电机往前走
}
/********************************************
****************************/
//
左转
void leftrun(void)
{
Left_moto_back
//
左电机往前走
Right_moto_go
//
右电机往前走
}
/********************************************
****************************/
//
右转
void rightrun(void)
{
Left_moto_go
//
左电机往前走
Right_moto_back
//
右电机往前走
}
/********************************************
****************************/
//STOP
void stoprun(void)
{
Left_moto_Stop
//
左电机停走
Right_moto_Stop
//
右电机停走
}
/*********************************************
***************************/
void COMM(
void )
{
push_val_left=5; //
舵机向左转
90
度
timer=0;
while(timer<=4000);
//
延时
400MS
让舵机转到其位置
StartModule();
//
启动超声波测距
Conut(); //
计算距离
S2=S;
push_val_left=23; //
舵机向右转
90
度
timer=0;
while(timer<=4000);
//
延时
400MS
让舵机转到其位置
StartModule();
//
启动超声波测距
Conut(); //
计算距离
S4=S;
push_val_left=14;
//
舵机归中
timer=0;
while(timer<=4000);
//
延时
400MS
让舵机转到其位置
< br>
StartModule();
//
启动超
声波测距
Conut(); //
计算距离
S1=S; if((S2<20)||(S4<20)) //
只要左右各有距离小于
20CM
小车后退
{
backrun();
//
后退
timer=0;
while(timer<=4000);
}
if(S2>S4)
{
rightrun();
//
车的左边比车的右边距离小
右转
timer=0;
while(timer<=4000);
}
else
{
leftrun();
//
车的左边比车的右边距离大
左转
timer=0;
while(timer<=4000);
}
} /************************************
************************************/
/* PWM
调制电机转速
*/
/************************
************************************************/
/*
左电机调速
*/
/*
调节
push_val_left
的值改变电机转速
,
占空比
*/
void
pwm_Servomoto(void)
{
if(pwm_val_left<=push_val_left)
Sevro_moto_pwm=1;
else
Sevro_moto_pwm=0;
if(pwm_val_left>=200)
pwm_val_left=0;
}
/*************************
**************************/
///*TIMER1<
/p>
中断服务子函数产生
PWM
信号
*/
void time1()interrupt 3 using
2
{
TH1=(65536-100)/256;
//100US
定时
TL1=(65536-100)%256;
timer++; //
定时器
100
US
为准。在这个基础上延时
pwm_val_left++;
pwm_Servomoto(); timer1++;
//2MS
扫一次数码管
if(timer1>=20)
{
timer1=0;
Display();
}
}
/************
***************************************/
///*TIMER0
中断服务子函数产生
PWM
信号
*/
void
timer0()interrupt 1 using 0
{
} /************************
***************************/ void main(void)
{ TMOD=0X11;
TH1=(65536-100)/256;
//100US
定时
TL1=(65536-100)%256;
-
-
-
-
-
-
-
-
-
上一篇:幼儿园进餐分餐制管理制度
下一篇:施耐德PLC初学者指南-高速计数器与PWM