Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Counter

Count up or down through specified range of numbers

Library

Signal Management / Switches and Counters

dspswit3

Description

The Counter block counts up or down through a specified range of numbers. The block enables the Inc (increment) port when you set the Count direction parameter to Up. When you set the Count direction parameter to Down, the block enables the Dec (decrement) port. If you set the Count event parameter to Free running, the block disables the Inc or Dec port, and counts at a constant time interval. For all other settings of the Count event parameter, the block increments or decrements the counter each time a trigger event occurs at the Inc or Dec input port. When a trigger event occurs at the optional Rst port, the block resets the counter to its initial state.

The Counter block accepts single-channel inputs. The Inc and Dec ports accept real-valued scalars or vectors. If the input to the Inc or Dec port is a vector, the block treats the vector as a frame. The Rst port only accepts real-valued scalars. The Rst port must have the same port sample time as the Inc or Dec input port. If you enable the optional Max input port, you must provide an unsigned integer input that the Count data type can represent.

See the following topics for more information:

Setting the Count Event Parameter

Specify the trigger event for the Inc/Dec and Rst ports by setting the Count event parameter to one of the following values:

  • Rising edge — Triggers a count or reset operation when the input to the Inc/Dec or Rst port behaves in one of the following ways:

    • Rises from a negative value to a positive value or zero.

    • Rises from zero to a positive value, where the rise is not a continuation of a rise from a negative value to zero (see the following figure).

  • Falling edge — Triggers a count or reset operation when the input to the Inc/Dec or Rst port behaves in one of the following ways:

    • Falls from a positive value to a negative value or zero.

    • Falls from zero to a negative value, where the fall is not a continuation of a fall from a positive value to zero (see the following figure).

  • Either edge — Triggers a count or reset operation when the input to the Inc/Dec or Rst port is a Rising edge or Falling edge.

  • Non-zero sample — Triggers a count or reset operation at each sample time when the input to the Inc/Dec or Rst port is not zero.

  • Free running — Disables the Inc/Dec port and enables the Samples per output frame and Sample time block parameters. The block increments or decrements the counter at a constant interval, Ts, which you specify using the Sample time parameter. For more information, see Free-Running Operation. In this mode, the block resets the counter whenever it receives a non-zero sample at the Rst port.

Setting the Counter Size and Initial Count Parameters

At the start of the simulation, the block sets the counter to the value you specify in the Initial count parameter. The Initial count can be any unsigned integer in the range defined by the Counter size parameter.

The Counter size parameter allows you to specify the range of integer values the block counts through. When the block counts through the entire counter range, the next time a trigger event occurs at the Inc/Dec port, the block resets the counter as follows:

  • When you set the Count direction parameter to Up and the counter reaches the upper-limit of the counter range, the block restarts the counter at zero.

  • When you set the Count direction parameter to Down and the counter reaches zero, the block restarts the counter at the upper-limit of the counter range.

You can set the Counter size parameter to one of the following options:

  • 8 bits — Specifies a counter with a range of 0 to 255.

  • 16 bits — Specifies a counter with a range of 0 to 65535.

  • 32 bits — Specifies a counter with a range of 0 to 2321.

  • User defined — Enables the Maximum count parameter, which allows you to specify the upper-count limit as any arbitrary unsigned integer that the Count data type can represent. The counter values range from 0 to the value of the Maximum count parameter.

  • Specify via input port — Enables the Max input port, which allows you to specify the upper-count limit as any arbitrary unsigned integer that the Count data type can represent. The counter values range from 0 to the value you specify as an input to the Max port.

Scalar Input Operation

When you set the Count direction parameter to Up, a trigger event at the Inc (increment) input port causes the block to increase the counter by one. Assuming no reset events occur, the block continues increasing the counter value when triggered, until the counter value reaches the upper-count limit. The next time a trigger event occurs at the Inc port, the block restarts the counter at 0 and resumes increasing the counter by one for each subsequent trigger event at the Inc port.

When you set the Count direction parameter to Down, a trigger event at the Dec (decrement) input port causes the block to decrease the counter by one. Assuming no reset events occur, the block continues decreasing the counter value when triggered until the counter value reaches zero. The next time a trigger event occurs at the Dec port, the block restarts the counter at the upper-count limit and resumes decreasing the counter by one for each subsequent trigger event at the Dec port.

Between triggering events, the block holds the output at its most recent value. The block resets the counter to its initial state when the trigger event specified by the Count event parameter occurs at the optional Rst input port. When the Inc/Dec and Rst ports receive trigger events simultaneously, the block first resets the counter, and then increments or decrements the counter appropriately. If you do not need to reset the counter during simulation, you can disable the Rst port by clearing the Reset input check box.

The Output parameter allows you to specify which values the block outputs.

  • Count enables a Cnt output port on the block, which provides the current value of the counter as a scalar value. The Cnt output port has the same port sample time as the Inc/Dec input port.

  • Hit enables a Hit output port on the block. The Hit port produces zeros while the value of the counter does not equal any of the integers you specify for the Hit values parameter. You can specify an integer or a vector of integers for the Hit values parameter. When the counter value does equal one or more of the values you specify for the Hit values parameter, the block outputs a value of 1 at the Hit output port. The Hit output port has the same port sample time as the Inc/Dec input port.

  • Count and Hit enables both the Cnt and Hit output ports.

Vector Input Operation

The block treats vector inputs to the Inc/Dec port as a frame. Vector operation is the same as scalar operation, except that the block increments or decrements the counter by the total number of trigger events contained in the Inc/Dec input vector. Thus, the counter may change multiple times during the processing of a single Inc/Dec input vector.

When the block has a Hit port, the block outputs a value of 1 if any of the Hit values match any of the counter values during the processing of the Inc/Dec input vector.

