Entry type: FAQ, Entry ID: 39309294, Entry date: 05/06/2016
(1)
Rate

In STEP 7 (TIA Portal), how can you count forwards and backwards in a bandwidth between -2147483648 and +2147483647?

  • Entry
  • Associated product(s)
Using the FB "Dcount" from the attached library, you can count forwards and backwards in the above-mentioned bandwidth.

Instructions
Fig. 1 shows the call of the function block "Dcount" in the OB "CountMain" with the parameterization of the variable of the the data block "Count". The program for counting was created in the FB "Dcount" and includes the German and English comments.
 


Fig. 1
  
Counting forwards and backwards in a bandwidth between -2147483648 and +2147483647 is done by an edge change at the "up" and "down" inputs.
  • With a positive edge at the "up" input the value at the "count" output is incremented by "1".
  • With a positive edge at the "down" input the value at the "count" output is decremented by "1".
  • With a positive edge at the "lPreset" input a preset value at the "preset" input is transferred to the "count" output.
  • You can input a comparison value at the "compare" input.
  • If during counting the "count" output reaches a value that is equal to or greater than the value entered at the "compare" input, the "qCompare" output receives the signal "1".

Note
The FB "Dcount" described above was created in the programming language SCL and for counting in the bandwidth it receives a series of IF instructions, for example:

IF #statCount >= #compare THEN
#qCompare := true;
ELSE
#qCompare := false;
END_IF;

To optimize the program even further you can also use the "Greater than Equal to" instruction instead of the IF instruction, for example:

#qCompare := #statCount >= #compare;

The table below shows the input and output parameters with the associated data type in each case for the FB "Dcount".
 

Parameter
Interface
Data type
Description
upInputBoolCounting up
downInputBoolCounting down
compareInputDIntInput for the comparison value
presetInputDIntInput for presetting the counter
lPresetInputBoolEnabling the presetting
countOutputDIntOutput of the counter value
qCompareOutputBoolOutput "1" when "count" >= "compare"

Sample application
Boxes are transported from A to B on a conveyor belt.

  • Scan 1 detects the boxes placed on the conveyor and with the input E0.0 the value at the "count" output is incremented by "1".
  • Scan 2 detects the boxes removed from the conveyor and with the input E0.1 the value at the "count" output is decremented by "1".
  • If boxes are removed or sorted out during the course of the transport and are not detected by Scan 2, then the value at the "count" output increases due to the difference by "1" for each box removed. For example, if a value of 20 boxes is set at the "compare" parameter and the "count" output reaches the value 20, the output A0.0 receives the signal "1". This is the difference value of the boxes removed in addition or lost.

Through a plug-in (not included in the download) you can program stopping of the conveyor belt with reference to the signal at output A0.0.
 


Fig. 2

You can use the library for the S7-1200/S7-1500 and for the S7-300. The Master Copies folder contains two subfolders. Each subfolder contains the FB "Dcount" with its instance DB, the OB "CountMain" and the global DB "Count".

  • If you are using the S7-1200 or S7-1500, you drag-and-drop the blocks from the subfolder "S7-1200/S7-1500" to the program folder.
  • If you are using S7-300, you drag-and-drop the blocks from the subfolder "S7-300" to the program folder. You can also use the program for the S7-400.

Download
The attached download includes the STEP 7 (TIA Portal) library "39309294_Dcount_Lib_STEP7_TIA_Portal_V13.zip" with the blocks described above. Copy the file into a separate directory and then start the file with a double-click. The STEP 7 (TIA Portal) library is then extracted automatically with all the associated subdirectories. Then you can use STEP 7 (TIA Portal) to open and edit the unpacked library.

 Registration required 39309294_Dcount_Lib_STEP7_TIA_Portal_V13.zip (1.1 MB)

Creation Environment
Fig. 1 and the download in this FAQ response were created with STEP 7 (TIA Portal) V13+SP1.