找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2802|回复: 5
打印 上一主题 下一主题
收起左侧

新人求助,智能小车程序冲突,求助大神!

[复制链接]
跳转到指定楼层
楼主
ID:254358 发表于 2018-10-26 15:53 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
100黑币
新人求助:这是一个超声波寻迹小车的程序,但现在除了一些问题,求各位大神能帮帮忙,将其修正。
需要达到的效果,四路小车能按照黑带自动寻迹,到达终点时能切换至超声波测距。
//hc-sr04 超声波测距模块 DEMO 程序
//晶振:11。0592
//接线:模块TRIG接 P3.7  ECH0 接P3.6
//数码管:共阳数码管P0接数据口,P2.2 P2.1 P2.0接选通数码管
/***********************************************************************************************************/     
#include <AT89x51.H>  //器件配置文件
#include <intrins.h>
# define Left_moto_go       {P1_0=1,P1_1=0,P1_2=0,P1_3=1;}   
# define Left_moto_back     {P1_0=0,P1_1=1,P1_2=1,P1_3=0;}

# define Right_moto_go     {P1_4=1,P1_5=0,P1_6=0,P1_7=1;}
# define Right_moto_back {P1_4=0,P1_5=1,P1_6=1,P1_7=0;}

# define Stop {P1_0 = 0,P1_1 = 0,P1_2 = 0,P1_3 = 0,P1_4 = 0,P1_5 = 0,P1_6 = 0,P1_7 = 0;}
# define S1  P2_4
# define S2  P2_5
# define S3  P2_6
# define S4  P2_7
#define  RX  P3_6
#define  TX  P3_7
unsigned int  time=0;
unsigned int  timer=0;
unsigned char posit=0;
unsigned long S=0;
bit      flag =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,};
int n = 0 ;
void run(void);
void right2trun(void);
void right1trun(void);
void left2run(void);
void left1run(void);
void xunji(void);
/********************************************************/
    void Display(void)     //扫描数码管
{
  if(posit==0)
  {P0=(discode[disbuff[posit]])&0x7f;}
  else
  {P0=discode[disbuff[posit]];}
   P2=positon[posit];
   if(++posit>=3)
   posit=0;
}
/********************************************************/
    void Conut(void)
{
  time=TH0*256+TL0;
  TH0=0;
  TL0=0;

  S=(time*1.7)/100;     //算出来是CM
  if((S>=700)||flag==1) //超出测量范围显示“-”
  {  
   flag=0;
   disbuff[0]=0;    //“-”
   disbuff[1]=0;    //“-”
   disbuff[2]=0;    //“-”
  }
  else
  {
   disbuff[0]=S%1000/100;
   disbuff[1]=S%1000%100/10;
   disbuff[2]=S%1000%10 %10;
  }
}
/********************************************************/
     void zd0() interrupt 1    //T0中断用来计数器溢出,超过测距范围
  {
flag=1;
          //中断溢出标志
  }
/********************************************************/
   void  zd3()  interrupt 3    //T1中断用来扫描数码管和计800MS启动模块
  {
  TH1=0xf8;
  TL1=0x30;
  Display();
  timer++;
  if(timer>=400)
  {
   timer=0;
   TX=1;                   //800MS  启动一次模块
   _nop_();
   _nop_();
   _nop_();
   _nop_();
   _nop_();
   _nop_();
   _nop_();
   _nop_();
   _nop_();
   _nop_();
   _nop_();
   _nop_();
   _nop_();
   _nop_();
   _nop_();
   _nop_();
   _nop_();
   _nop_();
   _nop_();
   _nop_();
   _nop_();
   TX=0;
  }
  }
void run()
{  
  static int a = 2;
   if(--a >= 1)
    Stop;
   if(a == 0)
   {
   Left_moto_go;
   Right_moto_go;
   a = 2;
   }
}
void right2trun()
{   

  static int b2 = 3;

  if(--b2 >= 1)
  {
   Stop;
  }
  if(b2 == 0)
  {
   
   Left_moto_go;
   Right_moto_back;
   b2 = 3;
  }
  
}
void right1trun()
{   

  int r = 1;
  while(r)
  {
   Left_moto_go;
   Right_moto_back;

   if(S3 == 0)
    r = 0 ;
  }
  
}
void left2run()
{
  static int c2 =2;

  if(--c2 >= 1)
  {
  Stop;
  }
  if(c2 == 0)
  {
   Left_moto_back;
   Right_moto_go;
   c2 = 2;
  }
  
}
void left1run()
{

int l = 1;
while(l)
{
  Left_moto_back;
  Right_moto_go;
  if(S2 == 0)
    l =  0;
}
}
void xunji()
{
n=n+1;
if(n == 1)
{
  n = 0 ;
  if(S1 == 1 && S2 == 1 && S3 == 0 && S4 == 1)
  {
   right2trun();
  }
   if((S1 == 1 && S2 == 1 && S3 == 0 && S4 == 0) || (S1 == 1 && S2 == 1 && S3 == 1 && S4 == 0))
  {           
   right1trun();
  }
   if(S1 == 1 && S2 == 0 && S3 == 1 && S4 == 1)
  {
   left2run();
  }
   if((S1 == 0 && S2 == 0 && S3 == 1 && S4 == 1)||(S1 == 0 && S2 == 1 && S3 == 1 && S4 == 1))
  {
   left1run();
  }
   if(S1 == 0&& S2 == 0 && S3 == 0 && S4 == 0)
  {
   Stop;
  }
   if(S1 == 1 && S2 == 1&& S3 == 1 && S4 == 1)
  {
   run();
  }
}
  else
  {
   Stop;
  }
}
/*********************************************************/
void  main(  void  )
  {  
    TMOD=0x11;     //设T0为方式1,GATE=1;
TH0=0;
TL0=0;         
TH1=0xf8;     //2MS定时
TL1=0x30;
ET0=1;             //允许T0中断
ET1=1;      //允许T1中断
TR1=1;      //开启定时器
EA=1;      //开启总中断
run();
while(1)
{
xunji();
if(S1==0&&S2==0&&S3==0&&S4==0)
{
  while(!RX);  //当RX为零时等待
  TR0=1;       //开启计数
  while(RX);   //当RX为1计数并等待
  TR0=0;    //关闭计数
     Conut();   //计算
}
   }
}
               

SmartCar_CPU.pdf

1.99 MB, 下载次数: 5, 下载积分: 黑币 -5

智能小车原理图

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:303383 发表于 2018-10-26 19:17 | 只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
回复

使用道具 举报

板凳
ID:481641 发表于 2019-5-8 16:04 | 只看该作者
程序拆开,分块用
回复

使用道具 举报

地板
ID:481641 发表于 2019-8-16 15:44 | 只看该作者
一块一块的调试
回复

使用道具 举报

5#
ID:560467 发表于 2019-8-17 15:18 | 只看该作者
你怎么没说是遇到了什么问题??怎么回答呀?
回复

使用道具 举报

6#
ID:156220 发表于 2019-8-17 17:12 | 只看该作者
你应该把出现的问题列出来,
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表