Advertisement
stenogriz

L9110S with Arduino

Jun 22nd, 2015
1,530
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.57 KB | None | 0 0
  1. /*
  2.  
  3. Sergei A. Minayev, 2015
  4. L9110S Stepper motor driver test app
  5. Working app: http://youtu.be/OD8UAhJ7-Qg
  6.  
  7. */
  8.  
  9. // pin assignments for L9110S
  10. const int AIA = 4;
  11. const int AIB = 5;
  12. const int BIA = 2;
  13. const int BIB = 3;
  14.  
  15. // step controller definitions
  16. int state = 0; // current step for state machine
  17. int spd = 30; //ms step timing (speed)
  18. bool dir = true; // true - forward; false - backward
  19. bool mode = true; // true - half step; false - full step
  20.  
  21. // communication definitions
  22. char incoming_char;
  23.  
  24. //0000 - AIA, AIB, BIA, BIB
  25. void phase_out (byte a){
  26.   if (bitRead(a,0)) {digitalWrite(BIB, HIGH);} else {digitalWrite(BIB, LOW);}
  27.   if (bitRead(a,1)) {digitalWrite(BIA, HIGH);} else {digitalWrite(BIA, LOW);}
  28.   if (bitRead(a,2)) {digitalWrite(AIB, HIGH);} else {digitalWrite(AIB, LOW);}
  29.   if (bitRead(a,3)) {digitalWrite(AIA, HIGH);} else {digitalWrite(AIA, LOW);}
  30. }
  31.  
  32. void state_machine (void){
  33.    switch (state) {
  34.     case 0:
  35.       phase_out(0b00001000);
  36.       if (mode) {if (dir){state=1;} else {state = 7;}} else {if (dir){state=2;} else {state = 6;}}
  37.       break;
  38.     case 1:
  39.       phase_out(0b00001010);
  40.       if (dir){state=2;} else {state = 0;}
  41.       break;
  42.     case 2:
  43.       phase_out(0b00000010);
  44.       if (mode) {if (dir){state=3;} else {state = 1;}} else {if (dir){state=4;} else {state = 0;}}
  45.       break;
  46.     case 3:
  47.       phase_out(0b00000110);
  48.       if (dir){state=4;} else {state = 2;}
  49.       break;
  50.     case 4:
  51.       phase_out(0b00000100);
  52.       if (mode) {if (dir){state=5;} else {state = 3;}} else {if (dir){state=6;} else {state = 2;}}
  53.       break;
  54.     case 5:
  55.       phase_out(0b00000101);
  56.       if (dir){state=6;} else {state = 4;}
  57.       break;
  58.     case 6:
  59.       phase_out(0b00000001);
  60.       if (mode) {if (dir){state=7;} else {state = 5;}} else {if (dir){state=0;} else {state = 4;}}
  61.       break;
  62.     case 7:
  63.       phase_out(0b00001001);
  64.       if (dir){state=0;} else {state = 6;}
  65.       break;
  66.     default:
  67.       state = 0;
  68.   }
  69. }
  70.  
  71. void setup() {
  72.   pinMode(AIA, OUTPUT);
  73.   pinMode(AIB, OUTPUT);
  74.   pinMode(BIA, OUTPUT);
  75.   pinMode(BIB, OUTPUT);
  76.   Serial.begin(9600);
  77.   Serial.write("f - full step; h - half step; d - forward; r - backward");
  78. }
  79.  
  80. void loop() {
  81.   state_machine();
  82.   delay(spd);
  83.  
  84.   if (Serial.available() > 0) {
  85.     incoming_char = Serial.read();
  86.     switch (incoming_char) {
  87.       case 'h':
  88.         mode = true;
  89.         break;
  90.       case 'f':
  91.         mode = false;
  92.         break;
  93.       case 'd':
  94.         dir = true;
  95.         break;
  96.       case 'r':
  97.         dir = false;
  98.         break;
  99.     }
  100.   }
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement