久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

標(biāo)題: 為什么我單片機(jī)程序的超聲波不能檢測(cè)?其它的功能是好的 求幫助 [打印本頁(yè)]

作者: 李宏輝    時(shí)間: 2019-7-4 16:12
標(biāo)題: 為什么我單片機(jī)程序的超聲波不能檢測(cè)?其它的功能是好的 求幫助
#include "reg52.h"
#include "delay.h"
#define right_moto_go    {IN2=1,IN3=0,IN4=1,IN5=0;}  //右前電機(jī)
#define left_moto_go     {IN0=1,IN1=0,IN6=1,IN7=0;}  //左前電機(jī)
#define left_moto_back   {IN2=0,IN3=1,IN4=0,IN5=1;}  //右后電機(jī)
#define right_moto_back  {IN0=0,IN1=1,IN6=0,IN7=1;}  //左后電機(jī)
#define right_moto_stop  {IN2=0,IN3=0,IN4=0,IN5=0;}  //左電機(jī)停止
#define left_moto_stop   {IN0=0,IN1=0,IN6=0,IN7=0;}  //右電機(jī)停止

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);     //定時(shí)器0
void Timer2Init(void);  //定時(shí)器2
void UartInit(void);    //串口通訊
void send(uchar a);

void run0(void) //前進(jìn)0
{
left_moto=10;
right_moto=10;
right_moto_go;
left_moto_go;
}
void run1(void)   //前進(jìn)1
{
left_moto=6;
right_moto=6;
right_moto_go;
left_moto_go;
}
void run3(void)  //前進(jìn)3
{
left_moto=15;
right_moto=15;
right_moto_go;
left_moto_go;
}
void run2(void)   //前進(jìn)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)   //左轉(zhuǎn)1
{
left_moto=15;
right_moto=15;
right_moto_stop;
left_moto_go;
}
void left2(void)   //左轉(zhuǎn)2
{
left_moto=15;
right_moto=15;
right_moto_go;
left_moto_stop;
}
void right1(void)   //右轉(zhuǎn)1
{
left_moto=15;
right_moto=15;
right_moto_go;
left_moto_stop;
}
void right2(void)  //右轉(zhuǎn)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);       //觸發(fā)信號(hào)是高電平脈沖,寬度大于10us
    TRIG=0;
    while(!ECHO);      //等待高電平
    TR0=1;
    while(ECHO);       //等待低電平
    TR0=0;
    S=TH0*256+TL0;
    S=S/58;      
      
    TH0=0;
    TL0=0;            //清除定時(shí)器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)   //定時(shí)器0
{
        
    TMOD = 0x01;//定時(shí)器0工作方式1
    TH0=0x00;   
    TL0=0x00;
       ET0=1;
       TF0=0;
       EA=1;
}
void Timer0_isr(void) interrupt 1  //定時(shí)器0子程序
{
      
      ECHO=0;
}

void Timer2Init(void)  //定時(shí)器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)   //串口通訊+定時(shí)器1   9600bps 11.0592MHz
{
SCON = 0x50;  //8位數(shù)據(jù),可變波特率
TMOD = 0x20;
PCON &= 0x7f;
TH1=TL1=0xFD;
TI=0;
RI=0;
ES=1;
ET1=0;
TR1 = 1;  //啟動(dòng)定時(shí)器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;
}








歡迎光臨 (http://www.zg4o1577.cn/bbs/) Powered by Discuz! X3.1
主站蜘蛛池模板: 99re在线视频 | 在线免费观看a级片 | 午夜久久久久久久久久一区二区 | 99热国产在线播放 | 日韩欧美在线视频 | 国产日韩欧美在线一区 | 欧美久久一级 | 久久久久久免费毛片精品 | 91久久| 一级网站 | 日韩一区在线视频 | 正在播放亚洲 | 美女天堂在线 | 亚洲精品国产a久久久久久 中文字幕一区二区三区四区五区 | www.日韩| 在线午夜 | 怡红院怡春院一级毛片 | 欧美日韩在线免费观看 | 日韩精品一区二区三区四区 | 国产操操操 | 日韩免费视频一区二区 | 日韩精品1区2区3区 成人黄页在线观看 | 亚洲精品久久久久久久久久久 | 天天色天天色 | www.日韩 | 欧美日韩一 | 亚洲免费人成在线视频观看 | 日本精品一区二区三区在线观看 | 成人在线播放 | 99久久亚洲| 国产精品久久久久久久7电影 | 久久精品国产a三级三级三级 | 青青草一区二区三区 | 国产精品国产精品 | 亚洲精品2 | 久久综合激情 | 毛片入口 | 久久精品久久久 | 亚洲一区在线免费观看 | 国产在线永久免费 | 国产精品久久精品 |