Components and supplies
Stepper motor SMJ40-4880-A
Arduino Mega 2560
EVALSP820-SP
Project description
Code
STEPPER MOTOR CONTROL WITH STSPIN820 EVAL BOARD
arduino
1[code] 2 3/* 4BIPOLAR STEPPER MOTOR CONTROL WITH EVALUATION BOARD OF THE STSPIN820 BY STMicroelectronicss 5Versione 1.0 del 07/03/2018 6AUTORE: microst 7*/ 8 9// set pin numbers: 10const int EN = 23; 11const int M0 = 25; 12const int M1 = 27; 13const int M2 = 29; 14const int STDBY = 33; 15const int STEP = 35; 16const int DIR = 37; 17 18 19// variables will change: 20 21int number = 0; 22int freq = 10; 23int swpulse = 0; 24int lettura = 0; 25int dur = 0.1; 26int mode = 0; 27int m0 = 0; 28int m1 = 0; 29int m2 = 0; 30int dir= 0; 31unsigned long tempMillis; di PWM 32byte caratteri; 33byte tempChar; 34 35 36void setup() { 37 // pin definitions 38 Serial.begin(9600); 39 pinMode(STDBY, OUTPUT); 40 pinMode(M0, OUTPUT ); 41 pinMode(M1, OUTPUT ); 42 pinMode(M2, OUTPUT); 43 pinMode(DIR, OUTPUT); 44 pinMode(STEP, OUTPUT); 45 digitalWrite(STDBY, LOW); //device in stand-by 46 pinMode(EN, OUTPUT); digitalWrite(EN, LOW); // output stage disabled 47 digitalWrite(M0, HIGH);digitalWrite(M1, LOW);digitalWrite(M2, LOW); //initial mode configuration 48 delay (1000); 49 // Serial monitor menu 50 Serial.println(F(" m = mode")); 51 Serial.println(F(" r = dir anti clockwise ")); 52 Serial.println(F(" l = dir clockwise")); 53 Serial.println(F(" e = driver on ")); 54 Serial.println(F(" o = driver in stand by")); 55 Serial.println(F(" s = step")); 56 Serial.println(F(" f = frequency or speed")); 57 // 58} 59 60void loop() { 61 //check serial 62 if (Serial.available()) { 63 byte command = Serial.read(); //leggo il primo byte 64 switch (command) { //controllo che sia un comando valido 65 66 // enable the device ( out of stand by ) 67 case 'e': 68 Serial.println(" driver on "); 69 digitalWrite(STDBY, HIGH); 70 delay(1); 71 break; 72 // disable the device - stand by 73 case 'o': 74 Serial.println(" driver in stand-by"); 75 digitalWrite(STDBY, LOW); 76 break; 77 // define the cw motor direction 78 case 'l': 79 Serial.println(" direction clock wise"); 80 dir=1; 81 break; 82 // define the ccw motor direction 83 case 'r': 84 Serial.println(" direction counter clock wise"); 85 dir=0; 86 break; 87 case 's': //step number 88 readnumb(); 89 swpulse=lettura; 90 Serial.println(F("step number is ")); 91 Serial.println(swpulse, DEC); 92 Serial.println(F(" motor actived ")); 93 stepper(swpulse,m0,m1,m2,dir,dur); 94 Serial.println(F("motor stopped ")); 95 break; 96 case 'f': // speed number 97 readnumb(); 98 freq=lettura; 99 dur= freq/2; 100 Serial.print(F("speed is ")); 101 Serial.println(freq, DEC); 102 break; 103 case 'm': //mode selection 104 readnumb(); 105 mode=lettura; 106 if(mode>6){mode=6;} 107 m0=0;m1=0;m2=0; 108 if ( mode==0 ) { Serial.println(F("mode 1/1 step selected"));} 109 if ( mode==1 ) { Serial.println(F("mode 1/2 step selected"));m0=1;}; 110 if ( mode==2 ) { Serial.println(F("mode 1/4 step selected"));m1=1;} 111 if ( mode==3 ) { Serial.println(F("mode 1/8 step selected"));m0=1;m1=1;} 112 if ( mode==4 ) { Serial.println(F("mode 1/16 step selected"));m2=1;} 113 if ( mode==5 ) { Serial.println(F("mode 1/128 step selected"));m2=1;m1=1;} 114 if ( mode==6 ) { Serial.println(F("mode 1/256 step selected"));m2=1;m1=1;m0=1;} 115 break; 116 } 117 } 118} 119 120 121 122// function stepper for EVALSP820-XS 123int stepper (int swpulse, int m0, int m1, int m2, int dir, int dur){ 124 if (m0==0) {digitalWrite(M0, LOW);} else {digitalWrite(M0, HIGH);} 125 if (m1==0) {digitalWrite(M1, LOW);} else {digitalWrite(M1, HIGH);} 126 if (m2==0) {digitalWrite(M2, LOW);} else {digitalWrite(M2, HIGH);} 127 if (dir==0) {digitalWrite(DIR, LOW);} else {digitalWrite(DIR, HIGH);} 128 pinMode(EN, INPUT_PULLUP); // output stage on 129 delay(10); 130 if (digitalRead(EN)==LOW) {Serial.println(F(" VM UVLO condition: check the VM power supply ")); } 131 while (digitalRead(EN)==LOW) {;} 132 // STEP GENERATOR 133 for ( int i = 0; i < swpulse; i++) { 134 digitalWrite(STEP, HIGH); 135 delay(dur); 136 digitalWrite(STEP, LOW); 137 delay(dur); 138 if (digitalRead(EN)==LOW) {Serial.println(F("fault ")); i=swpulse; } 139 } 140 pinMode(EN, OUTPUT); 141 digitalWrite(EN, LOW); // output stage off 142 return; 143} 144 145 int readnumb (){ 146 lettura = 0; tempMillis = millis(); caratteri = 0; 147 do { 148 if (Serial.available()) { 149 tempChar = Serial.read(); 150 caratteri++; 151 if ((tempChar >= 48) && (tempChar <= 57)) { //è un numero? ok 152 lettura = (lettura * 10) + (tempChar - 48); 153 } else if ((tempChar == 10) || (tempChar == 13)) { 154 //con invio e/o nuova linea esco 155 break; 156 } 157 } 158 } while ((millis() - tempMillis < 500) && (lettura <= 100) && (caratteri < 10)); 159 //se il valore di PWM è valido, lo imposto sul pin 160 if (lettura >= 500) {lettura=500;} 161 return (lettura); 162} 163 164 165[/code]
STEPPER MOTOR CONTROL WITH STSPIN820 EVAL BOARD
arduino
1[code] 2 3/* 4BIPOLAR STEPPER MOTOR CONTROL WITH EVALUATION BOARD 5 OF THE STSPIN820 BY STMicroelectronicss 6Versione 1.0 del 07/03/2018 7AUTORE: 8 microst 9*/ 10 11// set pin numbers: 12const int EN = 23; 13const int 14 M0 = 25; 15const int M1 = 27; 16const int M2 = 29; 17const int STDBY = 18 33; 19const int STEP = 35; 20const int DIR = 37; 21 22 23// variables will 24 change: 25 26int number = 0; 27int freq = 10; 28int swpulse = 0; 29int 30 lettura = 0; 31int dur = 0.1; 32int mode = 0; 33int m0 = 0; 34int m1 = 0; 35int 36 m2 = 0; 37int dir= 0; 38unsigned long tempMillis; di PWM 39byte caratteri; 40byte 41 tempChar; 42 43 44void setup() { 45 // pin definitions 46 Serial.begin(9600); 47 48 pinMode(STDBY, OUTPUT); 49 pinMode(M0, OUTPUT ); 50 pinMode(M1, OUTPUT ); 51 52 pinMode(M2, OUTPUT); 53 pinMode(DIR, OUTPUT); 54 pinMode(STEP, OUTPUT); 55 56 digitalWrite(STDBY, LOW); //device in stand-by 57 pinMode(EN, OUTPUT); digitalWrite(EN, 58 LOW); // output stage disabled 59 digitalWrite(M0, HIGH);digitalWrite(M1, LOW);digitalWrite(M2, 60 LOW); //initial mode configuration 61 delay (1000); 62 // Serial monitor menu 63 64 Serial.println(F(" m = mode")); 65 Serial.println(F(" r = dir anti clockwise 66 ")); 67 Serial.println(F(" l = dir clockwise")); 68 Serial.println(F(" e 69 = driver on ")); 70 Serial.println(F(" o = driver in stand by")); 71 Serial.println(F(" 72 s = step")); 73 Serial.println(F(" f = frequency or speed")); 74 // 75} 76 77void 78 loop() { 79 //check serial 80 if (Serial.available()) { 81 byte command = 82 Serial.read(); //leggo il primo byte 83 switch (command) { //controllo che sia 84 un comando valido 85 86 // enable the device ( out of stand by ) 87 case 88 'e': 89 Serial.println(" driver on "); 90 digitalWrite(STDBY, 91 HIGH); 92 delay(1); 93 break; 94 // disable the device - stand by 95 96 case 'o': 97 Serial.println(" driver in stand-by"); 98 digitalWrite(STDBY, 99 LOW); 100 break; 101 // define the cw motor direction 102 case 'l': 103 104 Serial.println(" direction clock wise"); 105 dir=1; 106 break; 107 108 // define the ccw motor direction 109 case 'r': 110 Serial.println(" 111 direction counter clock wise"); 112 dir=0; 113 break; 114 case 's': 115 //step number 116 readnumb(); 117 swpulse=lettura; 118 Serial.println(F("step 119 number is ")); 120 Serial.println(swpulse, DEC); 121 Serial.println(F(" 122 motor actived ")); 123 stepper(swpulse,m0,m1,m2,dir,dur); 124 Serial.println(F("motor 125 stopped ")); 126 break; 127 case 'f': // speed number 128 readnumb(); 129 130 freq=lettura; 131 dur= freq/2; 132 Serial.print(F("speed 133 is ")); 134 Serial.println(freq, DEC); 135 break; 136 case 'm': 137 //mode selection 138 readnumb(); 139 mode=lettura; 140 if(mode>6){mode=6;} 141 142 m0=0;m1=0;m2=0; 143 if ( mode==0 ) { Serial.println(F("mode 144 1/1 step selected"));} 145 if ( mode==1 ) { Serial.println(F("mode 1/2 146 step selected"));m0=1;}; 147 if ( mode==2 ) { Serial.println(F("mode 148 1/4 step selected"));m1=1;} 149 if ( mode==3 ) { Serial.println(F("mode 150 1/8 step selected"));m0=1;m1=1;} 151 if ( mode==4 ) { Serial.println(F("mode 152 1/16 step selected"));m2=1;} 153 if ( mode==5 ) { Serial.println(F("mode 154 1/128 step selected"));m2=1;m1=1;} 155 if ( mode==6 ) { Serial.println(F("mode 156 1/256 step selected"));m2=1;m1=1;m0=1;} 157 break; 158 } 159 } 160} 161 162 163 164// 165 function stepper for EVALSP820-XS 166int stepper (int swpulse, int m0, int m1, int 167 m2, int dir, int dur){ 168 if (m0==0) {digitalWrite(M0, LOW);} else {digitalWrite(M0, 169 HIGH);} 170 if (m1==0) {digitalWrite(M1, LOW);} else {digitalWrite(M1, HIGH);} 171 172 if (m2==0) {digitalWrite(M2, LOW);} else {digitalWrite(M2, HIGH);} 173 if (dir==0) 174 {digitalWrite(DIR, LOW);} else {digitalWrite(DIR, HIGH);} 175 pinMode(EN, INPUT_PULLUP); 176 // output stage on 177 delay(10); 178 if (digitalRead(EN)==LOW) {Serial.println(F(" 179 VM UVLO condition: check the VM power supply ")); } 180 while (digitalRead(EN)==LOW) 181 {;} 182 // STEP GENERATOR 183 for ( int i = 0; i < swpulse; i++) { 184 digitalWrite(STEP, 185 HIGH); 186 delay(dur); 187 digitalWrite(STEP, LOW); 188 delay(dur); 189 190 if (digitalRead(EN)==LOW) {Serial.println(F("fault ")); i=swpulse; } 191 } 192 193 pinMode(EN, OUTPUT); 194 digitalWrite(EN, LOW); // output stage off 195 return; 196} 197 198 199 int readnumb (){ 200 lettura = 0; tempMillis = millis(); caratteri = 0; 201 202 do { 203 if (Serial.available()) { 204 tempChar = Serial.read(); 205 206 caratteri++; 207 if ((tempChar >= 48) && (tempChar 208 <= 57)) { //è un numero? ok 209 lettura = (lettura * 10) + (tempChar 210 - 48); 211 } else if ((tempChar == 10) || (tempChar == 13)) { 212 213 //con invio e/o nuova linea esco 214 break; 215 216 } 217 } 218 } while ((millis() - tempMillis 219 < 500) && (lettura <= 100) && (caratteri < 10)); 220 //se il valore di 221 PWM è valido, lo imposto sul pin 222 if (lettura >= 500) {lettura=500;} 223 224 return (lettura); 225} 226 227 228[/code]
Downloadable files
STSPIN820 Evalution board with arduino mega 2560
STSPIN820 Evalution board with arduino mega 2560
Comments
Only logged in users can leave comments