fork download
  1. /*
  2.   Stepper motor state machine - pc test code
  3. */
  4.  
  5. #include <stdio.h>
  6. #include "binary.h"
  7. #include "itob.h"
  8.  
  9. #define setForward(m) (((m)->State) &= 0x03)
  10. #define setReverse(m) (((m)->State) |= 0x04)
  11.  
  12. typedef struct stepper4ph_t {
  13. unsigned State :4;
  14. } stepper4ph ;
  15.  
  16.  
  17.  
  18. unsigned char stateTable[] = {
  19. (3)+(b0101<<4),
  20. (0)+(b0110<<4),
  21. (1)+(b1010<<4),
  22. (2)+(b1001<<4),
  23. (5)+(b0001<<4),
  24. (6)+(b0010<<4),
  25. (7)+(b0100<<4),
  26. (4)+(b1000<<4)
  27. };
  28.  
  29.  
  30. unsigned char stepmotor (stepper4ph * motor) ;
  31.  
  32.  
  33. int main(void) {
  34.  
  35. stepper4ph motor;
  36. int i;
  37.  
  38. motor.State = 0;
  39. setForward(&motor);
  40.  
  41. for (i = 0;i < 30; i++) {
  42.  
  43. if (i > 12) setReverse(&motor);
  44.  
  45. printf("%s\n", itob4[stepmotor(&motor)]);
  46. }
  47.  
  48. return 0;
  49. }
  50.  
  51.  
  52.  
  53.  
  54. unsigned char stepmotor (stepper4ph * motor) {
  55. unsigned char index;
  56.  
  57. index = stateTable[motor->State];
  58.  
  59. motor->State = (index & 0x0F); // calc next state
  60. return (index & 0xF0) >> 4; // calc motor bits
  61.  
  62. }
  63.  
  64.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty