找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 885|回复: 1
收起左侧

这个程序 过红外循迹直道弯会冲出去 ,有能帮忙解答解答的吗

[复制链接]
ID:362894 发表于 2018-7-2 09:53 | 显示全部楼层 |阅读模式
  1. #include<reg52.h>
  2. #include<pwmkl.h>

  3. unsigned int count,i,j;
  4. unsigned int n;
  5. void delay(int xms)
  6. {
  7. for(i=1000;i--;i>0)
  8. for(j=xms;j--;j>0);
  9. }

  10. void xunji()
  11. {
  12. if((leftxled==0)&&(rightxled==0)/*&&(leftxled1==0)&&(rightxled1==0)*/)         //向前走
  13.          {
  14.           n=0;

  15.          }
  16.             else if((leftxled==1)&&(rightxled==0)/*&&(leftxled1==0)&&(rightxled1==0)*/) //左1
  17.          {

  18.           n=3;

  19.          }
  20.             /* else if((leftxled==0)&&(rightxled==1)&&(leftxled1==0)&&(rightxled1==0)) //左2
  21.          {
  22.           n=2;

  23.          }*/
  24.             else if((leftxled==0)&&(rightxled==1)/*&&(leftxled1==0)&&(rightxled1==0)*/)//右1
  25.          {
  26.           n=1;

  27.          }

  28.             /*else if((leftxled==0)&&(rightxled==0)&&(leftxled1==0)&&(rightxled1==1))//右2
  29.          {
  30.           n=4;

  31.          }*/
  32.          else if((leftxled==1)&&(rightxled==1)/*&&(leftxled1==1)&&(rightxled1==1)*/)//停
  33.          {
  34.           n=5;

  35.          }



  36.    switch(n)
  37.           {
  38.                   case(0):run();;break;
  39.                 case(1):leftrun();break;
  40.                 case(2):rightrun();break;
  41.                 case(3):rightrun();break;
  42.                 case(4):rightrun();break;
  43.                 case(5):stop();break;
  44.                 default:break;
  45.           }
  46. }
  47. void main()
  48. {
  49.    TMOD=0x01;
  50.    TH0=(65536-1000)/256;
  51.    TL0=(65536-1000)%256;
  52.    /*TH1=0xff;
  53.    TL1=0xa3; */

  54.    EA=1;
  55.    /*ET1=1;
  56.    TR1=1;*/

  57.    ET0=1;
  58.    TR0=1;

  59.    /*IT0=1;
  60.    EX0=1;*/
  61.   while(1)
  62.    {  
  63.                    xunji();
  64.    }
  65. }

  66. /*void Time1_Int() interrupt 3//
  67. {
  68.         TH1=0xff;
  69.         TL1=0xa3;
  70.         count=(count+1);//0.5ms次数加1
  71.         count=count%40;//次数始终保持为40即保持周期为20ms
  72. }*/
  73. /*中断*/
  74. void Int0() interrupt 0
  75. {
  76.                                   if(leftxled==0&&rightxled==1)//靠左边1检查到白,右边检查到黑
  77.                                 {
  78.                                  
  79.                                  leftrun();
  80.                                  delay(100);
  81.                                  stop();
  82.                                 }
  83.                                 if(leftxled1==0&&rightxled1==1)//靠左边2检查到白,右边检查到黑
  84.                                 {
  85.                                  
  86.                                  leftrun();
  87.                                   delay(100);
  88.                                  stop();
  89.                                 }
  90.                                 if(leftxled==1&&rightxled==0) //靠右边1检查到白,左边检查到黑
  91.                                 {
  92.                               
  93.                                  rightrun();
  94.                                   delay(100);
  95.                                  stop();
  96.                                 }
  97.                                 if(leftxled1==1&&rightxled1==0) //靠右边1检查到白,左边检查到黑
  98.                                 {
  99.                                  
  100.                                  rightrun();
  101.                                   delay(100);
  102.                                  stop();
  103.                                 }
  104. }
  105. /****************************************pwm  头文件***************************/
  106. #ifndef _LED_H_
  107. #define _LED_H_


  108. sbit IN1=P1^0;           //  右1电机         
  109. sbit IN2=P1^1;           //  右1电机   
  110. sbit IN3=P1^2;           //  左1电机   
  111. sbit IN4=P1^3;           //  左1电机     


  112. //循迹传感器定义 左P3^6,右P3^5。
  113. #define leftxled P3_6  //靠左边1循迹小灯
  114. #define rightxled P3_5                //右1
  115. #define leftxled1 P3_4        //靠左边2循迹小灯
  116. #define rightxled1 P3_7

  117. /*避障传感器定义 左P3^7,右P3^4。
  118. #define leftbled P3^7          //右边循迹小灯
  119. #define rightbled P3^4         */

  120. //定义pwm始能端
  121. #define leftpwm P1_4          //左边pwm 始能端
  122. #define rightpwm P1_5          //右边pwm 始能端

  123. //电机转向
  124. #define leftgo {IN1=1,IN2=0;}         //左边电机前进
  125. #define leftstop {IN1=0,IN2=0;}// 左  停
  126. #define rightgo {IN3=1,IN4=0;}         //右边电机前进
  127. #define rightstop {IN3=0,IN4=0;}// 右  停
  128. #define leftback {IN1=0,IN2=1;}         //左边电机后退
  129. #define rightback {IN3=0,IN4=1;}         //右边电机后退

  130. //定义pwm、占空比
  131. unsigned char pwmleft  =0;//变量定义
  132. unsigned char pwmright =0;
  133. unsigned char pushleft =0;//占空比N/10
  134. unsigned char pushright =0;
  135.         //定义固定变量
  136.     bit k=1;
  137.         bit kk=1;
  138.         unsigned  int  time=0;

  139. //前进函数
  140. void run()
  141. {
  142.    pushleft=4;
  143.    leftgo;
  144.    pushright=4;
  145.    rightgo;

  146. }
  147. //houtui

  148. void back()
  149. {
  150.   leftback;
  151.   rightback;
  152. }
  153. void stop()
  154. {
  155.    leftstop;
  156.    rightstop;
  157. }

  158. //左转函数
  159. void leftrun()
  160. {
  161.    pushleft=15;
  162.    leftgo;
  163.    pushright=12;
  164.    rightback;
  165. }

  166. //右转函数
  167. void rightrun()
  168. {
  169.    pushleft=12;
  170.    leftback;
  171.    pushright=15;
  172.    rightgo;
  173. }

  174. /******************pwm 调速************************************/
  175. //左电机进行调速
  176. void leftpwml()
  177. {
  178.   if(k)
  179.   {
  180.           if(pwmleft<=pushleft)
  181.         {
  182.            leftpwm=1;
  183.         }
  184.         else
  185.         {
  186.            leftpwm=0;
  187.         }
  188.         if(pwmleft>=20)
  189.         {
  190.            pwmleft=0;
  191.         }
  192.   }
  193.   else
  194.   {
  195.    leftpwm=1;
  196.   }
  197. }

  198. //右电机调速
  199. void rightpwmr()
  200. {
  201.   if(kk)
  202.   {
  203.           if(pwmright<=pushright)
  204.         {
  205.            rightpwm=1;
  206.         }
  207.         else
  208.         {
  209.            rightpwm=0;
  210.         }
  211.         if(pwmright>=20)
  212.         {
  213.            pwmright=0;
  214.         }
  215.   }
  216.   else
  217.   {
  218.      rightpwm=1;
  219.   }
  220. }
  221. //定时器0产生中断信号
  222. void time0() interrupt 1
  223. {
  224.     TH0=(65536-1000)/256;
  225.     TL0=(65536-1000)%256;
  226.         time++;
  227.         pwmleft++;
  228.         pwmright++;
  229.         leftpwml();
  230.         rightpwmr();
  231. }

  232. #endif


复制代码


回复

使用道具 举报

ID:362894 发表于 2018-7-2 09:54 | 显示全部楼层
这个程序 过红外循迹直道弯会冲出去 ,有能帮忙解答解答的吗
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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