You are on page 1of 8

S7-200 Tip

High Speed Counters

Tip No. 4

SIMATIC
S7-200 Tips
Group
2

Topic
How to use High Speed Counters of the S7-200 CPU 214

Overview
This Application Tip explains the function of one configuration for a high speed counter (HSC)
on the SIMATIC S7-200. High speed counters can be used in many different configurations for
processing the input signals of certain sensors (such as encoders).
This example uses a pulse output to generate high speed signals for the HSC to count. It is
possible to generate pulse strings along with pulse width modulations to control, for example, a
servo-motor. Since a pulse output is used, a CPU 214 DC/DC/DC is required.
The following application shows how a program should be structured to use a high speed
counter, and a simple feedback loop demonstrates the pulse output function.

Figure 4.1

Copyright 1996 by SIEMENS


Status: 10/96

page 1 / 8

S72_04e.doc
Version 3.2

SIMATIC S7-200 customers have free use of the application tips. These tips are only a general approach to using the S7-200 with various applications.
Your specific application may be different. It is your responsibility to use the SIMATIC S7-200 properly in your applications.

S7-200 Tip

High Speed Counters

Tip No. 4

Program Structure
Main Program

SBR1:
Start of HSC 0

SBR0: Initialization
and start of Pules
Program stops at the
end of the Pulse string

INT 0:
First Preset

INT 1:
Second Preset

INT 2:
Third Preset

1,500
INT 1
1,000

1,000
INT 0

INT 2

Program Description
This example demonstrates the usage of a high speed counter on the S7-200 CPU 214
DC/DC/DC. The counter used is a 2kHz software counter (also integrated in the CPU 212) and
counts more rapidly than the PLC scan time. In addition to a 2 kHz counter, the CPU 214 has
two 7 kHz hardware counters. Overall, the counters need 10 bytes of data memory for
controlling, counting, storing values, and reading the current state.
The size of the program is 96 words.
Chapter 6.1, High-speed Counter Instructions, of the S7-200 Programming Reference Manual
provides more information about high speed counters. Chapter 6.3 High-speed Output
Instructions gives additional information about pulse strings.

Copyright 1996 by SIEMENS


Status: 10/96

page 2 / 8

S72_04e.doc
Version 3.2

SIMATIC S7-200 customers have free use of the application tips. These tips are only a general approach to using the S7-200 with various applications.
Your specific application may be different. It is your responsibility to use the SIMATIC S7-200 properly in your applications.

S7-200 Tip

High Speed Counters

Tip No. 4

LAD (S7-MicroDOS)

STL (IEC)

Main Program
// The main program resets the output Q0.0 because it is needed for the
// pulse functions. In addition, it initializes the high speed counter HSC0
// and calls the subroutines 0 and 1 . HSC0 is thus enabled with
// the following features: updating of values possible and counting up.
// The program ends after the number of pulses (specified in the SBR 0 (SMD72)) is reached.

),567
6&$1%,7
604.
 5

029B%
(1

.+),128760%







