*-----------------------------------------------------------------------------
*  User M functions
*-----------------------------------------------------------------------------
*  Module Description
*    - M functions number = 10.
*    - When M function is running, NC will stop and waiting for M function finish.
*    - When M function is running, to push NC stop button, the output = 0.
*    - To use this function, you need double check everything(input, ouput...),
*      otherwise, don't setting Z axis brake output number into 'OUTPUT' field,
*      it is very danger!!
*    - After modify, please re-compiler!!
**
*    - PLC:\MP\MP_Data.tab for I/O setting:
*      NR  Symbol              Comment Value                       Value
*      137                                                         +0
*      138                     --- User M function                 +0
*      139                                                         +0
*      140 DG_user_M_output_1  User M function output 1  number [] +64
*      141 DG_user_M_output_2  User M function output 2  number [] +65
*      142 DG_user_M_output_3  User M function output 3  number [] +66
*      143 DG_user_M_output_4  User M function output 4  number [] +67
*      144 DG_user_M_output_5  User M function output 5  number [] +68
*      145 DG_user_M_output_6  User M function output 6  number [] +69
*      146 DG_user_M_output_7  User M function output 7  number [] +70
*      147 DG_user_M_output_8  User M function output 8  number [] +71
*      148 DG_user_M_output_9  User M function output 9  number [] +72
*      149 DG_user_M_output_10 User M function output 10 number [] +73
*      150 DG_user_M_output_11 User M function output 11 number [] +74
*      151 DG_user_M_output_12 User M function output 12 number [] +75
*      152 DG_user_M_output_13 User M function output 13 number [] +76
*      153 DG_user_M_output_14 User M function output 14 number [] +77
*      154 DG_user_M_output_15 User M function output 15 number [] +78
*      155 DG_user_M_output_16 User M function output 16 number [] +79
*      156 DG_user_M_input_1   User M function input 1  number  [] +192
*      157 DG_user_M_input_2   User M function input 2  number  [] +193
*      158 DG_user_M_input_3   User M function input 3  number  [] +194
*      159 DG_user_M_input_4   User M function input 4  number  [] +195
*      160 DG_user_M_input_5   User M function input 5  number  [] +196
*      161 DG_user_M_input_6   User M function input 6  number  [] +197
*      162 DG_user_M_input_7   User M function input 7  number  [] +198
*      163 DG_user_M_input_8   User M function input 8  number  [] +199
*      164 DG_user_M_input_9   User M function input 9  number  [] +200
*      165 DG_user_M_input_10  User M function input 10 number  [] +201
*      166 DG_user_M_input_11  User M function input 11 number  [] +202
*      167 DG_user_M_input_12  User M function input 12 number  [] +203
*      168 DG_user_M_input_13  User M function input 13 number  [] +204
*      169 DG_user_M_input_14  User M function input 14 number  [] +205
*      170 DG_user_M_input_15  User M function input 15 number  [] +206
*      171 DG_user_M_input_16  User M function input 16 number  [] +207
*
*    - Marker for output: (M6001 ~ M6016)
*      M6001 --> User M function 1 output
*      M6002 --> User M function 2 output
*        :                :
*      M6016 --> User M function 16 output
*
*    - Marker for input: (M6101 ~ M6116)
*      M6101 --> User M function 1 input
*      M6102 --> User M function 2 input
*        :                :
*      M6116 --> User M function 16 input
*
*    - Example 1:
*      User M function 1 code --> M501
*        When M501 running(output=1), waiting for input=1, then finish.
*        Create PLC:\NC_MACRO\User_M\M501.H
*           0  BEGIN PGM M501 MM
*           1  FN 17: SYSWRITE ID 2000 NR10 IDX6001 =+1
*           2  LBL 1
*           3  FN 18: SYSREAD Q1 = ID2000 NR10 IDX6101
*           4  FN 12: IF +Q1 LT +1 GOTO LBL 1
*           5  FN 17: SYSWRITE ID 2000 NR10 IDX6001 =+0
*           6  LBL 99
*           7  END PGM M501 MM
*
*    - Example 2:
*      User M function 2 code --> M502
*        When M502 running(output=1), delay 5 seconds, then finish.
*        Create PLC:\NC_MACRO\User_M\M502.H
*           0  BEGIN PGM M502 MM
*           1  FN 17: SYSWRITE ID 2000 NR10 IDX6002 =+1
*           2  CYCL DEF 9.0 DWELL TIME
*           3  CYCL DEF 9.1 DWELL5
*           4  FN 17: SYSWRITE ID 2000 NR10 IDX6002 =+0
*           5  LBL 99
*           6  END PGM M502 MM
*
*    - Example 3:
*      User M function 3 code --> M503
*        When M503 running, output=1, input_1=1, input_2=1, output=0, input_3=1, then finish.
*        Create PLC:\NC_MACRO\User_M\M503.H
*           0  BEGIN PGM M503 MM
*           1  ;=== Workpiece unclamping ==========
*           2  FN 17: SYSWRITE ID 2000 NR10 IDX6003 =+1
*           3  ;=== Waiting for clamped singal ====
*           4  LBL 1
*           5  FN 18: SYSREAD Q1 = ID2000 NR10 IDX6103
*           6  FN 12: IF +Q1 LT +1 GOTO LBL 1
*           7  ;=== Waiting for operator checked ==
*           8  LBL 2
*           9  FN 18: SYSREAD Q2 = ID2000 NR10 IDX6104
*           10 FN 12: IF +Q1 LT +1 GOTO LBL 2
*           11 ;=== Workpiece clamping ============
*           12 FN 17: SYSWRITE ID 2000 NR10 IDX6003 =+0
*           13 ;=== Waiting for unclamped singal ==
*           14 LBL 3
*           15 FN 18: SYSREAD Q3 = ID2000 NR10 IDX6105
*           16 FN 12: IF +Q1 LT +1 GOTO LBL 3
*           17 LBL 99
*           18 END PGM M503 MM
*
*    - Example 4:
*      User M function 4 code --> M504
*        When M504 running, Z axis goes to 0 position with feedrate=10m/min, then finish.
*        Create PLC:\NC_MACRO\User_M\M504.H
*           0  BEGIN PGM M504 MM
*           1  L Z+0 F10000
*           2  LBL 99
*           3  END PGM M504 MM
*
*-----------------------------------------------------------------------------

#define /s    KL_user_M_max                            K+10
#define /s    ML_user_M_output_initial                 M
#define /s    WL_index_number                          W
#define /s    ML_user_M_output                         M6001[16]
#define /s    ML_user_M_input                          M6101[16]
#define /s    WL_read_lines_number                     W
#define /s    DL_output_data                           D
#define /s    DL_input_data                            D

GLOBAL User_M_functions

;----------------------------
LBL User_M_functions
;----------------------------

  LN     MG_machine_parameter_are_read
  EMT

  L      KL_user_M_max
  ==     K+0
  EMT

  L      K-1
  =      WL_index_number

  REPEAT
    INC    WL_index_number

    L      DG_user_M_output_1[WL_index_number]
    -      K+400
    =      DL_output_data

    L      DG_user_M_input_1[WL_index_number]
    -      K+400
    =      DL_input_data

    L      ML_user_M_output[WL_index_number]
    =      O400[DL_output_data]

    L      I400[DL_input_data]
    =      ML_user_M_input[WL_index_number]

    L      WL_index_number
    >=     KL_user_M_max
  UNTILT

  LN     MG_control_operational
  IFT
    L      K+0
    D=     ML_user_M_output[K0]
  ENDI

EM
