# FPGA Implementation of IEC-61131-3-Based Hardware Aided Counters for PLC
## 2 Theoretical Background
### The Counter CALL  
The standard allows three ways of calling counter operations in IL. The first is to call a function block with the current parameters passed in parentheses [1]--e.g.,:  
CAL CNT (CU:=IN_CU, CD:=IN_CD,R:=IN_RES,  
PV:=IN_PV, LD:=IN_SET, CV=>CNT_VAL)  
This type of call may have a complete list of assigned parameters or may have only a portion of the parameters assigned. In this case, the unassigned parameters take the values stored previously ([1]: Table 42, page 105). This way of calling function blocks is available in CoDeSys [16], Concept [17] software, and also in the IEC counters of SIMATIC S7 PLCs [18]. The next two ways are more ambiguous and require more commentary. The second one consists in preparing suitable data in particular fields of the counter structure, and then calling the function updating the counter without any parameters using the CAL command. Not all fields of the counter structure have to be filled in immediately before the function is called. If some values are not assigned to the structure fields, the function uses the values of the structure fields stored earlier. This way of calling the counter function block in Instruction Language (IL) is shown in Listing 2. As can be seen, there is a set of instructions (LD) for loading data into the current result (CR) register and storing data to the individual fields that make up the counter structure (ST). All these instructions merely fill in the relevant fields. The counter functionality is realized only when the CAL instruction is executed. This method of calling counter operations is available in the CoDeSys [16], Concept [17], and ISAGRAF [19] environments. However, the provisions of the standard allow also using so-called short IL operators. When, in fact, the operation updating the state of the counter is performed, since the standard allows the use of operators (CU, CD, R, LD and PV). In this case, each short operand updates the counter state, which may improve the readability and conciseness of the program code. However, the standard says that each use of the short command is interpreted as sending a value from CR to the appropriate field of the counter structure, followed by a call (CAL) to the function that updates the counter. For the system designer it is a big difference that significantly affects the implementation of the counter block and also has a big impact on the execution time of this block. In the Siemens Simatic S7-300/400 controllers the basic counters for this system (Simatic Counters) do not form a function block, so there is no problem to use only counter elements that are necessary at the moment. It may seem that in this case the processing time of the counter is shorter than the longest one. Among the implementations of the standard known to the authors,  
Figure 2: Minimum representation of a counter data structure. only Concept allows using operator based programming. Other implementations, such as CoDeSys and ISAFRAF, do not allow this method of calling counter functions. **Listing 2. ** Counter block calling using CAL instruction. LD IN_CU  
ST CNT.CU ;CR -> CNT.CU  
LD IN_CD  
ST CNT.CD :CR -> CNT.CD  
LD IN_RES  
ST CNT.R ;CR -> CNT.R  
LD IN_PV  
ST CNT.PV ;CR -> CNT.PV  
LD IN_SET  
ST CNT.LD ;CR -> CNT.LD  
CAL CNT ;Counter Block Execution  
;IF CU Positive Edge THEN CNT.CV+1 -> CNT.CV  
;IF CD Positive Edge THEN CNT.CV-1 -> CNT.CV  
;IF LD THEN CNT.PV -> CNT.CV  
;IF R THEN 0 -> CNT.CV  
LD CNT.CV  
ST CNT.VAL ;CNT.CV -> CR  
In the field of the short operators, the standard is somehow weak. The example code is presented in Listing 3. It is easy to imagine now that the counter does not require the existence of the CAL instruction in the program, which actually implements the counter's functions based on the information gathered earlier, but simply executes individual instructions that exist in the program.