Application Note

First program: programming motion


This Application Note covers a small program for a motion sequence. It is developed in a fashion that no I/O is used. The only thing needed to be connected is power and communications. There is no difference in function between Lexium MDrive and MDrive motion products.


  • Getting Started
  • MCode OS
  • Motion

Applicable Product

  • Lexium MDrive
  • MDrive Linear Actuator
  • MDrive Plus
  • MForce


  1. PC with SEM Terminal installed
  2. An MDrive or MForce Motion Control connected to power and RS-422/485 communication. If using an MForce a motor needs to be connected as well.

Resource Files

[wpdm_package id=6010 template=[wpdm_package id=2520 template=


Program flow

The flowchart below gives a functional overview of what processes the program will employ. The short overview is that the program will ask for some rotations, ask an axis direction, and then perform the move. Once motion is complete, the axis position will be displayed, and the program will ask to repeat or quit.

What functions of the MCode language is learned in this tutorial?

  • Use of user variables and math functions to perform a move in familiar units of measure (in this case revolutions of the motor shaft).
  • Use of program branches (BR) to control program processes and error checking.
  • Use of labels to provide easy access to the various parts of the program, without having to track memory addresses.
  • Use of the print (PR) command and the input variable (IV)command to allow user interaction with the program.

Detailed explanationFull Program

The following represents the complete program separated out into functional blocks. The full program may be downloaded from the link above and programmed into your drive.

System Configuration Details
Vi=200000 ‘Initial velocity = 200000 steps /sec
Vm=2500000 ‘Max velocity = 2500000 steps /sec
This block, located outside the program envelope, specifies the basic system parameter under which the program will operate
Global user variables
VA k1=0 ‘# of revs to turn
VA k2=0 ‘direction
VA k3=51200 ‘steps/rev
VA k4=0 ‘repeat
The four global user variables contain the data the program will use to calculate and perform the motion based upon the user input into the program.
Initialization block
PG 1
LB su ‘the label “su” designates this ‘program to run on startup
P=-0 ‘clear position counter on startup
PR “At Home Position”
H 2000
This first set of program commands will initialize the position counter to 0, and inform the user that the axis is at home position.
First interaction block – How many revolutions?
LB z1
PR “Enter the number of revolutions in whole numbers”
IV k1
LB x1
BR x1, If=1
H 50
The first interaction block will use the input variable (IV) command to request the desired number of revolutions. This number will be stored in the global user variable k1.

Once the IV command becomes active, the Input Varable Pending flag, IF, will equal 1. We create a lable, x1, and conditionally branch back to the label for as long IF=1.

Second interaction block – which direction?
PR “Enter direction (0) negative (1) positive”
IV k2
LB x2
BR x2, If=1
H 50
The second iteration block, again uses the IV command to request information from a user, branching back to a label while IF=1.
Access the subroutine based upon user entry
BR y1,k2=0
BR y2, k2=1
PR “Invalid Entry”
This block will decide which subroutine to run based on the desired direction of rotation entered into user variable k2. Introduced here is error checking. If an invalid number (other than 0 or 1) was entered into k2, the program will branch back to the X4 location and await a valid entry.
Motion subroutines
‘****subroutine to move k1 revs in the negative ‘direction****
LB y1
MR -k3*k1

‘****subroutine to move k1 revs in the positive ‘direction****
LB y2
MR k3*k1

Thise block will calculate and command the motion based upon the entries into k1 and k2 using the value of k3, which was set to the number of microsteps (51200) in a revolution for an MS setting of 256.
Block to display the axis position
VA k5
PR “Axis position is “, k5, ” absolute from home”
H 3000
This block creates a local user variable, k5 to store the position in revolutions by dividing the position counter, which is in motor steps, by k3, one motor revolution in steps. The result is displayed in absolute revolutions from home.
Third interaction block – repeat or end?
PR “Repeat program (1) or quit (0)”
IV k4
LB x3
BR x3, If=1
BR z1, k4=1
BR z2, k4=0
PR “Invalid Entry”
This block will determine whether or not the program will repeat or end. Again, it stores a response in a user variable, and based upon that response will exit the program or return to the first interaction block.

Note that it also includes an error checking branch

End of program block
PR “Program Ended”
This block designates the end of the program, and the return to immediate mode.

Copy/Paste the below into the Motion Control Programmer application