找回密码
 立即注册

QQ登录

只需一步,快速开始

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

为什么我单片机程序的超声波不能检测?其它的功能是好的 求帮助

[复制链接]
跳转到指定楼层
楼主
ID:440456 发表于 2019-7-4 16:12 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include "reg52.h"
#include "delay.h"
#define right_moto_go    {IN2=1,IN3=0,IN4=1,IN5=0;}  //右前电机
#define left_moto_go     {IN0=1,IN1=0,IN6=1,IN7=0;}  //左前电机
#define left_moto_back   {IN2=0,IN3=1,IN4=0,IN5=1;}  //右后电机
#define right_moto_back  {IN0=0,IN1=1,IN6=0,IN7=1;}  //左后电机
#define right_moto_stop  {IN2=0,IN3=0,IN4=0,IN5=0;}  //左电机停止
#define left_moto_stop   {IN0=0,IN1=0,IN6=0,IN7=0;}  //右电机停止

sbit IN0=P0^0;
sbit IN1=P0^1;
sbit IN2=P0^2;
sbit IN3=P0^3;
sbit IN4=P0^4;
sbit IN5=P0^5;
sbit IN6=P0^6;
sbit IN7=P0^7;
sbit h1=P1^0;
sbit h2=P1^1;
sbit h3=P1^2;
sbit h4=P1^3;
sbit EN1=P1^4;
sbit EN2=P1^5;
sbit EN3=P1^6;
sbit EN4=P1^7;
sbit TRIG=P2^1;
sbit ECHO=P2^0;

uchar temp;
uchar right_moto=0;   
uchar left_moto=0;
uchar PWM_moto_right=0;
uchar PWM_moto_left=0;

float S;
void TIM0init(void);     //定时器0
void Timer2Init(void);  //定时器2
void UartInit(void);    //串口通讯
void send(uchar a);

void run0(void) //前进0
{
left_moto=10;
right_moto=10;
right_moto_go;
left_moto_go;
}
void run1(void)   //前进1
{
left_moto=6;
right_moto=6;
right_moto_go;
left_moto_go;
}
void run3(void)  //前进3
{
left_moto=15;
right_moto=15;
right_moto_go;
left_moto_go;
}
void run2(void)   //前进2
{
left_moto=7;
right_moto=7;
right_moto_go;
left_moto_go;
}
void back1(void)   //后退1
{
left_moto=9;
right_moto=9;
right_moto_back;
left_moto_back;
}
void back2(void)   //后退2
{
left_moto=15;
right_moto=15;
right_moto_back;
left_moto_back;
}
void left1(void)   //左转1
{
left_moto=15;
right_moto=15;
right_moto_stop;
left_moto_go;
}
void left2(void)   //左转2
{
left_moto=15;
right_moto=15;
right_moto_go;
left_moto_stop;
}
void right1(void)   //右转1
{
left_moto=15;
right_moto=15;
right_moto_go;
left_moto_stop;
}
void right2(void)  //右转2
{
left_moto=12;
right_moto=12;
right_moto_back;
left_moto_go;
}
void stop(void)     //停止
{
right_moto_stop;
left_moto_stop;
}
void csb(void)      //超声波避障
{

    TRIG=1;      
    DelayUs2x(10);       //触发信号是高电平脉冲,宽度大于10us
    TRIG=0;
    while(!ECHO);      //等待高电平
    TR0=1;
    while(ECHO);       //等待低电平
    TR0=0;
    S=TH0*256+TL0;
    S=S/58;      
      
    TH0=0;
    TL0=0;            //清除定时器0寄存器中的值
   if(S>20){run0();}
    else
    {
     right2();
     DelayMs(1000);
    }
}
void Track(void)      //循迹
{

if((h1==1)&&(h2==1)&&(h3==1)&&(h4==1))
{run1();}
else if((h1==0)&&(h2==1)&&(h3==0)&&(h4==0))
{run1();}
else if((h1==0)&&(h2==1)&&(h3==1)&&(h4==0))
{run1();}
else if((h1==0)&&(h2==0)&&(h3==1)&&(h4==0))
{run1();}
else if((h1==0)&&(h2==1)&&(h3==1)&&(h4==1))
{right1();}
else if((h1==0)&&(h2==0)&&(h3==1)&&(h4==1))
{right1();}
else if((h1==0)&&(h2==0)&&(h3==0)&&(h4==1))
{right1();}
else if((h1==1)&&(h2==0)&&(h3==1)&&(h4==1))
{run1();}
else if((h1==1)&&(h2==1)&&(h3==0)&&(h4==0))
{left1();}
else if((h1==1)&&(h2==0)&&(h3==0)&&(h4==0))
{left1();}
else if((h1==1)&&(h2==1)&&(h3==1)&&(h4==0))
{left1();}
}
void PWM_out_right(void)     //右PWM
{
if(PWM_moto_right<=right_moto)
  {EN1=1;EN4=1;}
else {EN1=0;EN4=0;}
if(PWM_moto_right>=20)
PWM_moto_right=0;
}
void PWM_out_left(void)     //左PWM
{
if(PWM_moto_left<=left_moto)
  {EN2=1;EN3=1;}
else {EN2=0;EN3=0;}
if(PWM_moto_left>=20)
PWM_moto_left=0;
}

void main(void)
{
TIM0init();
Timer2Init();
UartInit();
while(1)
{   
  switch(temp)
  {
   case '0':csb();send(temp);DelayMs(1000);break;
   case '1':run2();;send(temp);DelayMs(1000);break;
   case '2':run3();send(temp);DelayMs(1000);break;
   case '3':back1();send(temp);DelayMs(1000);break;
   case '4':back2();;send(temp);DelayMs(1000);break;
   case '5':right1();send(temp);DelayMs(1000);break;
   case '6':stop();send(temp);DelayMs(1000);break;
   case '7':left1();send(temp);DelayMs(1000);break;
   case '8':left2();send(temp);DelayMs(1000);break;
      case '9':Track();send(temp);DelayMs(1000);break;
  }   
}
}
void TIM0init(void)   //定时器0
{
        
    TMOD = 0x01;//定时器0工作方式1
    TH0=0x00;   
    TL0=0x00;
       ET0=1;
       TF0=0;
       EA=1;
}
void Timer0_isr(void) interrupt 1  //定时器0子程序
{
      
      ECHO=0;
}

void Timer2Init(void)  //定时器2
{

  RCAP2H=(0xFFFF-100)/256;
RCAP2L=(0xFFFF-100)%256;
TH2=RCAP2H;
TL2=RCAP2L;
T2CON=0;
EA=1;
ET2=1;
TR2=1;

}
void Timer2_isr(void) interrupt 5
{
  TF2=0;
  
++PWM_moto_right;
++PWM_moto_left;
PWM_out_right();
PWM_out_left();
}
void UartInit(void)   //串口通讯+定时器1   9600bps 11.0592MHz
{
SCON = 0x50;  //8位数据,可变波特率
TMOD = 0x20;
PCON &= 0x7f;
TH1=TL1=0xFD;
TI=0;
RI=0;
ES=1;
ET1=0;
TR1 = 1;  //启动定时器1
}
void ckinit(void) interrupt 4
{
if(RI==1)
{
  RI=0;
  temp=SBUF;
}
}
void send(uchar a)
{
TI=0;
SBUF=a;
while(TI==0);
TI=0;
}



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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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