- Steppermotor-Controlling with STM32F103C8T6 -

68 views Asked by At

You can see a picture from my trial stand attached to this forum post. I would like to introduce the composition of aggregates and the project content short:

1. A commercially available stepper motor (size: NEMA 23)

2. A digital stepping drive (Type: DM422C, Co.: LEADSHINE)

3. A STM32F103C8T6 on a plug-in board

Now it comes to my objectives and questions:

1. I would like to position the stepper motor in a predefined and accurately way with C++-code.

2. The micro-controller pin out for this composition is:

PA2 (STM32F103C8T6) -> PUL (DM422C)

PA3 -> DIR

PA4 -> ENA

5 V -> OPTO

Additional informations: The PUL-signal structure should be a "single mode"-structure after declarations in the data-sheet (DM422C, page 4).

But now to my inadequacies regarding "information technology" as main field or in particular a simple programming-question (C++) for this sample composition:

I tried for a simple drive of stepper motor in this composition the following code snippet:


    RCC->APB2ENR |= 0xFC;

    GPIOA->CRL = 0x44433344; 
    GPIOA->ODR |= (1<<4);
    GPIOA->ODR |= (1<<3);

  while (1)
  {
        GPIOA->ODR |= (1<<2);
        delay_ms(50);
        GPIOA->ODR &= !(1<<2);
        delay_ms(50);
  }

I would like to open our discussion now. Please be aware that a binary notation in c++-code in look to your answers would be a higher support for me. A minimalistic and working c++-snippet for a stepper motor drive is wished.

A used didactic way in the answers regarding the not working c++-code is also wished.

Have an - in common and below university-student-words - "nice" day or night!

Please excuse my style of post, it is one of my first posts here.

Yours TIMBO 2023

Trial Stand

1

There are 1 answers

7
Ben Voigt On

&= !(1<<2) is a mistake. You meant to use bitwise complement ~ not logical inversion !.

As a consequence of this error, you are clearing DIR and ENA every time you attempt to generate a falling edge on PUL.

Try with

GPIOA->ODR &= ~(1u<<2);

You may alternatively choose to use the hardware logic for generating GPIO falling edges, accessed through

GPIOA->BRR = 1u<<2;

and for rising edges

GPIOA->BSRR = 1u<<2;

These are better than bitwise manipulations |= and &= because

  • They don't require a read from ODR
  • They don't require an ALU operation
  • They are atomic, so if an interrupt occurs that writes a different pin on the same port, you can't accidentally lose the change made by the interrupt handler.

For these reasons, you should only use ODR when you intend to overwrite all pins of the GPIO port, and for changing individual pins or groups, use BSRR and BRR.