- UID
- 191144
- 在线时间
- 0 小时
- 最后登录
- 2013-7-30
- 注册时间
- 2012-8-15
- 宅魂
- 45 点
- 贡献
- 0 点
- 宅币
- 132 枚
- 宅の石(入宅度)
- 0 块
- 元气(技能点)
- 5 点
- 活跃
- 0 ℃
- 听众
- 4
- 收听
- 0
该用户从未签到
New Game
- 积分
- 517
|
发表于 2012-8-15 21:42:02
|
显示全部楼层
看了各位大神作品.....无比膜拜!!!!!
小弟擅长点单片机开发,平常爱做点电子制作.....(这个也算吧!!!)
最近在学 arm7 不过还没整出啥咚咚......
所以这里只好把去年用 51单片机做的智能小车贴出来(用c写的,......会c和c++以及一点vb吧)
照片一枚..
c写的程序
#include<reg52.h>
typedef unsigned char uint8;
typedef unsigned int uint16;
uint8 count_L=0;//用于定时计数的两个全局变量声明
uint8 count_R=0;
uint8 num=0;
sbit ENA=P1^0;//左电机的使能端
sbit ENB=P1^1;//右电机的使能端
sbit IN1=P1^2;//控制左电机
sbit IN2=P1^3;//控制左电机
sbit IN3=P1^4;//控制右电机
sbit IN4=P1^5;//控制右电机
sbit S_L=P2^0;//左端传感器
sbit S_M=P2^1;//中端传感器
sbit S_R=P2^2;//右端传感器
sbit L=P2^3;//左红外探测传感器
sbit M=P2^4;//中红外探测传感器
sbit R=P2^5;//右红外探测传感器
sbit distantA=P3^0;//遥控往前
sbit distantB=P3^1;//遥控往后
sbit distantC=P3^2;//遥控往左
sbit KEY=P3^3;//功能转换键
sbit distantE=P3^4;//遥控VT
void forward_L()//左电机前转
{
IN1=0;
IN2=1;
}
void forward_R()//右电机前转
{
IN3=0;
IN4=1;
}
void back_L()//左电机后转
{
IN1=1;
IN2=0;
}
void back_R()//右电机后转
{
IN3=1;
IN4=0;
}
void speed(int ct_L,int sd_L,int ct_R,int sd_R)//调速函数
{
if(ct_L<=sd_L)//有效电平
ENA=1;
else
ENA=0;
if(ct_R<=sd_R)//有效电平
ENB=1;
else
ENB=0;
}
void spin(int ct_L,int sd_L,int ct_R,int sd_R)//小车旋转函数
{
back_L();
forward_R();
speed(ct_L,sd_L,ct_R,sd_R);
}
void move(int ct_L,int sd_L,int ct_R,int sd_R)//小车转弯与前进函数
{
forward_L();
forward_R();
speed(ct_L,sd_L,ct_R,sd_R);
}
void backmove(int ct_L,int sd_L,int ct_R,int sd_R)//小车后退函数
{
back_L();
back_R();
speed(ct_L,sd_L,ct_R,sd_R);
}
void stop()//小车停止函数
{
IN1=0;
IN2=0;
IN3=0;
IN4=0;
}
void timer0()
{
TMOD|=0x01;
TMOD&=0xFD;//定时器0工作在模式1
TR0=1;
TH0=0xFC;//计数初值,定时1ms
TL0=0x67;
}
void delay(uint16 N)
{
uint16 x;
for(x=0; x<=N;x++);
}
void tracking() //循迹程序
{
while(1)
{
if(KEY==1){delay(10000);if(KEY==1)break;}
if(TF0==1)
{
TF0=0;
TH0=0xFC;
TL0=0x67;
count_L++;
count_R++;
}
if(count_L==100) //周期是100ms
count_L=0;
if(count_R==100) //周期是100ms
count_R=0;
if(S_L==1&&S_M==0&&S_R==1)
move(count_L,70,count_R,70);//直线前进
else if(S_L==1&&S_M==0&&S_R==0||S_L==1&&S_M ==1&&S_R==0)
move(count_L,80,count_R,10);//右转
else if(S_L==0&&S_M==0&&S_R==1||S_L==0&&S_M==1&&S_R==1)
move(count_L,10,count_R,80);//左转
else if(S_L ==1&&S_M==1&&S_R ==1)
backmove(count_L,70,count_R,70);//后退
else if(S_L ==0&&S_M==0&&S_R ==0)
stop();//停止
}
}
void Object_Detection()//避障函数
{
while(1)
{
if(KEY==1){delay(10000);if(KEY==1)break;}
if(TF0==1)
{
TF0=0;
TH0=0xFC;
TL0=0x67;
count_L++;
count_R++;
}
if(count_L==100) //周期是100ms
count_L=0;
if(count_R==100) //周期是100ms
count_R=0;
if(L==1&&M==1&&R==1)
move(count_L,70,count_R,70);//直线前进
else
if(L==0&&R==1)
move(count_L,80,count_R,10);//右转
else
if(L==1&&R==0)
move(count_L,10,count_R,80);//左转
else
if(L==0&&M==0&&R==0)
backmove(count_L,80,count_R,10);//右后方转
}
}
void telecontrol()//遥控程序
{
while(1)
{
if(KEY==1){delay(10000);if(KEY==1)break;}
if(TF0==1)
{
TF0=0;
TH0=0xFC;
TL0=0x67;
count_L++;
count_R++;
}
if(count_L==100) //周期是100ms
count_L=0;
if(count_R==100) //周期是100ms
count_R=0;
if(distantA==1&&distantB==0&&distantC==0)
move(count_L,70,count_R,70);//直线前进
else if(distantA==0&&distantB==1&&distantC==0)
backmove(count_L,70,count_R,70);//后退
else if(distantA==0&&distantB==0&&distantC==1)
spin(count_L,10,count_R,80);//转
else stop();
}
}
void main()
{
timer0();
while(1)
{
if(num>2) num=0;
switch(num)//选择小车功能
{
case 0:telecontrol();num++;break;
case 1:tracking();num++;break;
case 2:Object_Detection();num++;break;
default:telecontrol();
}
}
} |
评分
-
查看全部评分
|