找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 7062|回復(fù): 3
收起左側(cè)

今天調(diào)試的智能小車走Z字路徑成功,分享一下!

[復(fù)制鏈接]
ID:202991 發(fā)表于 2017-5-30 23:28 | 顯示全部樓層 |閱讀模式

智能小車_Z字路徑調(diào)試

智能小車_Z字路徑調(diào)試

程序如下:
//主程序文件1  main.c://


        #include<REG52.H>                  //包含51單片機頭文件,內(nèi)部有各種寄存器定義
        #include<PWM.H>                  //包含HL-1藍牙智能小車驅(qū)動IO口定義等函數(shù)
        #include<intrins.H>                  //包含HL-1藍牙智能小車驅(qū)動IO口定義等函數(shù)

unsigned int i;


void Delay1ms()                //@11.0592MHz
{
        unsigned char i, j;

        _nop_();
        i = 2;
        j = 199;
        do
        {
                while (--j);
        } while (--i);
}

void Delay100ms()                //@11.0592MHz
{
        unsigned char i, j;

        i = 180;
        j = 73;
        do
        {
                while (--j);
        } while (--i);
}

void Delay500ms()                //@11.0592MHz
{
        unsigned char i, j, k;

        _nop_();
        i = 4;
        j = 129;
        k = 119;
        do
        {
                do
                {
                        while (--k);
                } while (--j);
        } while (--i);
}


        void main(void)
{        

    P1=0X00;   //關(guān)電機        

        TMOD &= 0xF0;                //設(shè)置定時器模式
        TMOD |= 0x01;                //設(shè)置定時器模式
        TL0 = 0x66;                //設(shè)置定時初值
        TH0 = 0xFC;                //設(shè)置定時初值
                   TR0= 1;
                ET0= 1;
                EA = 1;
                                                   //開總中斷
B:                for(i=0;i<10;i++) //判斷K3是否按下
                {
                   Delay1ms();        //1ms內(nèi)判斷50次,如果其中有一次被判斷到K3沒按下,便重新檢測
                   if(anniu!=0 )//當K3按下時,啟動小車
                   goto B; //跳轉(zhuǎn)到標號B,重新檢測
                }
        //蜂鳴器響一聲
        BUZZ=0;        //50次檢測K3確認是按下之后,蜂鳴器發(fā)出“滴”聲響,然后啟動小車。
        Delay500ms();
        BUZZ=1;//響50ms后關(guān)閉蜂鳴器

        while(1)        //無限循環(huán)
        {
         
                         //有信號為0  沒有信號為1

              if(Left_1_led==0&&Right_1_led==0&&Left_2_led==1&&Right_2_led==1)

                          run();   //調(diào)用前進函數(shù)

                          else
                         {                          
                                               if(Left_1_led==1&&Right_1_led==0)            //左邊檢測到黑線
                                  {
                                           backrun();
                                          Delay100ms();
                                          Delay100ms();
                                      rightrun();                   //調(diào)用小車右轉(zhuǎn)        函數(shù)
                                          Delay100ms();

//                                           leftrun();                  //調(diào)用小車左轉(zhuǎn)  函數(shù)

                             }
               
                                               if(Left_2_led==1&&Right_2_led==0)            //左邊檢測到黑線
                                  {
                                           backrun();
                                          Delay100ms();
                                          Delay100ms();
                                           leftrun();                  //調(diào)用小車左轉(zhuǎn)  函數(shù)
                                          Delay100ms();
                                          Delay100ms();
                                          Delay100ms();
                                          Delay100ms();
//                                           leftrun();                  //調(diào)用小車左轉(zhuǎn)  函數(shù)
//                                      rightrun();                   //調(diào)用小車右轉(zhuǎn)        函數(shù)
                             }
                           
                                                             if(Right_1_led==1&&Left_1_led==0)                //右邊檢測到黑線
                                  {         
//                                      rightrun();                   //調(diào)用小車右轉(zhuǎn)        函數(shù)
                                           backrun();
                                          Delay100ms();
                                          Delay100ms();
                                           leftrun();                  //調(diào)用小車左轉(zhuǎn)  函數(shù)
                                          Delay100ms();
                                  }
                                
                                                             if(Right_2_led==1&&Left_2_led==0)                //右邊檢測到黑線
                                  {         
                                           backrun();
                                          Delay100ms();
                                          Delay100ms();
                                      rightrun();                   //調(diào)用小車右轉(zhuǎn)        函數(shù)
                                          Delay100ms();
                                          Delay100ms();
                                          Delay100ms();
                                          Delay100ms();
//                                           leftrun();                  //調(diào)用小車左轉(zhuǎn)  函數(shù)
//                                      rightrun();                   //調(diào)用小車右轉(zhuǎn)        函數(shù)
                                  }
                                
                                
                                
                                
                                                    if(Right_1_led==1&&Left_1_led==1)                //懸空狀態(tài)  避懸崖
                                  {         
                                      stop();                   //調(diào)用小車停止

                                  }

                        }         
         }
}






//調(diào)速文件2  pwm.h//

