Counter - Count up or down through specified range of numbers

Library

Signal Management / Switches and Counters

dspswit3

Description

The Counter block increments or decrements an internal counter each time it receives a trigger event at the Clk port. A trigger event at the Rst port resets the counter to its initial state.

The input to the Rst port must be a real sample based scalar. The input to the Clk port can be a real sample-based scalar, or a real frame-based vector (that is, single channel). When both inputs are sample based, they must have the same sample period. When the Clk input is frame based, the frame period must equal the sample period of the Rst input.

Sections of This Reference Page

Setting the Count Event Parameter

The trigger event for both inputs is specified by the Count event parameter, and can be one of the following:

When running simulations in the Simulink MultiTasking mode, sample-based reset signals have a one-sample latency, and frame-based reset signals have one frame of latency. Thus, there is a one-sample or one-frame delay between the time the block detects a reset event, and when it applies the reset. For more information on latency and the Simulink tasking modes, see Excess Algorithmic Delay (Tasking Latency) and Models with Multiple Sample Rates in the Real-Time Workshop User's Guide.

Setting the Counter Size and Initial Count Parameters

At the start of the simulation, the block sets the counter to the value specified by the Initial count parameter, which can be any integer in the range defined by the Counter size parameter. The Counter size parameter allows you to choose from three standard counter ranges, or to specify an arbitrary counter limit:

Sample-Based Operation

The block operates in sample-based mode when the Clk input is a sample-based scalar. Sample-based vectors and matrices are not accepted.

When the Count direction parameter is set to Up, a sample-based trigger event at the Clk input causes the block to increment the counter by one. The block continues incrementing the counter when triggered until the counter value reaches the upper count limit (that is 255 for an 8-bit counter). At the next Clk trigger event, the block resets the counter to 0, and resumes incrementing the counter with the subsequent Clk trigger event.

When the Count direction parameter is set to Down, a sample-based trigger event at the Clk input causes the block to decrement the counter by one. The block continues decrementing the counter when triggered until the counter value reaches 0. At the next Clk trigger event, the block resets the counter to the upper count limit (that is 255 for an 8-bit counter), and resumes decrementing the counter with the subsequent Clk trigger event.

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 in the Count event menu is received at the optional Rst input. When trigger events are received simultaneously at the Clk and Rst ports, the block first resets the counter, and then increments or decrements appropriately. (If you do not need to reset the counter during the simulation, you can disable the Rst port by clearing the Reset input check box.)

The Output pop-up menu provides three options for the output port configuration of the block icon:

Frame-Based Operation

The block operates in frame-based mode when the Clk input is a frame-based vector (that is, single channel). Multichannel frame-based inputs are not accepted.

Frame-based operation is the same as sample-based operation, except that the block increments or decrements the counter by the total number of trigger events contained in the Clk input frame. A trigger event that is split across two consecutive frames is counted in the frame that contains the conclusion of the event. When a trigger event is received at the Rst port, the block first resets the counter, and then increments or decrements the counter by the number of trigger events contained in the Clk frame.

The Cnt and Hit outputs are sample-based scalars with sample period equal to the Clk input frame period.

Free-Running Operation

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

The Rst port behaves as if the Count event parameter were set to Non-zero sample (triggers a reset at each sample time that the Rst input is not zero).

The Clk input port is disabled in this mode, and the block simply increments or decrements the counter using the constant sample period specified by the Sample time parameter, Ts. The Cnt output is a frame-based M-by-1 matrix containing the count value at each of M consecutive sample times, where M is specified by the Samples per output frame parameter. The Hit output is a frame-based M-by-1 matrix containing the hit status (0 or 1) at each of those M consecutive sample times. Both outputs have a frame period of M*Ts.

Examples

In the model below, the Clk port of the Counter block is driven by the Simulink Pulse Generator block, and the Rst port is triggered by an N-Sample Enable block. All of the Counter block's inputs and outputs are multiplexed into a single To Workspace block using a 4-port Mux block.

To run the model, first select Configuration Parameters from the Simulation menu. In the Select pane, click Solver, and set the Stop time to 30. Then adjust the block parameters as described below. (Use the default settings for the Pulse Generator and To Workspace blocks.)

The figure below shows the first 22 samples of the model's four-column output, yout. The first column is the Counter block's Clk input, the second column is the block's Rst input, the third column is the block's Cnt output, and the fourth column is the block's Hit output.

You can see that the seventh input samples to both the Clk and Rst ports of the Counter block represent trigger events (rising edges), so at this time step the block first resets the counter to its initial value of 5, and then immediately decrements the count to 4. When the counter reaches its minimum value of 0, it rolls over to its maximum value of 20 with the following trigger event at the Cnt port.

Dialog Box

Count direction

The counter direction, Up or Down. Tunable, except in the Simulink external mode.

Count event

The type of event that triggers the block to increment, decrement, or reset the counter when received at the Clk or Rst ports. Free running disables the Clk port, and counts continuously with the period specified by the Sample time parameter. For more information on all the possible settings, see Setting the Count Event Parameter.

Counter size

The range of integer values the block should count through before recycling to zero. For more information, see Setting the Counter Size and Initial Count Parameters.

Maximum count

The counter's maximum value when Counter size is set to User defined. Tunable.

Initial count

The counter's initial value at the start of the simulation and after reset. Tunable, except in the Simulink external mode.

Output

Selects the output port(s) to enable: Cnt, Hit, or both.

Hit value

The scalar value whose occurrence in the count should be flagged by a 1 at the (optional) Hit output. This parameter is available when Hit or Count and Hit are selected in the Output menu. Tunable.

Reset input

Enables the Rst input port when selected.

Samples per output frame

The number of samples, M, in each output frame. This parameter is available when you select Free running in the Count event menu.

Sample time

The output sample period, Ts, in free-running mode. This parameter is available when you select Free running in the Count event menu.

Count data type

The data type of the output from the Cnt output port. This parameter is available when the Output parameter is set to Count or Count and Hit.

Hit data type

The data type of the output from the Hit output port. For information on the Logical and Boolean options of this parameter, see Effects of Enabling and Disabling Boolean Support. This parameter is available when the Output parameter is set to Hit or the Output parameter is set to Count and Hit and the Count data type parameter is set to Double.

Supported Data Types

PortSupported Data Types

Clk

  • Double-precision floating point

  • Single-precision floating point

  • Boolean

Rst

  • Double-precision floating point

  • Single-precision floating point

  • Boolean

Cnt

  • Double-precision floating point

  • Single-precision floating point

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

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

Hit

See Also

Edge DetectorSignal Processing Blockset
N-Sample EnableSignal Processing Blockset
N-Sample SwitchSignal Processing Blockset

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS