Intro to MCode : Program and Language Structure
This Application Note covers the basic structure and programming conventions of the MCode language, The differences between Liberty* MCode and MDrive MCode is primarily found in the syntax of the I/O instructions.
- Liberty* MDrive
- MDrive Linear Actuator
- MDrive Plus
- PC with the Motion Control Programmer installed
- A Liberty* MDrive Motion Control connected to power and communications.
Basic structure of the MCode language
Liberty* MCode consists of 1 and 2 character mnemonics which access all the features and functions of the Motion Control product family. These mnemonics are clearly identified as to their function. i.e. VI (Initial Velocity), A (Acceleration), MS (MicroStep resolution) and etc. HINT: :Liberty* MCode commands ARE NOT Case sensitive. They may be upper or lower case. There are five basic components of the Liberty* MCode programming language, they are:
- Math functions
An instruction results in an action. There are four types of instructions:
The examples below will not show every instruction example, just the most commonly used.
Motion instructions are those that result in the movement of a motor. The syntax for these commands is as follows: type the command followed by a space, and then the velocity or position data. Example motion instructions
|MA||Move to an absolute position||MA 51200||Move abs position 51200|
|MR||Move to a relative position||MR 51200||Move relative 51200 steps|
|SL||Slew axis at constant velocity||SL 20000||Slew at 20000 steps/sec|
|HM||Homing mode set up||HM 1||Set homing mode to mode 1|
Liberty* MDrive I/O Instructions
An I/O instruction results in the change of parameters or the state of an input or output. The syntax for these commands are as follows: type the command then an equal sign, then the data. Example: O2=0 will set output 2 to 0. Example I/O instructions
MnemonicFunctionUsage exampleDescriptionIsSet an input point type and active stateIs=1,0,0Set input 1 to be general purpose active LOWOsSet an output point type and active stateOs=1,16,0Set output 1 to be general purpose active LOWI<input>Read an individual input state as 1 or 0PR I4Read the state of input 4CL k3, I4=1Call subroutine k3 when I/O 4 is HIGH (1)O<output>Set the logic state of an individual outputO3=1Set output 3 HIGH (1)
MDrive Motion Products
An I/O instruction results in the change of parameters or the state of an input or output. The syntax for these commands are as follows: type the command then an equal sign, then the data. Example: O2=0 will set output 2 to 0.
Example I/O instructions
A program instruction allows program manipulation. The syntax of these vary due to the nature of the command. Some command examples would be: PG 100, which toggles the system into program mode starting at address 100; BR LP, I1=1, which will branch to a program labeled LP if input 1 is true. Example program instructions
|PG||Enter/exit program mode||PG 100||Begin program at address 200|
|LB||Label program, subroutine or branch process||LB k1||Label subroutine k1|
|LB SU||Label program to run at startup (SU is a keyword for this action)|
|BR||Used to branch conditionally or unconditionally) to a routine or process||BR k1||Unconditional branch to process k1|
|BR k1, I1=1||Conditional branch to process k1 if I/O 1 = 1|
|CL||Call subroutine conditionally or unconditionally||CL Q2||Unconditionally call subroutine Q2|
|CL Q2, R1>1||Conditionally call subroutine Q2 if register 1 is greater than 1|
|H||Hold program execution||H||Hold program execution until motion completes|
|h 1000||Hold program execution for 1 second|
|E||Designates the end of a program||E||End program|
A system instruction is an instruction that can only be used in immediate mode to perform a system operation such as program execution (EX) or listing the contents of program memory (L). For example: EX 100 will execute a program located at address 100 of program memory space, or EX K1 will execute a program labeled K1. Example program instructions
|CP||Clear program memory space||CP 100||Clear program memory starting at address 100|
|EX||Execute program||EX G1||Execute program labeled G1|
|S||Save||S||Save all parameters and programs|
|FD||Restore factory default settings||FD||Restore factory defaults|
A Variable is identified by a mnemonic and allows the user to define or manipulate data. These can also be used with the math functions to manipulate data. There are two classes of variables: factory-defined and user-defined. There are 192 user program labels and variables available. The syntax for each variable may differ.
Factory defined variables
These variables are predefined at the factory. They cannot be deleted. When an FD (Factory Default) instruction is given, these variables will be reset to their factory default values. There are two types of factory defined variables:
- Read/write – variables that may have their value altered by the user to effect events in a program.
- P (position counter) can be set i.e. P=0 or read, i.e PR P.
- A (acceleration) can be changes as part of a move profile i.e. A=200000 steps/sec^2.
- Read only – Variables whose value cannot be modified by the user, but may be read or used to effect program events.
- V (velocity register the current velocity of the axis in steps/second.
User defined variables
The VA instruction allows the user to create a 2 character name to a user defined variable (32 bit value). When and FD (Factory Default) is given, user defined variables are deleted. The restrictions for this command are:
- A user variable must have a unique name, it cannot be named after an MCode command,
- The first character must be alpha, the second may be alpha-numeric, i.e. K1, Ga, Z3 etc.HINT: The following Alpha characters are not used by any MCode command: G, K, X, Y or Z. A best practice is to name all user variables and program/subroutine labels with one of these alpha characters followed by a number.
- Two character limit.
There are two types of user defined variables:
- Global variables – global variables are variables that are defined outside of a program. The benefit t to using a global variable is that no user program memory is required.
- Local variables – this type of user defined variable is defined within a program and can only affect events within that program. It is stored in RAM. Note a local variable is not static, but is erased and declared again each time a program is executed.
Commonly used variables
|A||Acceleration||A=200000||Set Acceleration to 200000 steps/sec^2|
|D||Deceleration||D=A||Set deceleration equal to acceleration|
|MS||Microstep resolution||MS=2||Set microstep resolution to 400 steps/rev (half-step)|
|P||Position||P=0||Set position counter to 0|
|VI||Initial velocity||VI=1000||Set initial velocity to 1000 steps/sec|
|VM||Maximum velocity||VM=10000000||Set max velocity to 10000000 steps/sec|
Keywords are used in conjunction with the PR and IP instructions to indicate or control variables and flags. For instance, PR UV would print the state of all the user-defined variables to the screen. IP would restore all the factory variables from the NVM.
Math functions are used to perform various arithmetic functions on numeric data stored in registers or variables. Supported functions are +, -, x, ÷, >, <, =, ≤, ≥, AND, OR, XOR, NOT. For purposes of the following examples we will use registers R1-R4) and user variables labeled k1….kn for illustrating the use of math functions, though any variable may be used.
|*||Multiply||Multiply to variables||R1=k1*k2|
|/||Division||Divide two variables||R1=k1/k2|
|<>||Not equal||Perform action if variable is not equal to variable or integer||BR G1, k1<>1 CL Z2, k1<>1|
|=||Equal to||Perform action if variable is equal to variable or integer||BR G1, k1=1 CL Z2, k1=0|
|<||Less than||Perform action if variable is less than a variable or integer||BR G1, k1<k2 CL Z2, k1<50|
|<=||Less than or equal to||Perform action if variable is less than or equal to a variable or integer||BR G1, k1<=k2 CL Z2, k1<=50|
|>||Greater than||Perform action if variable is greater than a variable or integer||BR G1, k1>k2 CL Z2, k1>50|
|>=||Greater than or equal to||Perform action if variable is greater than or equal to a variable or integer||BR G1, k1>=k2 CL Z2, k1>=50|
|&||AND (bitwise)||Perform a bitwise AND operation||R1=k1&k2|
||||OR (bitwise)||Perform a bitwise OR operation||R1=k1|k2|
|^||XOR (bitwise)||Perform a bitwise XOR operation||R1=k1^k2|
|!||NOT (bitwise)||Perform a bitwise NOT operation||R1=k1!k2|
|User labels||The MCode programming language allows for 192 user labels for your programs, subroutines, and user variables and flags. A label consists of 2 characters, the first of which must be a letter, the second may be alphanumeric. A label cannot use the same character combination as any of the mnemonics used in the MCode programming language||PG LB G1 – label program as G1VA K3 – label user variable K2LB Qa – label subroutine as Qa|
|Comment character (‘)||MCode allows for comments to be inserted in your program code. The comment character for the MCode language is the Apostrophe (‘). The device will ignore the text string following the apostrophe. Please note that the maximum length of a single line of program code is 64 characters, this includes program text, spaces and comments.||‘ this is a comment PG 100 ‘ enter program mode at address 100 LB G1 ‘label program G1|
Proper structuring of your MCode program will ensure your ability to work efficiently and will aid in trouble shooting your program. The figure below illustrates how your program can be blocked out to group the global system declarations, the main program body and the subroutines.
Bring it all together
Here is a simple program that, when executed, perform a move and print the position. Once entered, from immediate mode enter “EX G1” to run the program.
PG 100‘Enter program mode at address 100
LB G1′Label program G1
P=0‘Set position counter to 0
MR 200000′move relative 200000
H‘Hold until motion is complete
PR P’Print position to terminal
E‘End program G1
PG’Exit program mode
When it comes to your form, fit and function requirements, don’t settle. Get precisely what you need working with us. We know motion.
Contact us with any questions about how we can help you with your motion application or for assistance with your IMS products.