When a trigger event splits across two consecutive vectors, that event is counted in the vector that contains the conclusion of the event. When the Rst port receives a trigger event at the same time as the Inc/Dec port, the block first resets the counter. The block then increments or decrements the counter by the number of trigger events contained in the Inc/Dec input vector.

When the input to the Inc/Dec port is a length N vector, the port sample time of the Inc/Dec input port is equal to the frame period of the input, or N times the sample time of the input signal. The port sample time of the Cnt and Hit output ports equals that of the Inc/Dec input port.

Free-Running Operation

The block operates in free-running mode when you select Free running for the Count event parameter.

The Inc/Dec input port is disabled in this mode, and the block simply increments or decrements the counter at the constant interval, Ts, which you specify using the Sample time parameter.

In this mode, the Rst port always behaves as if the Count event parameter were set to Non-zero sample. Thus, the block triggers a reset event at each sample time that the Rst input is not zero.

In this mode, the Cnt output is an M-by-1 vector containing the count value at each of M consecutive sample times, where M is the value you specify for the Samples per output frame parameter. The Hit output is an M-by-1 vector containing the hit status (0 or 1) at each of those M consecutive sample times. Both the Cnt and Hit output ports have a port sample time of M*Ts.

Examples

In the following model, ex_counter_ref, the Simulink® Pulse Generator block drives the Dec port of the Counter block, and the N-Sample Enable block triggers the Rst port. All inputs to and outputs from the Counter block are multiplexed into a single To Workspace block using a 4-port Mux block.

The following figure shows the first 22 samples of the model's four-column output, yout.

You can see that the seventh input sample to both the Dec and Rst ports of the Counter block represent trigger events (rising edges). When this occurs, the block first resets the counter to its initial value of 5, and then immediately decrements the counter to 4. When the counter reaches its minimum value of 0, the block restarts the counter at its maximum value of 20 the next time a trigger event occurs at the Dec port.

Parameters

Count direction

Specify whether to count Up or Down. The port label on the block icon changes to Inc (increment) or Dec (decrement) based on the value of this parameter.

  • When you set the Count direction parameter to Up and the counter reaches the upper-limit of the counter range, the block restarts the counter at zero the next time a trigger event occurs at the Inc port.

  • When you set the Count direction parameter to Down and the counter reaches zero, the block restarts the counter at the upper-limit of the counter range the next time a trigger event occurs at the Dec port.

This parameter is tunable (Simulink) in Simulink Normal mode.

Count event

Specify the type of event that triggers the block to increment, decrement, or reset the counter when received at the Inc/Dec or Rst ports. When you set this parameter to Free running, the block disables the Inc/Dec port and counts at the constant interval specified by the Sample time parameter. For more information on all the possible settings of this parameter, see Setting the Count Event Parameter.

Counter size

Specify the range of integer values the block counts through. For more information about the valid values of this parameter, see Setting the Counter Size and Initial Count Parameters.

Maximum count

Specify the maximum value of the counter as any unsigned integer representable by the data type you specify for the Counter data type parameter. This parameter appears only when you set the Counter size parameter to User defined. Tunable (Simulink) in Simulink Normal mode.

Initial count

Specify the initial value of the counter. The block uses the initial value of the counter at the start of simulation and resets the counter back to that initial value each time a trigger event occurs at the Rst port. Tunable (Simulink).

Output

Select the output ports to enable. You can choose to enable the Count, Hit, or Count and Hit ports.

Hit values

Specify an integer or vector of integers whose occurrence in the count should be flagged by a 1 at the (optional) Hit output port. This parameter appears only when you set the Output parameter to Hit or Count and Hit. Tunable (Simulink).

Reset input

Select this check box to enable the Rst input port. When you enable the Rst port, the block resets the counter to its initial value each time a trigger event occurs at the Rst port. To specify the type of event that triggers a reset of the counter, set the Count event parameter. When you clear the Reset input check box, you cannot reset the counter during simulation.

Samples per output frame

Specify the number of samples, M, in each output vector. This parameter appears only when you set the Count event parameter to Free running.

Sample time

Specify the constant interval, Ts, at which the block increments or decrements the counter when in free-running mode. For example, to have the block increment the counter every 5 seconds, set the Count direction parameter to Up, the Count event parameter to Free running, and specify a value of 5 for the Sample time parameter. In free running mode, the sample time of the output ports is always M*Ts.

This parameter appears only when you set the Count event parameter to Free running.

Count data type

Specify the data type of the output at the Cnt port. This parameter appears only when you set the Output parameter to Count or Count and Hit.

Hit data type

Specify the data type of the output at the Hit port. This parameter appears only when you set the Output parameter to Hit or set it to Count and Hit with the Count data type parameter set to Double.

Supported Data Types

PortSupported Data Types

Inc/Dec

  • Double-precision floating point

  • Single-precision floating point

  • Boolean

  • 8-, 16-, and 32-bit signed integers

  • 8-, 16-, and 32-bit unsigned integers

Rst

  • Double-precision floating point

  • Single-precision floating point

  • Boolean

  • 8-, 16-, and 32-bit signed integers

  • 8-, 16-, and 32-bit unsigned integers

Max

  • 8-, 16-, and 32-bit unsigned integers

Cnt

  • Double-precision floating point

  • Single-precision floating point

  • 8-, 16-, and 32-bit signed integers

  • 8-, 16-, and 32-bit unsigned integers

Hit

  • Logical

  • Boolean — The block might output Boolean values from the Hit output port depending on the setting of the Hit data type parameter.

See Also

Edge DetectorDSP System Toolbox
N-Sample EnableDSP System Toolbox
N-Sample SwitchDSP System Toolbox

Introduced before R2006a

Was this topic helpful?