标题: stm32循迹源码 [打印本页]

作者: 拼命的疯子    时间: 2017-8-9 09:35
标题: stm32循迹源码
  1. #include <includes.h>
  2. //int x=4,z=0,v=0;
  3. //int flag=0;

  4. /******************************************
  5. *                                                                                                   
  6. *             延时程序 ms
  7. *
  8. ****************************************/

  9. void delay(unsigned int n)
  10. {
  11.         unsigned int y, k;
  12.         
  13.         while(n--)
  14.         {                                          
  15.                 for(y = 0; y < 160; y++)
  16.                 {
  17.                         for(k = 0; k < 76; k++)
  18.                         {
  19.                                 ;
  20.                         }
  21.                 }
  22.         }        
  23. }
  24. /**************************************************************************************************************************************
  25. *                                                                                                   
  26. *                                                           左拐弯 右拐弯
  27. *

  28. **************************************************************************************************************************************/

  29. void rightbig ()                 //右拐
  30. {
  31.         GPIO_ResetBits(GPIOB, GPIO_Pin_11);
  32.         TIM2->CCR3=50;       //PA^2        //左前  

  33.         GPIO_ResetBits(GPIOB, GPIO_Pin_10 );
  34.         TIM2->CCR2=10;                 //PA^0        //右前

  35.         GPIO_ResetBits(GPIOA, GPIO_Pin_5 );
  36.         TIM2->CCR4=50;                 //PA^3        //左后

  37.         GPIO_ResetBits(GPIOA, GPIO_Pin_4 );
  38.         TIM2->CCR1=10;                 //PA^1        //右后
  39. }


  40. void leftbig()                //左拐
  41. {
  42.         GPIO_ResetBits(GPIOB, GPIO_Pin_11);
  43.         TIM2->CCR3=10;       //PA^2        //左前

  44.         GPIO_ResetBits(GPIOB, GPIO_Pin_10 );
  45.         TIM2->CCR2=50;                 //PA^0        //右前

  46.         GPIO_ResetBits(GPIOA, GPIO_Pin_5 );
  47.         TIM2->CCR4=10;                 //PA^3        //左后

  48.         GPIO_ResetBits(GPIOA, GPIO_Pin_4 );
  49.         TIM2->CCR1=50;                 //PA^1        //右后

  50. }
  51. /**************************************************************************************************************************************
  52. *                                                                                                   
  53. *                                                                          停止
  54. *
  55. **************************************************************************************************************************************/
  56. //void stop()
  57. //{
  58. //        GPIO_ResetBits(GPIOA ,GPIO_Pin_0 );
  59. //        GPIO_ResetBits(GPIOA ,GPIO_Pin_1 );
  60. //        GPIO_ResetBits(GPIOB ,GPIO_Pin_2 );
  61. //        GPIO_ResetBits(GPIOB ,GPIO_Pin_3 );

  62. //}
  63. /**************************************************************************************************************************************
  64. *                                                                                                   
  65. *                                                           左右轮子分别反向
  66. *
  67. **************************************************************************************************************************************/
  68. void  leftgorightback()
  69. {
  70.         
  71.                 GPIO_ResetBits(GPIOB, GPIO_Pin_11);
  72.                 TIM2->CCR3=80;                                       
  73.         
  74.                 GPIO_ResetBits(GPIOA, GPIO_Pin_5 );
  75.                 TIM2->CCR4=80;                             //左前,左后向前走
  76.                
  77.                 GPIO_SetBits(GPIOB, GPIO_Pin_10 );
  78.                 TIM2->CCR2=80;        
  79.         
  80.             GPIO_SetBits(GPIOA, GPIO_Pin_4 );
  81.                 TIM2->CCR1=80;                                         //右前,右后向后走
  82. }

  83. void  leftbackrightgo()
  84. {
  85.                 GPIO_SetBits(GPIOB, GPIO_Pin_11);
  86.                 TIM2->CCR3=80;                                       
  87.         
  88.                 GPIO_SetBits(GPIOA, GPIO_Pin_5 );
  89.                 TIM2->CCR4=80;                             //左前,左后向后走
  90.                
  91.                 GPIO_ResetBits(GPIOB, GPIO_Pin_10 );
  92.                 TIM2->CCR2=80;        
  93.         
  94.             GPIO_ResetBits(GPIOA, GPIO_Pin_4 );
  95.                 TIM2->CCR1=80;                                         //右前,右后向左走
  96. }

  97. /**************************************************************************************************************************************
  98. *                                                                                                   
  99. *                                                           循迹函数
  100. *
  101. **************************************************************************************************************************************/
  102. void xunji()

  103. {
  104.         if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12)==0&&GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_13)==1)//右边没有检测到相当于,右拐
  105.         {
  106.                 TIM2->CCR1 =10;        
  107.                 TIM2->CCR2 =10;        
  108.                 TIM2->CCR3 =99;        
  109.                 TIM2->CCR4 =99;                //右拐
  110.         }
  111.         
  112.         if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12)==1&&GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_13)==0)//右边没检测到,左边黑线,左拐
  113.         {
  114.                 TIM2->CCR3 =10;        
  115.                 TIM2->CCR4 =10;        
  116.                 TIM2->CCR1 =99;        
  117.                 TIM2->CCR2 =99;                //左拐   
  118.         
  119.         }        
  120.         
  121.         if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12)==0&&GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_13)==0)//都有,直走
  122.         {
  123.                 TIM2->CCR1 =50;        
  124.                 TIM2->CCR2 =50;        
  125.                 TIM2->CCR3 =50;        
  126.                 TIM2->CCR4 =50;                //直走
  127.                
  128.         }
  129.         
  130.         
  131. }
  132. /**************************************************************************************************************************************
  133. *                                                                                                   
  134. *                                                           走到交接处函数
  135. *
  136. **************************************************************************************************************************************/
  137. void begin()
  138. {
  139.         if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_2)==1)
  140.         {
  141.                 TIM2->CCR1 =10;        
  142.                 TIM2->CCR2 =10;        
  143.                 TIM2->CCR3 =99;        
  144.                 TIM2->CCR4 =99;                //右拐
  145.                
  146.                 delay (10);
  147.         }        

  148. }

  149. void while_1()
  150. {
  151. //        rightbig ();
  152. //        leftbig ();
  153. //        leftgorightback();
  154.         leftbackrightgo();
  155. //        xunji ();        
  156. }

复制代码





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