Understanding the High-Speed Counter Instructions

High-speed counters count high-speed events that cannot be controlled at CPU scan rates, and can be configured for up to twelve different modes of operation. The maximum counting frequency of a high-speed counter is dependent upon your CPU type.

Each counter has dedicated inputs for clocks, direction control, reset, and start, where these functions are supported. For the two-phase counters, both clocks may run at their maximum rates. In quadrature modes, an option is provided to select one times (1x) or four times (4x) the maximum counting rates. All counters run at maximum rates without interfering with one another.

This topic discusses the following subjects:

Using the High-Speed Counter

Understanding the Detailed Timing for the High-Speed Counters

Connecting the Input Wiring for the High-Speed Counters

Addressing the High-Speed Counters (HC)

Understanding the Different High-Speed Counters

Selecting the Active State and 1x/4x Mode

Control Byte

HSC Modes

Setting Current Values and Preset Values

Status Byte

HSC_Interrupts

Using the High-Speed Counter

Back to Top

Typically, a high-speed counter is used as the drive for a drum timer, where a shaft rotating at a constant speed is fitted with an incremental shaft encoder. The shaft encoder provides a specified number of counts per revolution and a reset pulse that occurs once per revolution. The clock(s) and the reset pulse from the shaft encoder provide the inputs to the high-speed counter. The high-speed counter is loaded with the first of several presets, and the desired outputs are activated for the time period where the current count is less than the current preset. The counter is set up to provide an interrupt when the current count is equal to preset and also when reset occurs.

As each current-count-value-equals-preset-value interrupt event occurs, a new preset is loaded and the next state for the outputs is set. When the reset interrupt event occurs, the first preset and the first output states are set, and the cycle is repeated.

Because the interrupts occur at a much lower rate than the counting rates of the high-speed counters, precise control of high-speed operations can be implemented with relatively minor impact to the overall scan cycle of the programmable logic controller. The method of interrupt attachment allows each load of a new preset to be performed in a separate interrupt routine for easy state control, making the program very straightforward and easy to follow. Of course, all interrupt events can be processed in a single interrupt routine.

Understanding the Detailed Timing for the High-Speed Counters

Back to Top

The following timing diagrams show how each counter functions according to mode. The operation of the reset and start inputs is shown in a separate timing diagram and applies to all modes that use reset and start inputs. In the diagrams for the reset and start inputs, both reset and start are shown with the active state programmed to a high level.

Operation Example with Reset and without Start

Operation Example with Reset and Start

Operation Example of Modes 0, 1, and 2

Operation Example of Modes 3, 4, and 5

When you use counting modes 6, 7, or 8 and a rising edge on both the up clock and down clock inputs occurs within 0.3 microseconds of each other, the high-speed counter may see these events as happening simultaneously. If this happens, the current value is unchanged and no change in counting direction is indicated. As long as the separation between rising edges of the up and down clock inputs is greater than this time period, the high-speed counter captures each event separately. In either case, no error is generated and the counter maintains the correct count value.

Operation Example of Modes 6, 7, and 8

Operation Example of Modes 9,10, and 11 (Quadrature 1x Mode)

Operation Example of Modes 9,10, and 11 (Quadrature 4x Mode)

Connecting the Input Wiring for the High-Speed Counters

Back to Top

The tables below show the inputs used for the clock, direction control, reset, and start functions associated with the high-speed counters.

Dedicated Inputs for High-Speed Counters  
High-Speed Counter Inputs Used
HSC0 I0.0, I0.1, 0.2
HSC1 I0.6, I0.7, I1.0, I1.1
HSC2 I1.2, I1.3, I1.4, I1.5
HSC3 I0.1
HSC4 I0.3, I0.4, I0.5
HSC5 I0.4

There is some overlap in the input point assignments for some high-speed counters and edge interrupts. The same input cannot be used for two different functions; however, any input not being used by the present mode of its high-speed counter can be used for another purpose. For example, if HSC0 is being used in mode 2 which uses I0.0 and I0.2, I0.1 can be used for edge interrupts or for HSC3.

