The text of a manual · part 9 of 11
MELSEC FX Family Programmable Logic Controllers Beginners Manual, pages 81 to 90
Pages 81 to 90 of 106
The original document is the PDF (969 KB). This is its text as extracted, unedited; tables and drawings read best in the PDF, and each page heading opens it at that page.
For the Mitsubishi FX2NC-32MT
Page 81
More Advanced Programming
5.2
Instructions for Moving Data
Instructions for Moving Data The PLC uses data registers for storing measurements, output values, intermediate results of operations and table values. The controller’s math instructions can read their operands directly from the data registers and can also write their results back to the registers if you want. However, these instructions are also supported by additional “move" instructions, with which you can copy data from one register to another and write constant values to data registers.
5.2.1
Moving individual values with the MOV instruction The MOV instruction “moves” data from the specified source to the specified destination.
NOTE
Note that despite its name this is actually a copy process – it does not delete the data from the source location. Ladder Diagram
0
Instruction List
MOV D10 D200 ���
0 MOV
���
D10
D200
���
���
쐃 Data source (this can also be a constant) 쐇 Data destination In the example the value in data register D10 will be copied to register D200 when input X1 is on. This results in the following signal sequence: X001
D200
2271
125
963
5384
D10
5384
963 t
The contents of the data source will be copied to the data destination as long as the input condition evaluates true. The copy operation does not change the contents of the data source.
When the input condition is no longer true the instruction will no longer change the contents of the data destination.
Pulse-triggered execution of the MOV instruction In some applications it is better if the value is written to the destination in one program cycle only. For example, you will want to do this if other instructions in the program also write to the same destination or if the move operation must be performed at a defined time. If you add a “P” to the MOV instruction (MOVP) it will only be executed once, on the rising edge of the signal pulse generated by the input condition.
FX Beginners Manual
5–7
Page 82
Instructions for Moving Data
More Advanced Programming
In the example below the contents of D20 are written to data register D387 when the state of M110 changes from “0" to ”1". Ladder Diagram
Instruction List
M110 0
0 LD 1 MOVP
MOVP D20 D387
D20
M110 D387
After this single operation has been performed copying to register D387 stops, even if the M110 remains set. The signal sequence illustrates this: M110
4700
D20
D387
6800
3300
4700
3300 t
The contents of the data source are only copied to the destination on the rising pulse of the input condition.
Moving 32-bit data To move 32-bit data just prefix a D to the MOV instruction (DMOV): Ladder Diagram
Instruction List
X010 0
DMOV C200 D40
0 LD 1 DMOV
X010 C200
D40
When input X010 is on the current value of 32-bit counter C200 is written to data registers D40 and D41. D40 contains the least significant bits. As you might expect, there is also a pulse-triggered version of the 32-bit DMOV instruction: Ladder Diagram
Instruction List
M10 0
DMOVP D10 D610
0 LD 1 DMOVP D10
M10 D610
When relay M10 is set the contents of registers D10 and D11 are written to registers D610 and D611.
5–8
MITSUBISHI ELECTRIC
Page 83
More Advanced Programming
5.2.2
Instructions for Moving Data
Moving groups of bit devices The previous section showed how you can use the MOV instruction to write constants or the contents of data registers to other data registers. Consecutive sequences of relays and other bit devices can also be used to store numerical values, and you can copy them as groups with applied instructions. To do this you prefixing a “K” factor to the address of the first bit device, specifying the number of devices you want to copy with the operation. Bit devices are counted in groups of 4, so the K factor specifies the number of these groups of 4. K1 = 4 devices, K2 = 8 devices, K3 = 12 devices and so on. For example, K2M0 specifies the 8 relays from M0 through M7. The supported range is K1 (4 devices) to K8 (32 devices). Examples for addressing groups of bit devices: –
K1X0:
4 inputs, start at X0
(X0 to X3)
–
K2X4:
8 inputs, start at X4
(X4 to X13, octal notation)
–
K4M16: 16 relays, start at 16
(M16 to M31)
–
K3Y0:
12 outputs, start at Y0
(Y0 to X13, octal notation)
–
K8M0:
32 relays, start at M0
(M0 to M31)
Addressing multiple bit devices with a single instruction makes programming quicker and produces more compact programs. The following two examples both transfer the signal states of relays M0 – M4 to outputs Y10 – Y14: M0
Y010 M1
M8000
Y011
MOV K1M0 K1Y010
M2 Y012 M3 Y013
If the destination range is smaller than the source range the excess bits are simply ignored (see the following illustration, top example). If the destination is larger than the source “0” is written to the excess devices. Note that when this happens the result is always positive because bit 15 is interpreted as the sign bit (lower example in the following illustration). Bit 15
0
Bit 0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
Sign bit (0: positive, 1: negative) MOV D0 K2 M0 These relays will not be changed
M15 M14 M13 M12 M11 M10
M9
M8
0
1
0
1
0
1
0
1
M7
M6
M5
M4
M3
M2
M1
M0
1
0
1
0
MOV K2 M0 D1 Sign bit (0: positive, 1: negative)
0 Bit 15
FX Beginners Manual
0
0
0
0
0
0
0
0
1
0
1 Bit 0
5–9
Page 84
Instructions for Moving Data
5.2.3
More Advanced Programming
Moving blocks of data with the BMOV instruction The MOV instruction described in Chapter 5.2.1 can only write single 16 or 32 bit values to a destination. If you want, you can program multiple sequences of MOV instructions to move contiguous blocks of data. However, it is more efficient to use the BMOV (Block MOVe) instruction, which is provided specifically for this purpose. Ladder Diagram
0
Instruction List 0 BMOV
BMOV D10 D200 K5 ���
���
���
D10
D200
K5
���
���
���
쐃 Data source (16-bit device, first device in source range) 쐇 Data destination (16-bit device, first device in destination range) 쐋 Number of elements to be moved (max. 512) The example above works as follows:
BMOV D10 D200 K5
D 10 D 11 D 12 D 13 D 14
1234 5678 -156 8765 4321
1234 5678 -156 8765 4321
D 200 D 201 D 202 D 203 D 204
5 data registers
BMOV also has a pulse-triggered version, BMOVP (see section 5.2.1 for details on pulse-triggered execution). Blocks of bit devices: When you move blocks of bit devices with BMOV the K factors of the data source and the data destination must always be identical. Example
BMOV K1M0 K1Y0 K2 M0 M1 M2 M3 M4 M5 M6 M7
5 – 10
0 1 1 0 1 0 1 0
0 1 1 0 1 0 1 0
Y000 Y001 Y002 Y003 Y004 Y005 Y006 Y007
This copies 2 blocks with 4 bit devices each.
MITSUBISHI ELECTRIC
Page 85
More Advanced Programming
5.2.4
Instructions for Moving Data
Copying source devices to multiple destinations (FMOV) The FMOV (Fill MOVe) instruction copies the contents of a word or double word device or a constant to multiple consecutive word or double word devices. It is generally used to delete data tables and to set data registered to a predefined starting value. Ladder Diagram
0
Instruction List 0 FMOV
FMOV D4 D250 K20 ���
���
���
D4
D250
K20
���
���
���
쐃 Data to be written to the target devices (constants can also be used here) 쐇 Data destination (first device of the destination range) 쐋 Number of elements to be written in the destination range (max. 512) The following example writes the value “0” to 7 elements:
FMOV K0 D10 K7
0
0 0 0 0 0 0 0
D 10 D 11 D 12 D 13 D 14 D 15 D 16
7 data words
Here too, FMOV has a pulse-triggered version, FMOVP (see section 5.2.1 for details on pulse-triggered execution). You can also transfer 32-bit data by prefixing the instruction with “D” (DFMOV and DFMOVP).
FX Beginners Manual
5 – 11
Page 86
Instructions for Moving Data
5.2.5
More Advanced Programming
Exchanging data with special function modules You can add expansion modules to increase the number of inputs and outputs available to all base units of the MELSEC FX series except the FX1S models. In addition to this you can also supplement the controller’s functions by adding so-called “special function modules” – for example for reading analog signals for currents and voltages, for controlling temperatures and for communicating with external equipment. The digital I/O expansion modules do not require special instructions; the additional inputs and outputs are handled in exactly the same way as those in the base unit. Communication between the base unit and special function modules is performed with two special applied instructions: the FROM and TO instructions. Each special function module has a memory range assigned as a buffer for temporary storage of data, such as analog measurement values or received data. The base unit can access this buffer and both read the stored values from it and write new values to it, which the module can then process (settings for the module’s functions, data for transmission etc).
Base unit
Special function module
Device memory
Buffer memory TO
FROM
The buffer memory can have up to 32,767 individual addressable memory cells, each of which can store 16 bits of data. The functions of the buffer memory cells depends on the individual special function module – see the module’s documentation for details.
Buffer memory address 0 Buffer memory address 1 Buffer memory address 2 : : Buffer memory address n-1 Buffer memory address n
The following information is required when you use the FROM and TO instructions:
5 – 12
–
The special function module to be read from or written to
–
The address of the first buffer memory cell to be read from or written to
–
The number of buffer memory cells to be read from or written to
–
The location in the base unit where the data from the module is to be stored or containing the data to be written to the module
MITSUBISHI ELECTRIC
Page 87
More Advanced Programming
Instructions for Moving Data
Special function module address Since you can attach multiple special function modules to a single controller each module needs to have a unique identifier so that you can address it to transfer data to and from it. Each module is automatically assigned a numerical ID in the range from 0 – 7 (you can connect a maximum of 8 special function modules). The numbers are assigned consecutively, in the order in which the modules are connected to the PLC.
24- SLD
24+
24-
24+
L-
I+
VI-
VI-
V+
V+
L+
24-
24+
I+ L+
SLD L-
I+
SLD
I+
VI-
VI-
V+
V+
FG
V+
FG
L+
V+
FX2N -4AD-PT SLD
L+
I+
VI-
VI-
V+
L-
V+
FG I+
FX2N-4AD-TC
L-
I+
FX2N-4DA
I+
VI-
VI-
FX2N -4DA
D/A
Special function module 0
Module 1
Module 2
Starting address in the buffer memory Every single one of the 32,767 buffer addresses can be addressed directly in decimal notation in the range from 0 – 32,767 (FX1N: 0 – 31). When you access 32-bit data you need to know that the memory cell with the lower address stores the less significant 16 bits and the cell with the higher address stores the more significant bits. Buffer address n+1
Buffer address n
More significant 16 bits
Less significant 16 bits 32-Bit-Wert
This means that the starting address for 32-bit data is always the address containing the less significant 16 bits of the double word. Number of data units to be transferred The quantity of data is defined by the number of data units to be transferred. When you execute a FROM or TO instruction as a 16-bit instruction this parameter is the number of words to be transferred. In the case of the 32-bit versions DFROM and DTO the parameter specifies the number of double words to be transferred. 16-bit instruction Units of data: 5
32-bit instruction Units of data: 2
D100
Adr. 5
D100
Adr. 5
D101
Adr. 6
D101
Adr. 6
D102
Adr. 7
D102
Adr. 7
D103
Adr. 8
D103
Adr. 8
D104
Adr. 9
D104
Adr. 9
FX Beginners Manual
5 – 13
Page 88
Instructions for Moving Data
More Advanced Programming
The value you can enter for the number of data units depends on the PLC model you are using and whether you are using the 16-bit or 32-bit version of the FROM instruction: Valid range for no. of data units to be transferred
PLC Model
16-Bit Instruction (FROM, TO)
32-Bit Instruction (DFROM, DTO)
FX2N
1 bis 32
1 bis 16
FX2NC
1 bis 32
1 bis 16
FX3U
1 bis 32767
1 bis 16383
Data destination or source in the base unit In most cases you will read data from registers and write it to a special function module, or copy data from the module’s buffer to data registers in the base unit. However, you can also use outputs, relays and the current values of timers and counters as data sources and destinations. Pulse-triggered execution of the instructions If you add a P suffix to the instructions the data transfer is initiated by pulse trigger (for details see the description of the MOV instruction in section 5.2.1). How to use the FROM instruction The FROM instruction is used to transfer data from the buffer of a special function module to the controller base unit. Note that this is a copy operation – the contents of the data in the module buffer are not changed. Ladder Diagram
0
Instruction List
FROM K0 K9 D0 K1 ���
���
���
���
0 FROM
K0
K9
D0
K1
���
���
���
���
쐃 Special function module address (0 to 7) 쐇 Starting address in buffer (FX1N: 0 – 31, FX2N, FX2NC and FX3U: 0 – 32,766). You can use a constant or a data register containing the value. 쐋 Data destination in the controller base unit 쐏 Number of data units to be transferred The example above uses FROM to transfer data from an FX2N-4AD analog/digital converter module with the address 0. The instruction reads the current value of channel 1 from buffer address 9 and writes it to data register D0. The next example shows how the 32-bit version of the instruction is used to read data from address 2 in the special function module. The instruction reads 4 double words starting at buffer address 8 and writes them to data registers D8 – D15.
0
DFROM K2 K8 D8 K4
The next example illustrates the use of the pulse triggered version, FROMP. Here the contents of the four buffer addresses 0 – 3 are only transferred to data registers D10 – D13 when the signal state of the input condition changes from “0” to “1”.
0
5 – 14
FROMP K0 K0 D10 K4
MITSUBISHI ELECTRIC
Page 89
More Advanced Programming
Compare Instructions
How to use the TO instruction The TO instruction transfers data from the controller base unit to the buffer of a special function module. Note that this is a copy operation, it does not change the data in the source location. Ladder Diagram
0
Instruction List
TO K0 K1 D0 K1 0 TO ���
���
���
���
K0
K1
D0
K1
���
���
���
���
쐃 Special function module address (0 – 7) 쐇 Starting address in buffer (FX1N: 0 – 31, FX2N, FX2NC and FX3U: 0 – 32,766). You can use a constant or a data register containing the value. 쐋 Data source in the controller base unit 쐏 Number of data units to be transferred In the example above the contents of data register D0 are copied to the buffer address 1 of special function module number 0.
5.3
Compare Instructions Checking the status of bit devices like inputs and relays can be achieved with basic logic instructions because these devices can only have two states, “0” and “1”. However, you will also often want to check the contents of word devices before doing something – for example switching on a cooling fan when a specified setpoint temperature is exceeded. The controllers of the MELSEC FX family provide a number of different ways to compare data.
5.3.1
The CMP instruction CMP compares two numerical values, which can be constants or the contents of data registers. You can also compare the current values of timers and counters. Depending on the result of the comparison (greater than, less than or equal) one of three bit devices is set. Ladder Diagram
0
Instruction List
CMP D0 K100 M0 ���
���
���
���
0 LD 1 CMP
��� .... D0
K100
M0
���
���
���
쐃 Input condition 쐇 First value to be compared 쐋 Second value to be compared 쐏 First of three consecutive relays or outputs, which are set (signal status “1”) depending on the result of the comparison: 1. Device 1: ON if Value 1 > Value 2 2. Device 2: ON if Value 1 = Value 2 3. Device 3: ON if Value 1 < Value 2 In this example the CMP instruction controls relays M0, M1 and M2. M0 is “1” if the contents of D0 is greater than 100; M1 is “1” if the contents of D0 is precisely 100 and M2 is “1” if D0 is less than 100. The state of the three bit devices is maintained even after the input condition has been switched off because their last state is stored.
FX Beginners Manual
5 – 15
Page 90
Compare Instructions
More Advanced Programming
To compare 32-bit data you just use DCMP instead of CMP: Ladder Diagram
0
Instruction List
DCMP D0 D2 M0
0 LD 1 DCMP
.... D0
D2
M0
In the example above the contents of D0 and D1 are compared with the contents of D2 and D3. The handling of the three bit devices indicating the result of the comparison is exactly the same as for the 16-bit version of the instruction. Application example It is easy to create a two-point control loop with the CMP instruction: Ladder Diagram
Instruction List
M8000 0
CMP D20 K22 M20 M20
8
RST Y000
0 LD 1 CMP 8 LD 9 RST 10 LD 11 SET
M8000 D20 M20 Y000 M22 Y0001
K22
M20
M22 10
SET Y000
In this example the CMP instruction is executed cyclically. M8000 is always “1” when the PLC is executing the program. Register D20 contains the value for the current room temperature. Constant K22 contains the setpoint value of 22°C. Relays M20 and M22 show when the temperature goes higher or lower than the setpoint. If the room is too warm output Y0 is switched off. If the temperature is too low M22 switches output Y0 on again. This output could be used to control a pump for adding hot water, for example.
5 – 16
MITSUBISHI ELECTRIC