无线加循迹程序问题,求帮助
时间:10-02
整理:3721RD
点击:
/**************************************************************
名称:无线控制的小车
说明:
1、小车前后左右停止控制
2、请注意舵机接线与程序中的一致
***************************************************************/
#include <reg52.h>
#include <string.h>
#define uchar unsigned char
#define uint unsigned int
#define l_stop 0//110
#define r_stop 0 //110
#define Stop 242
#define Forward 243
#define Backward 244
#define TurnLeft 245
#define TurnRight 246
uchar i;
uchar l_speed;
uchar r_speed;
uint a,b;//a-left;b-right
sbit LeftCon=P2^0;
sbit RightCon=P2^1;
sbit ObsacleDetect=P3^3; //障碍物检测口
void init()//初始化
{
i=0;
a=1750;
b=1750;
TMOD=0x01;
TH0=-((1500+l_stop*2)/256);
TL0=-((1500+r_stop*2)%256);
TR0=1;
EA=1;
ET0=1;
}
void servo() interrupt 1 using 1
{
switch(i)
{
case 0:
{
TH0=(-a)/256;//a=l_stop*2+1500
TL0=(-a)%256;
LeftCon=1;
i++;
break;
}
case 1:
{
TH0=(-(2500-a))/256;
TL0=(-(2500-a))%256;
LeftCon=0;
i++;
break;
}
case 2:
{
TH0=(-b)/256;
TL0=(-b)%256;
RightCon=1;
i++;
break;
}
case 3:
{
TH0=(-(17500-b))/256;
TL0=(-(17500-b))%256;
RightCon=0;
i=0;
break;
}
}
}
//_____________________________________________________________________________________________________________
//动作函数库
void action(uchar num)
{
switch (num)
{
case Stop://stop
{
a=l_stop*2+1500;
b=r_stop*2+1500;
}
break;
case Forward: //forward
{
a=(l_stop+50)*2+1500;
b=(r_stop-50)*2+1500;
}
break;
case Backward: //backward
{
a=(l_stop-50)*2+1500;
b=(r_stop+50)*2+1500;
}
break;
case TurnLeft: //turn left
{
a=(l_stop-50)*2+1500;
b=(r_stop-50)*2+1500;
}
break;
case TurnRight://turn right
{
a=(l_stop+50)*2+1500;
b=(r_stop+50)*2+1500;
}
break;
default://stop
a=l_stop*2+1500;
b=r_stop*2+1500;
break;
}
}
//延时
void DelayMs(uint ms)
{
uint i,j;
for(i=0;i<ms;i++)
{
for(j=0;j<1000;j++);
}
}
void ex0_init()
{
EA=1;
EX0=1;
IT0=1;
}
void ex0_ISR() interrupt 0
{
uchar remote_val;
remote_val=P1&0x0f;
switch(remote_val)
{
case 0x01:
action(TurnRight);
break;
case 0x08:
action(TurnLeft);
break;
case 0x04:
action(Forward);
break;
case 0x02:
action(Backward);
break;
case 0x03:
action(Stop);
break;
default:break;
}
}
//*******************************************************************************************
void main(void)
{
init(); //初始化
ex0_init();
action(Stop);
while ( 1 )
{
DelayMs(80);
}
}
循迹程序
/**************************************************************
名称:小车实验
说明:
1、小车前后左右停止控制
2、请注意舵机接线与程序中的一致
***************************************************************/
#include <reg52.h>
#include <string.h>
#define uchar unsigned char
#define uint unsigned int
#define l_stop 0//110
#define r_stop 0 //110
#define Stop 242
#define Forward 243
#define Backward 244
#define TurnLeft 245
#define TurnRight 246
uchar i;
uchar l_speed;
uchar r_speed;
uint a,b;//a-left;b-right
sbit LeftCon=P2^0;
sbit RightCon=P2^1;
sbit ObsacleDetect=P3^3; //障碍物检测口
void init()//初始化
{
i=0;
a=1750;
b=1750;
TMOD=0x01;
TH0=-((1500+l_stop*2)/256);
TL0=-((1500+r_stop*2)%256);
TR0=1;
EA=1;
ET0=1;
}
void servo() interrupt 1 using 1
{
switch(i)
{
case 0:
{
TH0=(-a)/256;//a=l_stop*2+1500
TL0=(-a)%256;
LeftCon=1;
i++;
break;
}
case 1:
{
TH0=(-(2500-a))/256;
TL0=(-(2500-a))%256;
LeftCon=0;
i++;
break;
}
case 2:
{
TH0=(-b)/256;
TL0=(-b)%256;
RightCon=1;
i++;
break;
}
case 3:
{
TH0=(-(17500-b))/256;
TL0=(-(17500-b))%256;
RightCon=0;
i=0;
break;
}
}
}
//_____________________________________________________________________________________________________________
//动作函数库
void action(uchar num)
{
switch (num)
{
case Stop://stop
{
a=l_stop*2+1500;
b=r_stop*2+1500;
}
break;
case Forward: //forward
{
// a=(l_stop+50)*2+1500;
// b=(r_stop-50)*2+1500;
a=(l_stop+30)*2+1500;
b=(r_stop-30)*2+1500;
}
break;
case Backward: //backward
{
a=(l_stop-50)*2+1500;
b=(r_stop+50)*2+1500;
}
break;
case TurnLeft: //turn left
{
// a=(l_stop-50)*2+1500;
a=(l_stop-20)*2+1500;
b=(r_stop-50)*2+1500;
}
break;
case TurnRight://turn right
{
a=(l_stop+50)*2+1500;
//b=(r_stop+50)*2+1500;
b=(r_stop+20)*2+1500;
}
break;
default://stop
a=l_stop*2+1500;
b=r_stop*2+1500;
break;
}
}
//延时
void DelayMs(uint ms)
{
uint i,j;
for(i=0;i<ms;i++)
{
for(j=0;j<1000;j++);
}
}
//------循迹程序
void trace_line()
{
uchar temp;
temp=P0&0x0f;
switch(temp)
{
case 0x06: //0110 0100 0010 直行
case 0x04:
case 0x02:
action(Forward);
break;
case 0x01: //0001 0011 0111 偏右
case 0x03:
case 0x07:
action(TurnLeft);
break;
case 0x08: //1000 1100 1110 偏左
case 0x0c:
case 0x0e:
case 0x00:
action(TurnRight);
break;
case 0x0f: //1111 停止
action(Stop);
break;
default:break;
}
}
//*******************************************************************************************
void main(void)
{
init(); //初始化
while ( 1 )
{
trace_line();
DelayMs(80);
}
}
程序之间不知道怎么办,搞不好两个合并一起,弄个无线加循迹。
名称:无线控制的小车
说明:
1、小车前后左右停止控制
2、请注意舵机接线与程序中的一致
***************************************************************/
#include <reg52.h>
#include <string.h>
#define uchar unsigned char
#define uint unsigned int
#define l_stop 0//110
#define r_stop 0 //110
#define Stop 242
#define Forward 243
#define Backward 244
#define TurnLeft 245
#define TurnRight 246
uchar i;
uchar l_speed;
uchar r_speed;
uint a,b;//a-left;b-right
sbit LeftCon=P2^0;
sbit RightCon=P2^1;
sbit ObsacleDetect=P3^3; //障碍物检测口
void init()//初始化
{
i=0;
a=1750;
b=1750;
TMOD=0x01;
TH0=-((1500+l_stop*2)/256);
TL0=-((1500+r_stop*2)%256);
TR0=1;
EA=1;
ET0=1;
}
void servo() interrupt 1 using 1
{
switch(i)
{
case 0:
{
TH0=(-a)/256;//a=l_stop*2+1500
TL0=(-a)%256;
LeftCon=1;
i++;
break;
}
case 1:
{
TH0=(-(2500-a))/256;
TL0=(-(2500-a))%256;
LeftCon=0;
i++;
break;
}
case 2:
{
TH0=(-b)/256;
TL0=(-b)%256;
RightCon=1;
i++;
break;
}
case 3:
{
TH0=(-(17500-b))/256;
TL0=(-(17500-b))%256;
RightCon=0;
i=0;
break;
}
}
}
//_____________________________________________________________________________________________________________
//动作函数库
void action(uchar num)
{
switch (num)
{
case Stop://stop
{
a=l_stop*2+1500;
b=r_stop*2+1500;
}
break;
case Forward: //forward
{
a=(l_stop+50)*2+1500;
b=(r_stop-50)*2+1500;
}
break;
case Backward: //backward
{
a=(l_stop-50)*2+1500;
b=(r_stop+50)*2+1500;
}
break;
case TurnLeft: //turn left
{
a=(l_stop-50)*2+1500;
b=(r_stop-50)*2+1500;
}
break;
case TurnRight://turn right
{
a=(l_stop+50)*2+1500;
b=(r_stop+50)*2+1500;
}
break;
default://stop
a=l_stop*2+1500;
b=r_stop*2+1500;
break;
}
}
//延时
void DelayMs(uint ms)
{
uint i,j;
for(i=0;i<ms;i++)
{
for(j=0;j<1000;j++);
}
}
void ex0_init()
{
EA=1;
EX0=1;
IT0=1;
}
void ex0_ISR() interrupt 0
{
uchar remote_val;
remote_val=P1&0x0f;
switch(remote_val)
{
case 0x01:
action(TurnRight);
break;
case 0x08:
action(TurnLeft);
break;
case 0x04:
action(Forward);
break;
case 0x02:
action(Backward);
break;
case 0x03:
action(Stop);
break;
default:break;
}
}
//*******************************************************************************************
void main(void)
{
init(); //初始化
ex0_init();
action(Stop);
while ( 1 )
{
DelayMs(80);
}
}
循迹程序
/**************************************************************
名称:小车实验
说明:
1、小车前后左右停止控制
2、请注意舵机接线与程序中的一致
***************************************************************/
#include <reg52.h>
#include <string.h>
#define uchar unsigned char
#define uint unsigned int
#define l_stop 0//110
#define r_stop 0 //110
#define Stop 242
#define Forward 243
#define Backward 244
#define TurnLeft 245
#define TurnRight 246
uchar i;
uchar l_speed;
uchar r_speed;
uint a,b;//a-left;b-right
sbit LeftCon=P2^0;
sbit RightCon=P2^1;
sbit ObsacleDetect=P3^3; //障碍物检测口
void init()//初始化
{
i=0;
a=1750;
b=1750;
TMOD=0x01;
TH0=-((1500+l_stop*2)/256);
TL0=-((1500+r_stop*2)%256);
TR0=1;
EA=1;
ET0=1;
}
void servo() interrupt 1 using 1
{
switch(i)
{
case 0:
{
TH0=(-a)/256;//a=l_stop*2+1500
TL0=(-a)%256;
LeftCon=1;
i++;
break;
}
case 1:
{
TH0=(-(2500-a))/256;
TL0=(-(2500-a))%256;
LeftCon=0;
i++;
break;
}
case 2:
{
TH0=(-b)/256;
TL0=(-b)%256;
RightCon=1;
i++;
break;
}
case 3:
{
TH0=(-(17500-b))/256;
TL0=(-(17500-b))%256;
RightCon=0;
i=0;
break;
}
}
}
//_____________________________________________________________________________________________________________
//动作函数库
void action(uchar num)
{
switch (num)
{
case Stop://stop
{
a=l_stop*2+1500;
b=r_stop*2+1500;
}
break;
case Forward: //forward
{
// a=(l_stop+50)*2+1500;
// b=(r_stop-50)*2+1500;
a=(l_stop+30)*2+1500;
b=(r_stop-30)*2+1500;
}
break;
case Backward: //backward
{
a=(l_stop-50)*2+1500;
b=(r_stop+50)*2+1500;
}
break;
case TurnLeft: //turn left
{
// a=(l_stop-50)*2+1500;
a=(l_stop-20)*2+1500;
b=(r_stop-50)*2+1500;
}
break;
case TurnRight://turn right
{
a=(l_stop+50)*2+1500;
//b=(r_stop+50)*2+1500;
b=(r_stop+20)*2+1500;
}
break;
default://stop
a=l_stop*2+1500;
b=r_stop*2+1500;
break;
}
}
//延时
void DelayMs(uint ms)
{
uint i,j;
for(i=0;i<ms;i++)
{
for(j=0;j<1000;j++);
}
}
//------循迹程序
void trace_line()
{
uchar temp;
temp=P0&0x0f;
switch(temp)
{
case 0x06: //0110 0100 0010 直行
case 0x04:
case 0x02:
action(Forward);
break;
case 0x01: //0001 0011 0111 偏右
case 0x03:
case 0x07:
action(TurnLeft);
break;
case 0x08: //1000 1100 1110 偏左
case 0x0c:
case 0x0e:
case 0x00:
action(TurnRight);
break;
case 0x0f: //1111 停止
action(Stop);
break;
default:break;
}
}
//*******************************************************************************************
void main(void)
{
init(); //初始化
while ( 1 )
{
trace_line();
DelayMs(80);
}
}
程序之间不知道怎么办,搞不好两个合并一起,弄个无线加循迹。