If a mode of HSC0 is used that does not use input I0.1, then this input is available for use as either HSC3 or edge interrupts. Similarly, if I0.2 is not used in the selected HSC0 mode, this input is available for edge interrupts; and if I0.4 is not used in the selected HSC4 mode, this input is available for HSC5. Note that all modes of HSC0 always use I0.0 and all modes of HSC4 always use I0.3, so these points are never available for other uses when these counters are in use.

Input Point Assignments for High-Speed Counters and Edge Interrupts                            
Input Point (I)                            
Element 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 1.0 1.1 1.2 1.3 1.4 1.5
HSC0 x x x                      
HSC1             x x x x        
HSC2                     x x x x
HSC3   x                        
HSC4       x x x                
HSC5         x                  
Edge Interrupts x x x x                    

HSC Modes

Back to Top

Addressing the High-Speed Counters (HC)

Back to Top

To access the count value for the high-speed counter, you specify the address of the high-speed counter, using the memory type (HC) and the counter number (such as HC0). The current value of the high-speed counter is a read-only value and can be addressed only as a double word (32 bits), as shown below.

Format: HC[high-speed counter number] as for example HC2.

Accessing the High-Speed Counter Current Values

Understanding the Different High-Speed Counters

Back to Top

All counters function the same way for the same counter mode of operation. There are four basic types of counter modes. Note that every mode is not supported by every counter. You can use each type: without reset or start inputs, with reset and without start, or with both start and reset inputs.

When you activate the reset input, it clears the current value and holds it cleared until you de-activate reset. When you activate the start input, it allows the counter to count. While start is de-activated, the current value of the counter is held constant and clocking events are ignored. If reset is activated while start is inactive, the reset is ignored and the current value is not changed. If the start input becomes active while the reset input is active, the current value is cleared.

You must select the counter mode before a high-speed counter can be used. You can do this with the HDEF instruction (High-Speed Counter Definition). HDEF provides the association between a high-speed counter (HSCx) and a counter mode. You can only use one HDEF instruction for each high-speed counter. Define a high-speed counter by using the first scan memory bit, SM0.1 (this bit is turned on for the first scan and is then turned off), to call a subroutine that contains the HDEF instruction.

Selecting the Active State and 1x/4x Mode

Back to Top

Four counters have three control bits that are used to configure the active state of the reset and start inputs and to select 1x or 4x counting modes (quadrature counters only). These bits are located in the control byte for the respective counter and are only used when the HDEF instruction is executed.

You must set these control bits to the desired state before the HDEF instruction is executed. Otherwise, the counter takes on the default configuration for the counter mode selected. The default setting of the reset input and the start input are active high, and the quadrature counting rate is 4x (or four times the input clock frequency). Once the HDEF instruction has been executed, you cannot change the counter setup unless you first place the CPU in the STOP mode.

        HDEF Control Bits

(used only when HDEF is executed)

HSC0 HSC1 HSC2 HSC4 Description
SM37.0 SM47.0 SM57.0 SM147.0 Active level control bit for Reset:

0 = Reset active high

1 = Reset active low

  SM47.1 SM57.1   Active level control bit for Start:

0 = Start active high

1 = Start active low

SM37.2 SM47.2 SM57.2 SM147.2 Counting rate selection for Quadrature counters:

0 = 4x counting rate

1 = 1x counting rate


Control Byte

Back to Top

Once you have defined the counter and the counter mode, you can program the dynamic parameters of the counter. Each high-speed counter has a control byte that allows the counter to be enabled or disabled; the direction to be controlled (modes 0, 1, and 2 only), or the initial counting direction for all other modes; the current value to be loaded; and the preset value to be loaded. Examination of the control byte and associated current and preset values is invoked by the execution of the HSC instruction. The following table describes each of these control bits.

SM Control Bits for HSC Parameters

HSC0 HSC1 HSC2 HSC3 HSC4 HSC5 Description
SM37.3 SM47.3 SM57.3 SM137.3 SM147.3 SM157.3 Counting direction control bit:

