Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Stepper motor driver running as background process

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-AVR code here
View previous topic :: View next topic  
Author Message
Per Svensson

Bascom Member



Joined: 03 Oct 2004
Posts: 238
Location: Gothenburg, Sweden

sweden.gif
PostPosted: Tue Jan 16, 2018 12:13 am    Post subject: Stepper motor driver running as background process Reply with quote

Time for a small but perhaps useful contribution.
Hope you can make use of it
/Per


'********************** STEP MOTOR DRIVING for Arduino Mega2560 and PBL3717 ****************************

This program drives a stepper motor by an interrupt driven background process. One microstep per interrupt
A fully autonomous session consist of Ramp_up -> Constant speed for N microsteps -> Ramp_down
Any type of 2-phase stepper motor may be used. 4,8,16 or 32 microsteps per physical motor cycle may be specified. This is equivalent to 1,2,4 or 8 microsteps per motor step
A stepping sequence can range from -2^31 to +2^31 microsteps (See STEPPER MOTOR PARAMS here below)
Each stepping sequence will ramp up and down from a safe start rate, for smoother start/stop
It is written for Arduino Mega 2560 interfacing a PBL3717 (NJR3717) or equivalent power IC, but should be rather easy to adapt to other IC's.
A simple command line interpreter will accept commands over a serial port

The whole stepping sequence is strated by calling the STEPP routine. It will start background stepping and then return.
The Tim1_irq interrupt routine will take care of the actual stepping

SYNTAX: CALL STEPP(<steps> , <rate>)
Where <steps> may be -2^31 to +2^31 microsteps
and <rate> may be R to XTAL microsteps/sec, but as TIMER1 is just 16 bits long, there is a lower limit to R
This lower rate limit is R=XTAL/65535 microsteps/s and is automatically checked by the STEPP routine
The upper rate limit is XTAL microsteps/s, but this is of course an unrealistic rate for any motor...

Stepping will automatically wait until previous stepping is finished (or cancelled by user)
Stepping will start from a safe rate (S). Then ramp up to final sustained rate (R)

Two PBL3717 ICs are needed. On for each winding. (see attached schematic)
The Arduino Mega 2560 interface is like this:
Vref_a/b is controlling the amount of power for each microstep via a PWM acting as a DAC (Sine wave approximated power)
Phase_a/b is controlling currents direction in the coils during a full microstep sequence
Phase_a Alias Portl.0 'Arduino pin 49
Phase_b Alias Portl.1 'Arduino pin 48
I0_a Alias Portl.2 'Arduino pin 47
I0_b Alias Portl.3 'Arduino pin 46
Vref_a Alias Portl.4 'Arduino pin 45
Vref_b Alias Portl.5 'Arduino pin 44
I1_a Alias Portl.6 'Arduino pin 43
I1_b Alias Portl.7 'Arduino pin 42


TCC1 is used for control of the stepping rate
TCC5 is used for Motor power control. It acts as a PWM-DAC for the sine wave microstepping
Back to top
View user's profile Visit poster's website
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 6197
Location: Holland

blank.gif
PostPosted: Tue Jan 16, 2018 10:04 am    Post subject: Reply with quote

Hi Per,

Thank you for sharing your work. This is a great piece of code. Wish i had that some years ago when i had to figure it out Very Happy
I see that you also included an AN about the LC filter. Interesting.

_________________
Mark
Back to top
View user's profile Visit poster's website
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 1133

poland.gif
PostPosted: Tue Jan 16, 2018 10:47 am    Post subject: Reply with quote

About this neat code description and style... Wow Shocked
I will take some shapes from this work for my next codes Very Happy
Thanks for sharing.
Back to top
View user's profile Visit poster's website
snow

Bascom Member



Joined: 28 Jun 2005
Posts: 212
Location: Ashburton / Mid Canterbury / New Zealand

newzealand.gif
PostPosted: Mon Feb 10, 2020 9:21 pm    Post subject: Reply with quote

Thanks for sharing. Great piece of code. Will help me out alot
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-AVR code here All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum