Timer Interrupts to generate step signal for TMC5160 on Atmega2560

Hi!
I'm trying to create my firmware to run stepper motors for a 3d printer. So far, I've been using timer interrupts to generate the step signal.
Calculations I'm using:
Steps per mm(calculated based on the belt) = 40.76
Micro steps = 16
Number of pulses per revolution = 20016 = 3200
If speed = 10 mm/s,
pulses req in 1 sec = 40.76
10 = 408
pulse period = 1/408 = 0.00245 seconds
count = 0.00254/2*(25e-6)
So, in a 25 micro second interrupt, I increment a count variable and keep the state high till the count reaches my calculated value. Then, the state is made low till the count reaches twice the calculated value.
This works fine as long as the pulse period is greater than my interrupt time (25 us). But, for high speeds and different steps per mm value, the pulse period is as low as 6 us.
I would like to know if there's any point in using a 1 micro second interrupt to run at high speeds or any other method for this.
Hardware used: Atmega2560 microcontroller, TMC5160-BOB drivers and NEMA23/17 motors.
Thanks in advance!

A popular library is Accelstepper.h. It uses acceleration, speed and more.

Oftenly used is a Gcode sender that sends Gcode to the Gcode interpreter in the controller.

Hi, @tris0601 .

I understand that you want to use timer interrupt.
AccelSteppler requires periodic execution, so it is difficult to use in an execution environment where there is a delay, such as when communicating with a PC.

I suggest Mobatools. You can install by library manager in Arduino IDE. This is a library that uses timer interrupts.

However, if your application does not include delays, such as communication with a PC, then AccelStepper, as suggested by @Railroader , would be of more help.

That means a steprate of 166.66kHz. I don't think this is possible with an ATMega2560.
On an AVR with 16MHz Accelstepper run() needs about 200µs if a step is due. MobaTools limits the steprate to 2.5kHz ( for upt o 6 steppers each ). If you need only one stepper you can tune to 5kHz.
You need a much more powerful and faster processor for your speed requirements. Maybe a Teensy 4.0?

As a comparison GRBL can max out at 30khz steps on an Arduino UNO. This means theoretically you could get 750mm/s (for one motor). That is fast! Typical print speed for ABS filament is 60 mm/s.

My question is why are you rolling your own code? There is open source 3D printer firmware available. Marlin is one example.

Will try it out, thanks!

I've tried to avoid all blocking statements. Does reading from SD card module come under this?
Will check out mobatools, thanks for the help.

I had some doubts that it's not possible on atmega2560. But, usual 3d printer firmware such as Marlin can run this, so I was wondering if there's some other approach I'm missing.

The goal is to create something similar to Marlin, not will all the extensive features ofc, but a bare skeleton version that can give a somewhat decent print.

I operate my CNC machine with an Arduino UNO loaded with GRBL. I'm running with 8-microsteps but at the speeds I run my machine it doesn't even come close to stressing the UNO.

Hi, I was going through the source code of accelstepper and found that it uses the micros function. Is it better to tweak my logic based on micros function to generate step signal?
I'm thinking of this because, besides the speed limit issue, I've found the motor runs slowly when I use more than one motor.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.