0 = count down

1 = count up

SM37.4 SM47.4 SM57.4 SM137.4 SM147.4 SM157.4 Write the counting direction to the HSC:

0 = no update

1 = update direction

SM37.5 SM47.5 SM57.5 SM137.5 SM147.5 SM157.5 Write the new preset value to the HSC:

0 = no update

1 = update preset

SM37.6 SM47.6 SM57.6 SM137.6 SM147.6 SM157.6 Write the new current value to the HSC:

0 = no update

1 = update current

SM37.7 SM47.7 SM57.7 SM137.7 SM147.7 SM157.7 Enable the HSC:

0 = disable the HSC

1 = enable the HSC


Setting Current Values and Preset Values

Back to Top

Each high-speed counter has a 32-bit current value and a 32-bit preset value. Both the current and the preset values are signed integer values. To load a new current or preset value into the high-speed counter, you must set up the control byte and the special memory bytes that hold the current and/or preset values. You must then execute the HSC instruction to cause the new values to be transferred to the high-speed counter. The following table describes the special memory bytes used to hold the new current and preset values.

In addition to the control bytes and the new preset and current holding bytes, the current value of each high-speed counter can be read using the data type HC (High-Speed Counter Current) followed by the number (0, 1, 2, 3, 4, or 5) of the counter. Thus, the current value is directly accessible for read operations, but can only be written with the HSC instruction described above.

Value to be Loaded HSC0 HSC1 HSC2 HSC3 HSC4 HSC5
New current SMD38 SMD48 SMD58 SMD138 SMD148 SMD158
New preset SMD42 SMD52 SMD62 SMD142 SMD152 SMD162

Status Byte

Back to Top

A status byte is provided for each high-speed counter that provides status memory bits that indicate the current counting direction, and whether the current value is greater or equal to the preset value. The table below defines these status bits for each high-speed counter.

Status Bits for HSC0, HSC1, HSC2, HSC3, HSC4, and HSC5            
HSC0 HSC1 HSC2 HSC3 HSC4 HSC5 Description
SM36.0 SM46.0 SM56.0 SM136.0 SM146.0 SM156.0 Not used
SM36.1 SM46.1 SM56.1 SM136.1 SM146.1 SM156.1 Not used
SM36.2 SM46.2 SM56.2 SM136.2 SM146.2 SM156.2 Not used
SM36.3 SM46.3 SM56.3 SM136.3 SM146.3 SM156.3 Not used
SM36.4 SM46.4 SM56.4 SM136.4 SM146.4 SM156.4 Not used
SM36.5 SM46.5 SM56.5 SM136.5 SM146.5 SM156.5 Current counting direction status bit:
0 = counting down;
1 = counting up
SM36.6 SM46.6 SM56.6 SM136.6 SM146.6 SM156.6 Current value equals preset value status bit:
0 = not equal; 1 = equal
SM36.7 SM46.7 SM56.7 SM136.7 SM146.7 SM156.7 Current value greater than preset value status bit:
0 = less than or equal;
1 = greater than

Note:

Status bits are valid only while the high-speed counter interrupt routine is being executed. The purpose of monitoring the state of the high-speed counter is to enable interrupts for the events that are of consequence to the operation being performed.

HSC Interrupts

Back to Top

All counter modes support an interrupt on current value equal to the preset value. Counter modes that use an external reset input support an interrupt on external reset activated. All counter modes except modes 0, 1, and 2 support an interrupt on a counting direction change. Each of these interrupt conditions may be enabled or disabled separately.

Note:

When you are using the external reset interrupt, do not attempt to load a new current value or disable, then re-enable the high-speed counter from within the interrupt routine that attached the event. A CPU fatal error condition can result.

To help you understand the operation of high-speed counters, the following descriptions of the initialization and operation sequences are provided. HSC1 is used as the model counter throughout these sequence descriptions. The initialization descriptions make the assumption that the S7-200 has just been placed in the RUN mode, and for that reason, the first scan memory bit is true. If this is not the case, remember that the HDEF instruction can be executed only one time for each high-speed counter after entering RUN mode. Executing HDEF for a high-speed counter a second time generates a run-time error and does not change the counter setup from the way it was set up on the first execution of HDEF for that counter.

