DRV8825 steeper motor controller with serial interface for delimited matlab input input format dir,step, delay
Diff: main.cpp
- Revision:
- 0:24171bdfc41f
- Child:
- 1:19ba52379a64
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri Sep 16 13:31:15 2016 +0000 @@ -0,0 +1,133 @@ +/*Kamil Kanas +NUCLEO FR401 Serial Stepper motor controller DRV8825 +Matlab interface (dir,step,speed) +15/9/2016 +*/ + + +#include "mbed.h" + +// DRV8825 control pins + +#define stepPin PA_5 //Step pin +#define dirPin PA_6 //Direction pin +#define enPin PA_7 //Enable pin + +/*--INITS-------------------------------------------------------------------------------*/ +Serial pc(USBTX, USBRX); // create PC interface +DigitalOut stepOut(stepPin); // step output +DigitalOut dirOut(dirPin); // direction output +DigitalOut enableOut(enPin); //enable output + +/*--VARIABLES---------------------------------------------------------------------------*/ +char buffer[16]; //Serial buffer +uint8_t direction; //Direction 0 - right, 1- left +uint16_t step; //step 0-400 full turn in half step mode +uint16_t speed; //speed +char * pch; //auxilary pointer to buffer +char *mot_char[16]; //motor char +int mot_int[3]; //motor int +/*--FUNCTION DECLARATIONS---------------------------------------------------------------*/ +void int_usb(void); +void stepMOtor (int,int,int); +void stepDemo (void); +/*--FUNCTION DEFINITIONS----------------------------------------------------------------*/ + +/* + void int_usb (void) // initialize serial connection +*/ + +void int_usb(void) + + { + pc.baud(115200); /*Set baud rate*/ + } + + +/* stepMottor + int dir 0- right, 1- left + int steps 0-200 full step mode + int speed +*/ + +void stepMOtor(int dir, int steps, int speedS ) + +{ + + if (!dir) // Checking direction flag + { + + enableOut=0; + dirOut=0; // drive right + + } + else + { + enableOut=0; + dirOut=1; // drive left + } + + int deg; + + for (deg=0;deg<steps;deg++) + { + + stepOut=1; + wait_us(500); + stepOut=0; + wait_ms(speedS); + if (deg==steps-2) + { + pc.printf("ready %s \n"); + } + } + } + +/* +void stepDemo(void); +full motor revolution +0.8 deg / sec +*/ + +void stepDemo (void) +{ + enableOut=0; + dirOut=0; // Enables the motor direction right + for(int x = 0; x < 199; x++) { // Makes 400 steps for full revolution + stepOut=1; + wait_us(1000); + stepOut=0; + wait_ms(999); + } +} + +int main() { + int_usb(); + + while(1){ + pc.printf("Hello %s \n"); + wait(0.5); + while(true){ + while (pc.readable()) { //if data in the ring buffer + pc.gets(buffer,16); //read 7 characters + pc.printf("stringReceived %s ", buffer); + char * pch; + pch = strtok (buffer,","); + int i=0; + while (pch != NULL) + + { + mot_char[i++] = pch; + pch = strtok (NULL, ","); + } + + for (i = 0; i < 3; ++i) { + //pc.printf("%s\n", mot_char[i]); + mot_int[i]=atoi(mot_char[i]); + pc.printf("%d\n", mot_int[i]); + } + break; } + + } + } + }