5 months, 1 week ago.

Arduino code to mbed

Hi, can you please convert this arduino code to Mbed used to command a step motor using an STM32 Nucleo and a DRV8825? Thank you! It's urgent! - Command of a StepStick/Driver A4988 ---- DRV8825_Test.ino Commandof a step motor with a pilote DRV8825 and Arduino.

  1. define pinEnable 13 Activation of the driver
  2. define pinStep 9 Signal of STEP (avancement)
  3. define pinDir 8 Direction

void setup(){ Serial.begin(9600); Serial.println("Test DRV8825");

pinMode( pinEnable, OUTPUT ); pinMode( pinDir , OUTPUT ); pinMode( pinStep , OUTPUT ); }

void loop(){ int i = 0;

digitalWrite( pinDir , HIGH); Direction forward digitalWrite( pinStep , LOW); Initialisation of the pin step

Avance de 200 pas for( i=0; i<200; i++){ Serial.println( i ); digitalWrite( pinStep, HIGH ); delay( 10 ); digitalWrite( pinStep, LOW ); delay( 10 ); }

Changer de direction digitalWrite( pinDir , LOW); Direction avant

Refaire 200 pas dans l'autre sens for( i=0; i<200; i++){ Serial.println( i ); digitalWrite( pinStep, HIGH ); delay( 1 ); digitalWrite( pinStep, LOW ); delay( 1 ); }

Pas de step et pas d'ordre... l'axe du moteur est donc bloqué Serial.println("Axe bloqué + attendre 5 sec"); delay( 5000 );

déblocage de l'axe moteur Serial.println("Deblocage axe"); digitalWrite( pinEnable, HIGH ); logique inversée

Fin et blocage du programme Presser reset pour recommander Serial.println("Fin de programme"); while( true ); }

Comment on this question

1 Answer

5 months, 1 week ago.

Hi there,

please, for the next time, open "Editing tips" and correctly mark your code with <<code title=Title>> Your Code <</code>>

You can start here https://os.mbed.com/docs/mbed-os/v5.12/quick-start/index.html

Here you have some libraries for DRV8825 (not tested)

Import libraryDRV8825

Lybrary to drive DRV8825 stepper motor driver

Import libraryDRV88255

Fully functional driver for the DRV88255 stepper motor driver. Includes microstepping, speed, disable and enable.

But 10 min is not too much so here you have a code. But keep in mind I not tested this code, I only rewrote your code for Mbed Compiler.

Code

#include "mbed.h"

#define ENABLE  D13 //Activation of the driver 
#define STEPS   D9  //Signal of STEP (avancement) 
#define DIR     D8  //Direction 

#define HIGH    1   //Direction 
#define LOW     0   //Direction 

//Serial serial(USBTX, USBRX); // I use default STDout
DigitalOut enable(ENABLE);
DigitalOut steps(STEPS);
DigitalOut dir(DIR);

int main(void){ 
    //serial.begin(9600); // 9600 is default
    printf("Test DRV8825\n");
    
    int i = 0;
    dir = HIGH; //Direction forward
    steps = LOW; //Initialisation of the pin step
    //Avance de 200 pas
    for( i=0; i<200; i++) {
        printf("%d\n",i);
        steps = HIGH;
        wait_ms(10);
        steps = LOW;
        wait_ms(10);
    }
    //Changer de direction
    dir = LOW;  //Direction avant
    //Refaire 200 pas dans l'autre sens
    for( i=0; i<200; i++) {
        printf("%d\n",i);
        steps = HIGH;
        wait_ms(10);
        steps = LOW;
        wait_ms(10);
    }
    //Pas de step et pas d'ordre... l'axe du moteur est donc bloqué
    printf("Axe bloqué + attendre 5 sec\n");
    wait(5);
    //déblocage de l'axe moteur
    printf("Deblocage axe\n");
    enable = HIGH; //logique inversée
    //Fin et blocage du programme Presser reset pour recommander
    printf("Fin de programme\n");

    while(true);
}

Best regards

J.


You need to log in to post a question