Initialization Modes 0, 1, or 2

The following steps describe how to initialize HSC1 for Single Phase Up/Down Counter with Internal Direction (Modes 0, 1, or 2):

1.Use the first scan memory bit to call a subroutine in which the initialization operation is performed. Since you use a subroutine call, subsequent scans do not make the call to the subroutine, which reduces scan time execution and provides a more structured program.

2.In the initialization subroutine, load SMB47 according to the desired control operation. For example:
SMB47 = 16#F8 produces the following results:
Enables the counter
Writes a new current value
Writes a new preset value
Sets the direction to count up
Sets the start and reset inputs to be active high

3.Execute the HDEF instruction with the HSC input set to 1 and the MODE input set to 0 for no external reset or start, to 1 for external reset and no start, or to 2 for both external reset and start.

4.Load SMD48 (double word size value) with the desired current value (load with 0 to clear it).

5.Load SMD52 (double word size value) with the desired preset value.

6.In order to capture the current value equal to preset event, program an interrupt by attaching the CV = PV interrupt event (event 13) to an interrupt routine.

7.In order to capture an external reset event, program an interrupt by attaching the external reset interrupt event (event 15) to an interrupt routine.

8.Execute the global interrupt enable instruction (ENI) to enable interrupts.

9.Execute the HSC instruction to cause the S7-200 to program HSC1.

10.Exit the subroutine

Initialization Modes 3, 4, or 5

The following steps describe how to initialize HSC1 for Single Phase Up/Down Counter with External Direction (Modes 3, 4, or 5):

1.Use the first scan memory bit to call a subroutine in which the initialization operation is performed. Since you use a subroutine call, subsequent scans do not make the call to the subroutine, which reduces scan time execution and provides a more structured program.

2.In the initialization subroutine, load SMB47 according to the desired control operation. For example:

SMB47 = 16#F8 produces the following results:
Enables the counter
Writes a new current value
Writes a new preset value
Sets the initial direction of the HSC to count up
Sets the start and reset inputs to be active high

3.Execute the HDEF instruction with the HSC input set to 1 and the MODE input set to 3 for no external reset or start, 4 for external reset and no start, or 5 for both external reset and start.

4.Load SMD48 (double word size value) with the desired current value (load with 0 to clear it).

5.Load SMD52 (double word size value) with the desired preset value.

6.In order to capture the current value equal to preset event, program an interrupt by attaching the CV = PV interrupt event (event 13) to an interrupt routine.

7.In order to capture direction changes, program an interrupt by attaching the direction changed interrupt event (event 14) to an interrupt routine.

8.In order to capture an external reset event, program an interrupt by attaching the external reset interrupt event (event 15) to an interrupt routine.

9.Execute the global interrupt enable instruction (ENI) to enable interrupts.

10.Execute the HSC instruction to cause the S7-200 to program HSC1.

11.Exit the Subroutine.

Initialization Modes 6, 7, or 8

The following steps describe how to initialize HSC1 for Two Phase Up/Down Counter with Up/Down Clocks (Modes 6, 7, or 8):

1.Use the first scan memory bit to call a subroutine in which the initialization operations are performed. Since you use a subroutine call, subsequent scans do not make the call to the subroutine, which reduces scan time execution and provides a more structured program.

2.In the initialization subroutine, load SMB47 according to the desired control operation. For example:

SMB47 = 16#F8 produces the following results:
Enables the counter
Writes a new current value
Writes a new preset value
Sets the initial direction of the HSC to count up
Sets the start and reset inputs to be active high

3.Execute the HDEF instruction with the HSC input set to 1 and the MODE set to 6 for no external reset or start, 7 for external reset and no start, or 8 for both external reset and start.

4.Load SMD48 (double word size value) with the desired current value (load with 0 to clear it).

5.Load SMD52 (double word size value) with the desired preset value.

