• No results found

Section Description Page

7.1 Overview 7-2

7.2 Setting, Resetting, and Enabling a Counter 7-3

7.3 Couting Up and Counting Down 7-5

7.4 Loading a Count Value as Integer 7-6

7.5 Loading a Count Value in Binary Coded Decimal Format 7-7

7.6 Counter Example 7-8

7.7 Address Locations and Ranges for Counter Instructions 7-10 Chapter Overview

7

7.1 Overview

A counter is a function element of the STEP 7 programming language that counts.

Counters have an area reserved for them in the memory of your CPU. This memory area reserves one 16-bit word for each counter. The statement list instruction set supports 256 counters. To find out how many counters are available in your CPU, please refer to the CPU technical data.

Counter instructions are the only functions with access to the memory area reserved for the counter.

The statement list representation of the STEP 7 programming language provides the following counter instructions:

 Set (S)

 Reset (R)

 Count up (CU)

 Count down (CD)

 Enable counter (FR)

 Load counter in one of the following formats:

– Binary (L)

– Binary coded decimal (LC)

 Check the signal state of a counter and combine the result in a Boolean logic operation (A, AN, O, ON, X, XN).

A signal state check with an A, O or X instruction will have the result

“1”, when the value of a counter is greater than “0”.

A signal state check with an A, O or X instruction will have the result

“0”, when the value of the counter is equal to “0”.

Figure 7-1 summarizes the instructions that use a counter word as their address.

Enable a counter (FR) Count Down (CD)

Load a count value (L, LC) Counter word

Set a counter (S) Count Up (CU) Reset a counter (R)

Check the signal state of a counter (A, AN, O, ON, X, XN)

Figure 7-1 Instructions That Can Use a Counter Word as an Address Definition

Available Instructions Counter Instructions

7.2 Setting, Resetting, and Enabling a Counter