#ifndef __PWM_H_
#define __PWM_H_

    //定義小車驅(qū)動模塊輸入IO口
   sbit L293D_IN1=P1^2;
   sbit L293D_IN2=P1^3;
   sbit L293D_IN3=P1^4;
   sbit L293D_IN4=P1^5;
        

        /***蜂鳴器接線定義*****/
    sbit BUZZ=P2^3;
         sbit anniu=P3^2;

    sbit Left_1_led=P3^7;         //左傳感器  
        
    sbit Right_1_led=P3^6;         //右傳感器
        
    sbit Left_2_led=P3^4;         //左傳感器  
        
    sbit Right_2_led=P3^5;         //右傳感器   
           

        sbit Left_moto_pwm=P1^6;         //PWM信號端

        sbit Right_moto_pwm=P1^7;         //PWM信號端

        #define Left_moto_Stop    {P1_2=0,P1_3=0;}         //左邊電機停轉(zhuǎn)                     
        #define Right_moto_Stop   {P1_4=0,P1_5=0;}              //右邊電機停轉(zhuǎn)

        unsigned char pwm_val_left  =0;//變量定義
        unsigned char push_val_left =0;// 左電機占空比N/20
        unsigned char pwm_val_right =0;
        unsigned char push_val_right=0;// 右電機占空比N/20
        bit Right_moto_stop=1;
        bit Left_moto_stop =1;

/************************************************************************/
//前速前進
     void  run(void)
{
     push_val_left=5;         //速度調(diào)節(jié)變量 0-20。。。0最小,20最大
         push_val_right=5;

   L293D_IN1=1;
   L293D_IN2=0;
   L293D_IN3=1;
   L293D_IN4=0;
}

//后退函數(shù)
     void  backrun(void)
{
     push_val_left=10;         //速度調(diào)節(jié)變量 0-20。。。0最小,20最大
         push_val_right=10;

   L293D_IN1=0;
   L293D_IN2=1;
   L293D_IN3=0;
   L293D_IN4=1;
}

//左轉(zhuǎn)
     void  leftrun(void)
{         
     push_val_left=7;
         push_val_right=20;

   L293D_IN1=0;
   L293D_IN2=1;
   L293D_IN3=1;
   L293D_IN4=0;
}

//右轉(zhuǎn)
     void  rightrun(void)
{
         push_val_left=20;
         push_val_right=7;

   L293D_IN1=1;
   L293D_IN2=0;
   L293D_IN3=0;
   L293D_IN4=1;
}

//停止
     void  stop(void)
{         


   L293D_IN1=0;
   L293D_IN2=0;
   L293D_IN3=0;
   L293D_IN4=0;
}

/************************************************************************/
/*                    PWM調(diào)制電機轉(zhuǎn)速                                   */
/************************************************************************/
/*                    左電機調(diào)速                                        */
/*調(diào)節(jié)push_val_left的值改變電機轉(zhuǎn)速,占空比            */
                void pwm_out_left_moto(void)
{  
   if(Left_moto_stop)
   {
    if(pwm_val_left<=push_val_left)
               {
                     Left_moto_pwm=1;
                   }
        else
               {
                 Left_moto_pwm=0;
                   }
        if(pwm_val_left>=20)
               pwm_val_left=0;
   }
   else   
          {
           Left_moto_pwm=0;
                  }
}
/******************************************************************/
/*                    右電機調(diào)速                                  */  
   void pwm_out_right_moto(void)
{
  if(Right_moto_stop)
   {
    if(pwm_val_right<=push_val_right)
              {
               Right_moto_pwm=1;
                   }
        else
              {
                   Right_moto_pwm=0;
                  }
        if(pwm_val_right>=20)
               pwm_val_right=0;
   }
   else   
          {
           Right_moto_pwm=0;
              }
}

/***************************************************/
///*TIMER0中斷服務(wù)子函數(shù)產(chǎn)生PWM信號*/
         void Timer0Init() interrupt 1  
{
        TMOD &= 0xF0;                //設(shè)置定時器模式
        TMOD |= 0x01;                //設(shè)置定時器模式
        TL0 = 0x66;                //設(shè)置定時初值
        TH0 = 0xFC;                //設(shè)置定時初值
         pwm_val_left++;
         pwm_val_right++;
         pwm_out_left_moto();
         pwm_out_right_moto();
}        

/*********************************************************************/        
//void Timer0Init(void)                //1毫秒@11.0592MHz
//{
//        AUXR &= 0x7F;                //定時器時鐘12T模式
//        TMOD &= 0xF0;                //設(shè)置定時器模式
//        TMOD |= 0x01;                //設(shè)置定時器模式
//        TL0 = 0x66;                //設(shè)置定時初值
//        TH0 = 0xFC;                //設(shè)置定時初值
//        TF0 = 0;                //清除TF0標志
//        TR0 = 1;                //定時器0開始計時
//}

#endif

資料下載地址:
2.傳感器底板.pdf (67.53 KB, 下載次數(shù): 11)
1.主板原理圖.pdf (105.15 KB, 下載次數(shù): 6)

回復(fù)

使用道具 舉報

ID:1 發(fā)表于 2017-6-2 02:57 | 顯示全部樓層
謝謝分享 一個很經(jīng)典的小車程序,51黑有你更精彩!!!
回復(fù)

使用道具 舉報

ID:169021 發(fā)表于 2017-6-3 01:56 | 顯示全部樓層
走Z字路徑還是很難搞的 不知道樓主這個效果怎么樣
回復(fù)

使用道具 舉報

ID:202991 發(fā)表于 2017-6-3 23:49 來自手機 | 顯示全部樓層
51hei小小 發(fā)表于 2017-6-3 01:56
走Z字路徑還是很難搞的 不知道樓主這個效果怎么樣

走的很好!
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復(fù) 返回頂部 返回列表