DSP Blockset | ![]() ![]() |
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.
The trigger event for both inputs is specified by the Count event pop-up menu, and can be one of the following:
Clk
or Rst
input rises from a negative value to zero or a positive value, or from zero to a positive value.Clk
or Rst
input falls from a positive value to zero or a negative value, or from zero to a negative value.Clk
or Rst
input is not zero.Clk
port, and enables the Samples per output frame and Sample time parameters. The block increments or decrements the counter at a constant interval, Ts, specified by the Sample time parameter. See Free-Running Operation below.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 deselecting the Reset input check box.)
The Output pop-up menu provides three options for the output port configuration of the block icon:
Cnt
port, which produces the current value of the counter as a sample-based scalar with the same sample period as the inputs.Hit
port. The Hit
port produces zeros while the value of the counter does not equal the integer Hit value parameter setting. When the counter value does equal the Hit value setting, the block generates a value of 1
at the Hit
port. The output is sample-based with the same sample period as the inputs.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 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 Simulink's Discrete 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 Discrete Pulse Generator and To Workspace blocks.)
6
20
5
4
4
.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
Clk
or Rst
ports. Free running disables the Clk
port, and counts continuously with the period specified by the Sample time parameter.Cnt
, Hit
, or both.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 Simulink's external mode.Rst
input port when selected.See Also
Edge Detector |
DSP Blockset |
N-Sample Enable |
DSP Blockset |
N-Sample Switch |
DSP Blockset |
![]() | Correlation | Covariance AR Estimator | ![]() |