6.In order to capture the current value equal to preset event, program an interrupt by attaching the CV = PV interrupt event (event 13) to an interrupt routine.

7.In order to capture direction changes, program an interrupt by attaching the direction changed interrupt event (event 14) to an interrupt routine.

8.In order to capture an external reset event, program an interrupt by attaching the external reset interrupt event (event 15) to an interrupt routine.

9.Execute the global interrupt enable instruction (ENI) to enable interrupts.

10.Execute the HSC instruction to cause the S7-200 to program HSC1.

11.Exit the subroutine.

Initialization Modes 9, 10, or 11

The following steps describe how to initialize HSC1 for A/B Phase Quadrature Counter (Modes 9, 10, or 11):

1.Use the first scan memory bit to call a subroutine in which the initialization operations are performed. Since you use a subroutine call, subsequent scans do not make the call to the subroutine, which reduces scan time execution and provides a more structured program.

2.In the initialization subroutine, load SMB47 according to the desired control operation.

For example (1x counting mode):
SMB47 = 16#FC produces the following results:

Enables the counter
Writes a new current value
Writes a new preset value
Sets the initial direction of the HSC to count up
Sets the start and reset inputs to be active high

For example (4x counting mode):
SMB47 = 16#F8 produces the following results:

Enables the counter
Writes a new current value
Writes a new preset value
Sets the initial direction of the HSC to count up
Sets the start and reset inputs to be active high

3.Execute the HDEF instruction with the HSC input set to 1 and the MODE input set to 9 for no external reset or start, 10 for external reset and no start, or 11 for both external reset and start.

4.Load SMD48 (double word size value) with the desired current value (load with 0 to clear it).

5.Load SMD52 (double word size value) with the desired preset value.

6.In order to capture the current value equal to preset event, program an interrupt by attaching the CV = PV interrupt event (event 13) to an interrupt routine.

7.In order to capture direction changes, program an interrupt by attaching the direction changed interrupt event (event 14) to an interrupt routine.

8.In order to capture an external reset event, program an interrupt by attaching the external reset interrupt event (event 15) to an interrupt routine.

9.Execute the global interrupt enable instruction (ENI) to enable interrupts.

10.Execute the HSC instruction to cause the S7-200 to program HSC1.

11.Exit the subroutine.

Change Direction in Modes 0, 1, or 2

The following steps describe how to configure HSC1 for Change Direction for Single Phase Counter with Internal Direction (Modes 0, 1, or 2):

1.Load SMB47 to write the desired direction:
SMB47 = 16#90 Enables the counter
Sets the direction of the HSC to count down

SMB47 = 16#98 Enables the counter
Sets the direction of the HSC to count up

2.Execute the HSC instruction to cause the S7-200 to program HSC1.

Load a New Current Value (Any Mode)

The following steps describe how to change the counter current value of HSC1 (any mode):

Changing the current value forces the counter to be disabled while the change is made. While the counter is disabled, it does not count or generate interrupts.

1.Load SMB47 to write the desired current value:
SMB47 = 16#C0 Enables the counter
Writes the new current value

2.Load SMD48 (double word size) with the desired current value (load with 0 to clear it).

3.Execute the HSC instruction to cause the S7-200 to program HSC1.

Load a New Preset Value (Any Mode)

The following steps describe how to change the preset value of HSC1 (any mode):

1.Load SMB47 to write the desired preset value:
SMB47 = 16#A0 Enables the counter
Writes the new preset value

2.Load SMD52 (double word size value) with the desired preset value.

3.Execute the HSC instruction to cause the S7-200 to program HSC1.

Disable a High-Speed Counter (Any Mode)

The following steps describe how to disable the HSC1 high-speed counter (any mode):

1.Load SMB47 to disable the counter:
SMB47 = 16#00 Disables the counter

2.Execute the HSC instruction to disable the counter.

Although the above sequences show how to change direction, current value, and preset value individually, you may change all or any combination of them in the same sequence by setting the value of SMB47 appropriately and then executing the HSC instruction.