029B':
(1

.,128760'


029B':
(1

.,128760'


+'()
(1

+6&
02'


 &$//


 &$//


 0(1'


Copyright 1996 by SIEMENS


Status: 10/96

page 3 / 8

LD
R
MOVB

MOVD

SM0.1// First scan bit


Q0.0, 1
// Resetting of output Q0.0
// for pulse strings
16#F8, SMB37
// Load control bits for
// high speed counter
// HSC0 (Enable HSC0,
// update CV, update PV,
// update direction,
// and count up). The
// HSC instruction uses
// these control bits to
// configure the HSC.
0, SMD38
// Start value of high speed
// counter HSC0 (CV)

MOVD

1000, SMD42
// First Stop Value of
// HSC0 (PV)

HDEF

0, 0

// Definition of high speed


// counter 0 in Mode 0

CALL

// Calling Subroutine 0

CALL

// Calling Subroutine 1

MEND

// End of Main Routine

S72_04e.doc
Version 3.2

SIMATIC S7-200 customers have free use of the application tips. These tips are only a general approach to using the S7-200 with various applications.
Your specific application may be different. It is your responsibility to use the SIMATIC S7-200 properly in your applications.

S7-200 Tip

High Speed Counters

Tip No. 4

Subroutines
// Subroutine 0 initializes and enables the pulse output.
// The output is defined in special memory byte SMB67: pulse strings, time base,
// updateability of values and enable. The word SMW68 contains the
// pulse time as a multiplier of the time base. Finally, the number of pulses
// to be generated will be specified in the special memory-double word.

6%5

$/:$<6
21%,7
60029B%
(1

.+',128760%


029B:
(1

.,128760:


029B':
(1

.,128760'


3/6
(1

4;



 5(7


SBR

LD
MOVB

SM0.0 //Always on bit


16#8D,SMB67
// Pulse Output: 1ms, PTO,
// Enable, Updating

MOVW

1,SMW68
// Length of a pulse in ms

MOVD

30000, SMD72
// Number of pulses to be
// generated

PLS

RET

// Subroutine 0

// Enabling of pulse function


// on output Q0.0

// End of Subroutine 0

// Subroutine 1 starts the high speed counter HSC0 and assigns the Interrupt
// Routine 0 to Interrupt Event 12 (HSC0 current value = preset value). This event occurs
// as soon as the number of pulses counted (current value) reach the stop value (Preset).
// Finally, the interrupts are enabled.

6%5

$/:$<6
21%,7
60$7&+
(1

.,17
.(97


 (1,

Copyright 1996 by SIEMENS
Status: 10/96

page 4 / 8

SBR

LD
ATCH

SM0.0 // Always on bit


0, 12
// Assignment of INT 0 to
// Interrupt Event 12 (HSC0
// CV=PV)

ENI

// Subroutine 1

// Enabling the interrupts

S72_04e.doc
Version 3.2

SIMATIC S7-200 customers have free use of the application tips. These tips are only a general approach to using the S7-200 with various applications.
Your specific application may be different. It is your responsibility to use the SIMATIC S7-200 properly in your applications.

S7-200 Tip

High Speed Counters


+6&
(1

1



 5(7


Tip No. 4

HSC

// Starting high speed


// counter 0 as earlier
// configured

RET

// End of Subroutine 1

Interrupt routines
// Interrupt Routine 0 is called when the first Preset value (1000)
// of high speed counter 0 is reached. Output Q0.1 is set, and a
// new Preset value (1500) is loaded for the high speed counter.
// Interrupt Routine 1 is now assigned to Interrupt Event 12 instead of INT 0.

,17

$/:$<6
21%,7
604.
 6

029B%
(1

.+$,128760%


029B':
(1

.,128760'


$7&+
(1

.,17
.(97

+6&
(1

1



 5(7,


INT

// Interrupt Routine 0

LD
S

SM0.0// Always on bit


Q0.1,1
// Setting output Q0.1

MOVB

16#A0, SMB37
// Set control bits to only
// load a new preset value
// (PV)

MOVD

1500, SMD42
// Next Preset value of
// HSC0

ATCH

1, 12
// Assign Interrupt Event 12
// to INT 1 instead of INT 0

HSC

RETI

// Load new Preset to High


// Speed Counter 0

// End of Interrupt Routine 0

// Interrupt Routine 1 is called when the second Preset value (1500)


// of high speed counter 0 is reached. Output Q0.2 is set,
// the counting direction is switched to down, and a new
// Preset value (1000) is loaded.
// Interrupt Routine 2 is assigned to Interrupt Event 12 (HSC0 CV=PV).

Copyright 1996 by SIEMENS


Status: 10/96

page 5 / 8

S72_04e.doc
Version 3.2

SIMATIC S7-200 customers have free use of the application tips. These tips are only a general approach to using the S7-200 with various applications.
Your specific application may be different. It is your responsibility to use the SIMATIC S7-200 properly in your applications.

S7-200 Tip

High Speed Counters

,17

$/:$<6
21%,7
604.
 6

029B%
(1

.+%,128760%


029B':
(1

.,128760'


$7&+
(1

.,17
.(97

+6&
(1

1



 5(7,


Tip No. 4

INT

LD
S

SM0.0 // Always on bit


Q0.2, 1
// Setting output Q0.2
16#B0, SMB37
// Set control bits load a
// new PV and to start
// counting down

MOVB

// Interrupt Routine 1

MOVD

1000, SMD42
// Next Preset Value (PV)

ATCH

2, 12

// Assign Interrupt Event


// 12 to INT 2 instead of
// INT 1

HSC

// Load new Preset and


// direction to High Speed
// Counter 0

RETI

// End of Interrupt
// Routine 1

// Interrupt Routine 2 is called when the third Preset value (1000) of


// high speed counter 0 is reached. Outputs Q0.1 and Q0.2 are
// reset, the counting direction is switched to up again, and the
// current counting value is reset to zero. The Preset value remains unchanged.
// By reassigning Interrupt Routine 0 to Interrupt Event 12, the
// program run starts again.
// The program ends when the number of pulses specified in SBR 0 (SMD72) has been
// reached.

,17

$/:$<6
21%,7
604.
 5

029B%
(1

.+',128760%



Copyright 1996 by SIEMENS


Status: 10/96

page 6 / 8

INT

LD
R

SM0.0 // Always on bit


Q0.1, 2
// Reset outputs Q0.1
// and Q0.2
16#D8, SMB37
// Set control bits to load a
// new CV and start
// counting up

MOVB

// Interrupt Routine 2

S72_04e.doc
Version 3.2

SIMATIC S7-200 customers have free use of the application tips. These tips are only a general approach to using the S7-200 with various applications.
Your specific application may be different. It is your responsibility to use the SIMATIC S7-200 properly in your applications.

S7-200 Tip

High Speed Counters

029B':
(1

.,128760'


$7&+
(1

.,17
.(97

+6&
(1

1



 5(7,


MOVD

Tip No. 4
0, SMD38
// Reset the current value of
// HSC0

ATCH

0, 12
// Reassign INT 0 to
// Interrupt Event 12

HSC

RETI

// Restarting high speed


// counter 0

// End of Interrupt
// Routine 2

Conversion Notes
To Convert from IEC STL to S7-Micro/DOS STL:

z
z
z
z
z

Add a K before all non-Hex numerical constants (i.e. 4 K4)


Replace 16# with KH for all Hex constants (i.e. 16#FF KHFF)
Commas denote field divisions. Use arrow or TAB keys to toggle between fields.
To convert an S7-Micro/DOS STL program to LAD form, every network must begin
with the word NETWORK and a number. Each network in this Application Tip
program is designated by a number on the ladder diagram. Use the INSNW
command under the EDIT menu to enter a new network. The MEND, RET, RETI,
LBL, SBR, and INT commands each receive their own networks.
Line-Comments denoted by // are not possible with S7-Micro/DOS, but NetworkComments are possible.

Copyright 1996 by SIEMENS


Status: 10/96

page 7 / 8

S72_04e.doc
Version 3.2

SIMATIC S7-200 customers have free use of the application tips. These tips are only a general approach to using the S7-200 with various applications.
Your specific application may be different. It is your responsibility to use the SIMATIC S7-200 properly in your applications.

S7-200 Tip

High Speed Counters

Tip No. 4

General Notes
The SIMATIC S7-200 Application Tips are provided to give users of the S7-200 some indication
as to how, from the view of programming technique, certain tasks can be solved with this
controller. These instructions do not purport to cover all details or variations in equipment, nor do
they provide for every possible contingency. Use of the S7-200 Application Tips is free.
Siemens reserves the right to make changes in specifications shown herein or make
improvements at any time without notice or obligation. It does not relieve the user of
responsibility to use sound practices in application, installation, operation, and maintenance of
the equipment purchased. Should a conflict arise between the general information contained in
this publication, the contents of drawings or supplementary material, or both, the latter shall take
precedence.
Siemens is not liable, for whatever legal reason, for damages or personal injury resulting from
the use of the application tips.
All rights reserved. Any form of duplication or distribution, including excerpts, is only permitted
with express authorization by SIEMENS.

Copyright 1996 by SIEMENS


Status: 10/96

page 8 / 8

S72_04e.doc
Version 3.2

SIMATIC S7-200 customers have free use of the application tips. These tips are only a general approach to using the S7-200 with various applications.
Your specific application may be different. It is your responsibility to use the SIMATIC S7-200 properly in your applications.

You might also like