Counter
Count up or down through specified range of numbers
Libraries:
DSP System Toolbox /
Signal Management /
Switches and Counters
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. For more information about scalar input operation, vector input operation, and free-running operation, see Algorithms.
Examples
Count Down Through Range of Numbers
This example shows how to use the Counter block to count down through a range of numbers. The 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 four-column output, dsp_examples_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.
Ports
Input
Inc/Dec — Input signal to trigger count event
scalar | vector
Input signal used to determine when the block increments or decrements the counter, specified as a real-valued scalar or vector. If the input to the Inc or Dec port is a vector, the block treats the vector as a frame. Each time a triggering event occurs at the Inc or Dec input port, the block increments or decrements the counter, respectively. You control the type of triggering event using the Count event parameter.
Dependencies
The block enables the Inc (increment) port
when you set the Count direction
parameter to Up
.
The block enables the Dec (decrement) port
when you set the Count direction
parameter to Down
.
The block disables the
Inc/Dec input port
when you set the Count event
parameter to Free running
. In free
running mode, the block counts at a constant time interval.
Data Types: single
| double
| int8
| int16
| int32
| uint8
| uint16
| uint32
| Boolean
Rst — Reset signal
scalar
Input signal used to determine when the block resets the counter, specified as a real-valued scalar. The Rst port must have the same port sample time as the Inc or Dec input port. Each time a triggering event occurs at the Rst port, the block resets the counter to its initial value. For more information about triggering events, see Count event.
Dependencies
To enable this port, select the Reset input check box.
Data Types: single
| double
| int8
| int16
| int32
| uint8
| uint16
| uint32
| Boolean
Max — Maximum counter size
scalar
Specify the maximum counter size as any 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.
Dependencies
To enable this port, set the Counter size
parameter to Specify via input
port
.
Data Types: uint8
| uint16
| uint32
Output
Cnt — Current value of counter
scalar | vector
Current value of the counter, specified as a scalar or vector. When
you set the Count event parameter to
Free running
, the
Cnt output is a 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.
Dependencies
To enable this port, set the Output parameter
to Count
or Count and
Hit
.
Data Types: single
| double
| int8
| int16
| int32
| uint8
| uint16
| uint32
| Boolean
Hit — Hit status
scalar | vector
Hit status of the integer values you specified in the Hit
values parameter. When a value you specify occurs in the
count, the block outputs a 1
at the
Hit port.
Note
The block might output Boolean values from the Hit output port depending on the setting of the Hit data type parameter.
Dependencies
To enable this port, set the Output parameter
to Hit
or Count and
Hit
.
Data Types: Boolean
| Logical
Parameters
Count direction — Count up or down
Up
(default) | Down
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.
Tunable: Yes
Count event — Type of trigger event
Rising edge
(default) | Falling edge
| Either edge
| Non-zero sample
| Free running
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. You can select:
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 aRising edge
orFalling 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.
Dependencies
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.
Counter size — Range of integer values to count through
8 bits
(default) | 16 bits
| 32 bits
| User defined
| Specify via input port
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 of0
to255
.16 bits
— Specifies a counter with a range of0
to65535
.32 bits
— Specifies a counter with a range of0
to2
32–1
.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 from0
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 from0
to the value you specify as an input to the Max port.
Maximum count — Maximum value of counter
255
(default) | positive integer
Specify the maximum value of the counter as any unsigned integer representable by the data type you specify for the Counter data type parameter. Tunable (Simulink) in Simulink normal mode.
Tunable: Yes
Dependencies
To enable this parameter, set the Counter size to
User defined
.
Initial count — Initial value of counter
0
(default) | integer ≥ 0
Specify the initial value of the counter as any unsigned integer in the range defined by the Counter size parameter. 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: Yes
Output — Output count value, hit value, or both
Count
(default) | Hit
| Count and Hit
Select the output ports to enable. You can choose to enable the
Count
, Hit
, or
Count and Hit
ports.
Hit values — Count values to flag
32
(default) | scalar | vector
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: Yes
Reset input — Enable Rst input port
on
(default) | off
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 — Number of samples in each output vector
1
(default) | positive integer
Specify the number of samples, M, in each output vector as a positive integer.
Dependencies
To enable this parameter, set the Count
event to Free running
.
Sample time — Sample time in Free Running
mode
1
(default) | -1
| scalar ≥ 0
Specify the constant interval,
Ts, at which the block
increments or decrements the counter when in free-running mode. You can
specify a scalar that is greater than or equal to zero, or specify a value
of -1
to inherit the sample time.
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
MTs.
Dependencies
To enable this parameter, set the Count
event to Free
running
.
Count data type — Data type of Cnt port
double
(default) | single
| int8
| uint8
| int16
| uint16
| int32
| uint32
Specify the data type of the output at the Cnt port.
Dependencies
To enable this parameter, set the Output
parameter to Count
or Count and
Hit
.
Hit data type — Data type of Hit port
Logical
(default) | Boolean
Specify the data type of the output at the Hit port.
Dependencies
To enable this parameter, set the Output
parameter to Hit
or set it to
Count and Hit
with the Count
data type parameter set to
Double
.
Block Characteristics
Data Types |
|
Direct Feedthrough |
|
Multidimensional Signals |
|
Variable-Size Signals |
|
Zero-Crossing Detection |
|
Algorithms
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. The Cnt port 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 of1
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
MTs.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using Simulink® Coder™.
Version History
Introduced before R2006a
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)