找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2006|回复: 3
收起左侧

巡线小车程序分享

[复制链接]
ID:322248 发表于 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

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:396331 发表于 2018-9-14 13:50 | 显示全部楼层
感谢分享
回复

使用道具 举报

ID:408592 发表于 2018-10-12 14:56 | 显示全部楼层
请问这个适用于四路红外感应吗?超萌新提问
回复

使用道具 举报

ID:322248 发表于 2019-10-23 17:27 | 显示全部楼层
我爱罗是是是 发表于 2018-10-12 14:56
请问这个适用于四路红外感应吗?超萌新提问

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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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