DSP Blockset    
Counter

Count up or down through a specified range of numbers.

Library

Signal Management / Switches and Counters

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 (i.e., single channel). If both inputs are sample-based, they must have the same sample period. If 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:

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 (e.g., 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 (e.g., 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 (i.e., 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 Free running is selected 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.

Example

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 Simulation Parameters from the Simulation menu, 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, except in the Simulink external mode.
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 Free running is selected in the Count event menu.
Sample time
The output sample period, Ts, in free-running mode. This parameter is available when Free running is selected in the Count event menu.
Count data type
The data type of the output from the Cnt output port. This parameter is available when Free running is selected in the Count event menu.
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 Free running is selected in the Count event menu and Hit or Count and Hit is selected in the Output menu.

Supported Data Types

To learn how to convert to the above data types in MATLAB and Simulink, see Supported Data Types and How to Convert to Them.

See Also

Edge Detector
DSP Blockset
N-Sample Enable
DSP Blockset
N-Sample Switch
DSP Blockset

Also see Switches and Counters for a list of all the blocks in the Switches and Counters library.


  Correlation Covariance AR Estimator