Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Sergei A. Minayev, 2015
- L9110S Stepper motor driver test app
- Working app: http://youtu.be/OD8UAhJ7-Qg
- */
- // pin assignments for L9110S
- const int AIA = 4;
- const int AIB = 5;
- const int BIA = 2;
- const int BIB = 3;
- // step controller definitions
- int state = 0; // current step for state machine
- int spd = 30; //ms step timing (speed)
- bool dir = true; // true - forward; false - backward
- bool mode = true; // true - half step; false - full step
- // communication definitions
- char incoming_char;
- //0000 - AIA, AIB, BIA, BIB
- void phase_out (byte a){
- if (bitRead(a,0)) {digitalWrite(BIB, HIGH);} else {digitalWrite(BIB, LOW);}
- if (bitRead(a,1)) {digitalWrite(BIA, HIGH);} else {digitalWrite(BIA, LOW);}
- if (bitRead(a,2)) {digitalWrite(AIB, HIGH);} else {digitalWrite(AIB, LOW);}
- if (bitRead(a,3)) {digitalWrite(AIA, HIGH);} else {digitalWrite(AIA, LOW);}
- }
- void state_machine (void){
- switch (state) {
- case 0:
- phase_out(0b00001000);
- if (mode) {if (dir){state=1;} else {state = 7;}} else {if (dir){state=2;} else {state = 6;}}
- break;
- case 1:
- phase_out(0b00001010);
- if (dir){state=2;} else {state = 0;}
- break;
- case 2:
- phase_out(0b00000010);
- if (mode) {if (dir){state=3;} else {state = 1;}} else {if (dir){state=4;} else {state = 0;}}
- break;
- case 3:
- phase_out(0b00000110);
- if (dir){state=4;} else {state = 2;}
- break;
- case 4:
- phase_out(0b00000100);
- if (mode) {if (dir){state=5;} else {state = 3;}} else {if (dir){state=6;} else {state = 2;}}
- break;
- case 5:
- phase_out(0b00000101);
- if (dir){state=6;} else {state = 4;}
- break;
- case 6:
- phase_out(0b00000001);
- if (mode) {if (dir){state=7;} else {state = 5;}} else {if (dir){state=0;} else {state = 4;}}
- break;
- case 7:
- phase_out(0b00001001);
- if (dir){state=0;} else {state = 6;}
- break;
- default:
- state = 0;
- }
- }
- void setup() {
- pinMode(AIA, OUTPUT);
- pinMode(AIB, OUTPUT);
- pinMode(BIA, OUTPUT);
- pinMode(BIB, OUTPUT);
- Serial.begin(9600);
- Serial.write("f - full step; h - half step; d - forward; r - backward");
- }
- void loop() {
- state_machine();
- delay(spd);
- if (Serial.available() > 0) {
- incoming_char = Serial.read();
- switch (incoming_char) {
- case 'h':
- mode = true;
- break;
- case 'f':
- mode = false;
- break;
- case 'd':
- dir = true;
- break;
- case 'r':
- dir = false;
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement