注册会员
- 积分
- 139
- 威望
- 193
- 贡献
- 36
- 兑换币
- 0
- 注册时间
- 2011-4-6
- 在线时间
- 0 小时
|
#include <reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define ZHENG 1
#define FAN 0
uchar jing_x; //
uchar wei_y; //
uchar direction; //
uchar sensor; //
uchar SbufData; //
uint time1; //
uchar wpbit; //涡扇脉冲标志位
uchar jsh1;
uchar jsh2;
uchar kzhbit;
sbit ws=P3^7; //控制涡扇电机的PWM信号ws
sbit sensor1=P2^0; //传感器1输入
sbit sensor2=P2^1; //传感器2输入
sbit sensor3=P2^2; //传感器3输入
sbit sensor4=P2^3; //传感器4输入
sbit sensor5=P2^4; //传感器5输入
sbit sensor6=P2^5; //传感器6输入
sbit motor1f=P1^0; //前进左电机方向
sbit motor1=P1^1; //前进左电机运行
sbit motor3f=P1^4; //前进右电机方向
sbit motor3=P1^5; //前进右电机运行
void f_move()
{ sensor=sensor&&0x03;
for(;;)
{ if(!((sensor1==1)||(sensor2==1))) //两个前进传感器都没有信号表示正常前进
{
motor1f=ZHENG; motor3f=ZHENG;
motor1=ZHENG; motor3=ZHENG;
}
else{
if((sensor1==1)&&(sensor2==0)) //两个传感器其中一个有信号,表示偏离,采取补偿
{ motor1f=ZHENG; motor3f=ZHENG;
motor1=FAN; motor3=ZHENG;
}
else if((sensor2==1)&&(sensor1==0))
{ //zuo两个传感器其中一个有信号,表示偏离,采取补偿
motor1f=ZHENG; motor3f=ZHENG;
motor1=ZHENG; motor3=FAN;
}
else //两个传感器都有信号,表示走到横线或延直线,继续前进
{
motor1f=ZHENG; motor3f=ZHENG;
motor1=ZHENG; motor3=ZHENG;
}
}
if ((sensor3==0)&&(sensor4==0))
break;
}
}
void q_j_1(uchar f_b_0 ,uchar distance_n)
{
uchar i,j=wei_y,k=jing_x,m=0;
motor1f=ZHENG; motor3f=ZHENG;
motor1=ZHENG; motor3=ZHENG;
// delay_100ms();
for(i=0;i<distance_n;i++)
{ // Resetdbdj1(120);
f_move(); //前进
while((sensor3==1)&&(sensor4==1)) ; //直到两边的两个传感器都为1
if((++m)==distance_n)
P0=0xff;
else
{
f_move();
P0=0xff;
while((sensor3==0)&&(sensor4==0));
}
// ++m;
if(m==distance_n) //如果步数等于设定的步数
{
motor1=0; //电机停止
motor3=0;
}
switch(f_b_0) //方向判断
{
case 0: { j++; wei_y=j; }break; //纬度Y坐标值加1
case 1: { k++; jing_x=k; }break; //经度X坐标值加1
case 2: { j--; wei_y=j; }break; //纬度Y坐标值减1
case 3: { k--; jing_x=k; }break; //经度X坐标值减1
}
}
}
main()
{
q_j_1(0,3);
while(1);
} |
|