标题: 巡线小车程序分享 [打印本页]

作者: 骁勇阿凯    时间: 2018-9-9 11:50
标题: 巡线小车程序分享
#include <REGX52.H> //°üo¬51μ¥Ƭ»úÏà1ØμÄí·Îļt

sbit LeftLed=P2^0;                        
sbit RightLed=P0^7;                       
sbit FontLled=P1^0;

sbit LeftIR=P3^5;                        
sbit RightIR=P3^6;                        
sbit FontIR=P3^7;                        

sbit M1A=P0^0;                           
sbit M1B=P0^1;                           

sbit M2A=P0^2;                           
sbit M2B=P0^3;                           

sbit B1=P0^4;                             
sbit SB1=P0^6;                           


void tingzhi()
{
   M1A=0;                                   
   M1B=0;                                   
   M2A=0;                                   
   M2B=0;

}

void qianjin()
{
   M1A=1;                                   
   M1B=0;                                   
   M2A=1;                                   
   M2B=0;
}

void houtui()
{
   M1A=0;                                   
   M1B=1;                                   
   M2A=0;                                   
   M2B=1;
}

void zuozhuan()
{
   M1A=0;                                   
   M1B=1;                                   
   M2A=1;                                   
   M2B=0;
}

void youzhuan()
{
   M1A=1;                                   
   M1B=0;                                   
   M2A=0;                                   
   M2B=1;
}

void delay_nus(unsigned int i)  
{
  i=i/10;
  while(--i);
}   
void delay_nms(unsigned int n)  
{
  n=n+1;
  while(--n)  
  delay_nus(900);         

}  


void ControlCar(unsigned char ConType)   
{

  tingzhi();
switch(ConType)                          
{
  case 1:  //ǰ½ø                        
  {
    qianjin();
    break;
  }
  case 2: //oóíË                          
  {
    houtui();                                
    break;
  }
  case 3: //×ó×a                          
  {
    zuozhuan();                              
        break;
  }
  case 4: //óò×a                          
  {
    youzhuan();                                

        break;
  }
  case 8: //í£Ö1                          
  {
    tingzhi();
        break;                                
  }
}
}
void main()                              
{
  bit RunFlag=0;                          
  //RunShow=0;                              

  ControlCar(8);                          
  while(1)                                
  {
   Start:

   LeftLed=LeftIR;                        
   RightLed=RightIR;                     
   FontLled= FontIR;
   SB1=LeftIR;
   if(LeftIR == 0 && RightIR == 0)   
   {
      ControlCar(1);                     
      delay_nms (10);
          goto NextRun;
   }

   if(LeftIR == 0 && RightIR == 1)
   {
      ControlCar(3);                     
      delay_nms (10);
          goto NextRun;
   }

   if(LeftIR == 1 && RightIR == 0)
   {
      ControlCar(4);                     
      delay_nms (10);
          goto NextRun;
   }


   goto Start;
   NextRun:
   ControlCar(8);
  }
}


原理图.png (186.48 KB, 下载次数: 44)

原理图.png

作者: niim    时间: 2018-9-14 13:50
感谢分享
作者: 我爱罗是是是    时间: 2018-10-12 14:56
请问这个适用于四路红外感应吗?超萌新提问
作者: 骁勇阿凯    时间: 2019-10-23 17:27
我爱罗是是是 发表于 2018-10-12 14:56
请问这个适用于四路红外感应吗?超萌新提问

适用,原理是一样的,只是更改相应的引脚而已。




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1