To set a counter in your statement list program, include three statements to trigger the following operations:

 Check a signal state for 0 or 1 (for example, A I 2.3)

 Load a count value (for example, L C# 3) into the low word of accumulator 1

 Set a counter with the count value you loaded (for example, S C 1). This operation moves the count value from accumulator 1 to the counter word.

In your statement list program, a change in the result of logic operation (RLO) from 0 to 1 prior to a Set (S) instruction statement sets a counter to the programmed count value. The programmed count value and the Set statements must follow the logic operation directly that provides the condition for setting the counter.

You set a counter to a specific value by loading that value into the low word of accumulator 1 and, immediately thereafter, setting that counter. When you set a counter in your program, the CPU looks in accumulator 1 for the count value. Then the CPU transfers the count value from the accumulator to the counter word that you specified in your set statement (for example, S C 1).

The range of the count value is 0 to 999.

Figure 7-2 provides an example for setting a counter. A change in signal state from 0 to 1 at input I 2.3 sets the counter. The figure refers to the following program:

STL Explanation

A I 2.3 L C# 3 S C 1

Check signal state of input I 2.3.

If the signal state is 1, load count value 3 into accumulator 1.

Set counter C 1 to a count value of 3. This operation moves the count value of 3 from the accumulator into counter word 1.

Description

The Starting Count

Example of Setting a Counter

Counter Instructions

You reset a counter by using the Reset (R) instruction. The CPU resets a counter when the result of logic operation is 1 immediately before the R instruction in your program. As long as the RLO that comes before an R instruction statement is 1, an A, O, or X instruction that checks the signal state of a counter produces a result of 0 and an AN, ON, or XN instruction that checks the signal state of a counter produces a result of 1.

When your program resets a counter, it clears the counter, that is, it resets it to a value of 0.

If a counter is to be reset by a static signal at the Reset (R) and independently of the RLO of the other counter instructions, you need to write the Reset statement immediately after the Set, Count Up, or Count Down statement (see Section 7.3) and before the signal check or load operation.

Programming for counters should adhere to the following sequence (see also the programming example in Section 7.6):

1. Count up 2. Count down 3. Set counter 4. Reset counter

5. Check signal state of counter 6. Load count value (Read count value)

A change in the result of logic operation of the Enable instruction (FR) from 0 to 1 enables a counter. The CPU executes the FR instruction only on a positive signal edge.

A counter enable is not required to set a counter, nor is it required for normal counting. An enable is only used to set a counter, or to count up or down, if a positive edge (transition from 0 to 1) in front of the corresponding count statement is needed, and if the RLO bit in front of the corresponding statement has a signal state of 1.

Resetting a Counter

Enabling a

Counter for Restart Counter Instructions

7.3 Couting Up and Counting Down

In your statement list program, a change in the result of logic operation from 0 to 1 prior to a Count Up (CU) instruction statement increments the counter.

Each time a positive edge change occurs in the RLO directly before a Count Up instruction, the count is incremented by one unit.

When the count reaches its upper limit of 999, incrementing stops and any further change in the signal state at the Count Up input have no effect. No provisions are made for overflow (OV).

STL Explanation

A I 0.1 CU C1

If there is a positive edge change at input I 0.1, counter C 1 is incremented by 1.

In your statement list program, a change in the result of logic operation from 0 to 1 prior to a Count Down (CD) instruction statement decrements the counter. Each time a positive edge change occurs in the RLO directly before a Count Down instruction, the count is decremented by one unit.

When the count reaches its lower limit of 0, decrementing stops and any further change in the signal state at the Count Down input have no effect.

The counter does not count with negative values.

STL Explanation

A I 0.2 CD C1

If there is a positive edge change at input I 0.2, counter C 1 is decremented by 1.

Description of Counting Up

Description of Counting Down

Counter Instructions

7.4 Loading a Count Value as Integer

A count value is stored in a counter word in binary code. You can use the following instruction to read the binary count value out of a counter word and load it into the low word of accumulator 1:

L <counter word>

This type of loading is referred to as loading a counter value directly.

STL Explanation

L C 1 Load accumulator 1 low directly with the count value of counter C 1 in binary format.

0 0 0 0 0 0

9 0

X X X X X X

Count Value Counter word

for C 1

Accumulator 1 low

10 15

15 10 0

Figure 7-3 Loading a Count Value into Accumulator 1 Using the L Instruction You can use the value contained in the accumulator as a result of the L operation for further processing. You cannot transfer a value from the accumulator to the counter word. If you want to start a counter with a specific count value, you need to use the appropriate Set counter statement.

Description

Counter Instructions

7.5 Loading a Count Value in Binary Coded Decimal Format

A count value is stored in a counter word in binary code. You can use the following instruction to read the count value in binary coded decimal (BCD) format out of a counter word and load it into the low word of accumulator 1:

LC <counter word>

This type of loading is referred to as loading a count value in BCD format.

The value contained in the low word of accumulator 1 as a result of the LC operation has the same format as is needed to set a counter.

STL Explanation

LC C 1 Load accumulator 1 low directly with the count value of counter C 1 in binary coded decimal format.

Count value

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

Count in BCD format 15

102 101 100

14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

Binary to BCD

0 0 0 0

(Hundreds) (Tens) (Ones)

Counter word for C 1

Accumulator 1 low

Figure 7-4 Loading a Count Value into Accumulator 1 Using the LC Instruction The value contained in the accumulator as a result of the LC operation can be used for further processing, such as transferring the value to the outputs for a Description

Counter Instructions

7.6 Counter Example

Figure 7-5 provides an example of counting up, counting down, setting and resetting a counter, checking the signal state of a counter, and loading a count value. The example follows the programming sequence recommended in Section 7.2. The numbers in squares in the figure are keyed to explanations that follow the figure. Figure 7-5 refers to the statement list program that follows the explanatory list.

ÎÎÎ

ÎÎÎ

I 2.0 Enable I 2.1 Count Up

I 2.2 Count Down

I 2.3 Set

I 2.4 Reset

Counter Response





Check signal state of counter output Q 4.0

MW10 Load

MW12 ÎÎÎ

ÎÎÎ ÎÎÎ

ÎÎÎ







 





3 2

1 0 1 2

1 1

3 2

1 0 1 2

1 1

Figure 7-5 Example of Counter Instructions Counter Instructions

The following list describes the elements of Figure 7-5:

 A change in the RLO from 0 to 1 at the Set input sets the counter to a count value of 3. A transition from 1 to 0 at the Set input has no effect on the counter.

 A change in the RLO from 0 to 1 at the Count Down input decreases the counter by one. A transition from 1 to 0 at the Count Down input has no effect on the counter.

 The result of the signal state check statement A C 1 is 0 when the count value is 0.

 A change in the RLO from 0 to 1 at the Count Up input increases the counter by one. A transition from 1 to 0 at the Count Up input has no effect on the counter.

 If an RLO of 1 is applied at the Reset input, the counter is reset.

Checking the signal state produces a result of 0. A change in the RLO from 1 to 0 at the Reset input has no effect on the counter.

 A change in the RLO from 0 to 1 at the Count Up input while the Reset signal is applied causes the counter to increase momentarily but to reset immediately because of the Reset statement that follows directly in the program. (The momentary increase is indicated by a pulse line in the timing diagram in Figure 7-5). Checking the signal state then produces a result of 0.

A change in the RLO from 0 to 1 at the Enable input with Count Up and Count Down applied causes the counter to increase momentarily but then decrease immediately because of the Count Down

statement that follows directly in the program. (The momentary increase is indicated by a pulse line in the timing diagram in Figure 7-5). A transition from 1 to 0 at the Enable input has no effect on the counter.

STL Explanation

A I 2.0

FR C 1

A I 2.1

CU C 1

A I 2.2

CD C 1

A I 2.3

L C# 3

S C 1

A I 2.4

Enable counter C 1.

Count up (increment by 1).

Count down (decrement by 1).

Set counter C 1.

Counter Instructions

7.7 Address Locations and Ranges for Counter Instructions

Table 7-1 shows the address types, address locations, and address ranges for the counter instructions.

Table 7-1 Address Locations, Ranges, and Types for Counter Instructions Address Range according to Addressing Type

Direct Memory Indirect

0 to 65,535

[DBW]

[DIW]

[LW]

[MW]

0 to 65,534 Counter Instructions