Advertisement
Guest User

Untitled

a guest
Jun 30th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.43 KB | None | 0 0
  1. #include "main.h"
  2. #include "motors.h"
  3. #include "hc595.h"
  4.  
  5. //typedef uint8_t bool;
  6. #define true 1
  7. #define false 0
  8.  
  9.  
  10. static volatile int8_t stop=1;
  11. static uint32_t delay=10000/2;
  12. static uint8_t motor_number=1;
  13.  
  14. bool step=false;
  15.  
  16.  
  17. void motor_select(MotorType type)
  18. {
  19.     motor_number=type;
  20. }
  21. void motor_mode12(void)
  22. {
  23.     hc_outbits((hc_getbits()&(~(MOTOR_M0|MOTOR_M1|MOTOR_M2)))|MOTOR_M0);
  24. }
  25. void motor_mode14(void)
  26. {
  27.     hc_outbits((hc_getbits()&(~(MOTOR_M0|MOTOR_M1|MOTOR_M2)))|MOTOR_M1);
  28. }
  29. void motor_mode18(void)
  30. {
  31.     hc_outbits((hc_getbits()&(~(MOTOR_M0|MOTOR_M1|MOTOR_M2)))|MOTOR_M0|MOTOR_M1);
  32. }
  33. void motor_mode116(void)
  34. {
  35.     hc_outbits((hc_getbits()&(~(MOTOR_M0|MOTOR_M1|MOTOR_M2)))|MOTOR_M2);
  36. }
  37. void motor_mode132(void)
  38. {
  39.     hc_outbits((hc_getbits()&(~(MOTOR_M0|MOTOR_M1|MOTOR_M2)))|MOTOR_M0|MOTOR_M2);
  40. }
  41. void motor_modegfull(void)
  42. {
  43.     hc_outbits((hc_getbits()&(~(MOTOR_M0|MOTOR_M1|MOTOR_M2))));
  44. }
  45. void motor_left(void)
  46. {
  47.     stop=1;
  48.     if(motor_number==1)
  49.     {
  50.         hc_setbits(MOTOR1_DIR);
  51.     }else if(motor_number==2)
  52.     {
  53.         hc_setbits(MOTOR2_DIR);
  54.     }if(motor_number==3)
  55.     {
  56.         hc_setbits(MOTOR3_DIR);
  57.     }
  58. }
  59. void motor_right(void)
  60. {
  61.     stop=1;
  62.     if(motor_number==1)
  63.     {
  64.         hc_clearbits(MOTOR1_DIR);
  65.     }else
  66.         if(motor_number==2)
  67.         {
  68.             hc_clearbits(MOTOR2_DIR);
  69.         }else  if(motor_number==3)
  70.         {
  71.             hc_clearbits(MOTOR3_DIR);
  72.         }
  73. }
  74.  
  75. void motor_step(void)
  76. {
  77.     if(motor_number==1)
  78.     {
  79.         hc_tooglebits(MOTOR1_STEP);
  80.     }else
  81.         if(motor_number==2)
  82.         {
  83.             hc_tooglebits(MOTOR2_STEP);
  84.         }
  85.         else
  86.             if(motor_number==3)
  87.             {
  88.                 hc_tooglebits(MOTOR3_STEP);
  89.             }
  90.    
  91. }
  92.  
  93. void motor_control(MotorType type, MotorDirection direction, MotorMode mode, uint64_t steps, uint32_t speed)
  94. {
  95.     uint32_t timestamp=0;
  96.     uint32_t step_time=0;
  97.    
  98.     switch (type)
  99.     {
  100.         case MOTOR_ROTATION:
  101.             motor_select(MOTOR_ROTATION);
  102.             break;
  103.         case MOTOR_CAMERA:
  104.             motor_select(MOTOR_CAMERA);
  105.             break;
  106.         case MOTOR_PLATFORM:
  107.             motor_select(MOTOR_PLATFORM);
  108.             break;
  109.            
  110.         default:
  111.             break;
  112.     }
  113.    
  114.     switch (direction)
  115.     {
  116.         case LEFT:
  117.             motor_left();
  118.             break;
  119.         case RIGHT:
  120.             motor_right();
  121.             break;
  122.            
  123.         default:
  124.             break;
  125.     }
  126.    
  127.     switch (mode)
  128.     {
  129.         case MODE_FULL:
  130.             motor_modegfull();
  131.             break;
  132.         case MODE_2:
  133.             motor_mode12();
  134.             break;
  135.         case MODE_4:
  136.             motor_mode14();
  137.             break;
  138.         case MODE_8:
  139.             motor_mode18();
  140.             break;
  141.         case MODE_16:
  142.             motor_mode116();
  143.             break;
  144.         case MODE_32:
  145.             motor_mode132();
  146.             break;
  147.            
  148.         default:
  149.             break;
  150.     }
  151.     workLED(1,1);
  152.     for(int i=0; i<steps; i++)
  153.     {
  154.         timestamp++;
  155.        
  156.         if((timestamp-step_time)>speed)
  157.         {
  158.             motor_step();
  159.             step_time=timestamp;
  160.             if (BTN_START_STOP && BTN_OPTIONS_BACK)
  161.             {
  162.                 workLED(0,1);
  163.                 return 0;
  164.             }
  165.         }
  166.     }
  167.     workLED(0,1);
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement