Main Content
Count up or down through specified range of numbers
expand all in page
Libraries:
DSP System Toolbox / Signal Management / Switches and Counters
Description
The Counter block counts up or down through a specified range ofnumbers. The block enables the Inc (increment) port when you setthe Count direction parameter to Up
.When you set the Count direction parameter toDown
, the block enables the Dec(decrement) port. If you set the Count event parameter toFree running
, the block disables theInc or Dec port and counts at a constanttime interval. For all other settings of the Count event parameter,the block increments or decrements the counter each time a trigger event occurs at theInc or Dec input port. When a triggerevent occurs at the optional Rst port, the block resets the counterto its initial state.
The Counter block accepts single-channel inputs. For more informationabout scalar input operation, vector input operation, and free-running operation, seeAlgorithms.
Examples
expand all
Count Down Through Range of Numbers
This example uses:
- DSP System ToolboxDSP System Toolbox
- SimulinkSimulink
Open Model
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
expand all
Inc/Dec — Input signal to trigger count event
scalar | vector
Input signal used to determine when the block increments or decrementsthe counter, specified as a real-valued scalar or vector. If the inputto the Inc or Dec port is avector, the block treats the vector as a frame. Each time a triggeringevent occurs at the Inc or Decinput port, the block increments or decrements the counter,respectively. You control the type of triggering event using theCount event parameter.
Dependencies
The block enables the Inc (increment) portwhen you set the Count directionparameter to Up
.
The block enables the Dec (decrement) portwhen you set the Count directionparameter to Down
.
The block disables theInc/Dec input portwhen you set the Count eventparameter to Free running
. In freerunning 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 portmust have the same port sample time as the Inc orDec input port. Each time a triggering eventoccurs at the Rst port, the block resets thecounter to its initial value. For more information about triggeringevents, see Count event.
Dependencies
To enable this port, select the Reset inputcheck 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 theCount data type can represent. The countervalues range from 0
to the value you specify as aninput to the Max port.
Dependencies
To enable this port, set the Counter sizeparameter to Specify via inputport
.
Data Types: uint8
| uint16
| uint32
Output
expand all
Cnt — Current value of counter
scalar | vector
Current value of the counter, specified as a scalar or vector. Whenyou set the Count event parameter toFree running
, theCnt output is a M-by-1vector containing the count value at each of M consecutive sample times, where M is the value you specify for theSamples per output frameparameter.
Dependencies
To enable this port, set the Output parameterto Count
or Count andHit
.
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 Hitvalues parameter. When a value you specify occurs in thecount, the block outputs a 1
at theHit port.
Note
The block might output Boolean values from theHit output port depending on the setting ofthe Hit data type parameter.
Dependencies
To enable this port, set the Output parameterto Hit
or Count andHit
.
Data Types: Boolean
| Logical
Parameters
expand all
Count direction — Count up or down
Up
(default) | Down
Specify whether to count Up
orDown
. The port label on the block iconchanges 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 upperlimit of the counter range, the block restarts the counter at zerothe next time a trigger event occurs at the Incport.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 counterrange the next time a trigger event occurs at theDec 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 theInc/Dec orRst ports. You can select:
Rising edge
— Triggers a countor reset operation when the input to theInc/Dec orRst port behaves in one of the followingways:Rises from a negative value to a positive value orzero.
Rises from zero to a positive value, where the rise is nota continuation of a rise from a negative value to zero (seethe following figure).
Falling edge
— Triggers a countor reset operation when the input to theInc/Dec orRst port behaves in one of the followingways:Falls from a positive value to a negative value orzero.
Falls from zero to a negative value, where the fall is nota continuation of a fall from a positive value to zero (seethe following figure).
Either edge
— Triggers a countor reset operation when the input to theInc/Dec orRst port is aRisingedge
orFallingedge
.Non-zero sample
— Triggers acount or reset operation at each sample time when the input to theInc/Dec orRst port is not zero.Free running
— Disables theInc/Dec port andenables the Samples per output frame andSample time block parameters. The blockincrements or decrements the counter at a constant interval,Ts, which youspecify using the Sample time parameter. Formore information, see Free-Running Operation. In this mode, the block resetsthe counter whenever it receives a non-zero sample at theRst port.
Dependencies
When you set this parameter to Freerunning
, the block disables theInc/Dec port and counts atthe constant interval specified by the Sample timeparameter.
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 theblock counts through the entire counter range, the next time a trigger eventoccurs at the Inc/Dec port, theblock resets the counter as follows:
When you set the Count direction parameter to
Up
and the counter reaches the upperlimit of the counter range, the block restarts the counter atzero.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 counterrange.
You can set the Counter size parameter to one of thefollowing options:
8 bits
— Specifies a counterwith a range of0
to255
.16 bits
— Specifies a counterwith a range of0
to65535
.32 bits
— Specifies a counterwith a range of0
to2
32–1
.User defined
— Enables theMaximum count parameter, which allows youto specify the upper-count limit as any arbitrary unsigned integerthat the Count data type can represent. Thecounter values range from0
to the value of theMaximum count parameter.Specify via input port
— Enablesthe Max input port, which allows you to specifythe upper-count limit as any arbitrary unsigned integer that theCount data type can represent. The countervalues range from0
to the value you specify asan 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 integerrepresentable by the data type you specify for the Counter datatype parameter. Tunable (Simulink) in Simulink normal mode.
Tunable: Yes
Dependencies
To enable this parameter, set the Counter size toUser defined
.
Initial count — Initial value of counter
0
(default) | integer ≥ 0
Specify the initial value of the counter as any unsigned integer in therange defined by the Counter size parameter. The blockuses the initial value of the counter at the start of simulation and resetsthe counter back to that initial value each time a trigger event occurs atthe 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 theCount
, Hit
, orCount and Hit
ports.
Hit values — Count values to flag
32
(default) | scalar | vector
Specify an integer or vector of integers whose occurrence in the countshould be flagged by a 1
at the (optional)Hit output port. This parameter appears only whenyou 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 thecounter to its initial value each time a trigger event occurs at theRst port. To specify the type of event thattriggers a reset of the counter, set the Count eventparameter. 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 vectoras a positive integer.
Dependencies
To enable this parameter, set the Countevent to Free running
.
Sample time — Sample time in Free Running
mode
1
(default) | -1
| scalar ≥ 0
Specify the constant interval,Ts, at which the blockincrements or decrements the counter when in free-running mode. You canspecify a scalar that is greater than or equal to zero, or specify a valueof -1
to inherit the sample time.
For example, to have the block increment the counter every5
seconds, set the Countdirection parameter to Up
, theCount event parameter to Freerunning
, and specify a value of 5
forthe Sample time parameter. In free running mode, thesample time of the output ports is alwaysMTs.
Dependencies
To enable this parameter, set the Countevent to Freerunning
.
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 Cntport.
Dependencies
To enable this parameter, set the Outputparameter to Count
or Count andHit
.
Hit data type — Data type of Hit port
Logical
(default) | Boolean
Specify the data type of the output at the Hitport.
Dependencies
To enable this parameter, set the Outputparameter to Hit
or set it toCount and Hit
with the Countdata type parameter set toDouble
.
Block Characteristics
Data Types |
|
Direct Feedthrough |
|
Multidimensional Signals |
|
Variable-Size Signals |
|
Zero-Crossing Detection |
|
Algorithms
expand all
Scalar Input Operation
When you set the Count direction parameter toUp
, a trigger event at the Inc(increment) input port causes the block to increase the counter by one. Assuming noreset 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 eventoccurs at the Inc port, the block restarts the counter at0
and resumes increasing the counter by one for eachsubsequent trigger event at the Inc port.
When you set the Count direction parameter toDown
, a trigger event at the Dec(decrement) input port causes the block to decrease the counter by one. Assuming noreset events occur, the block continues decreasing the counter value when triggereduntil the counter value reaches zero. The next time a trigger event occurs at theDec port, the block restarts the counter at the upper-countlimit and resumes decreasing the counter by one for each subsequent trigger event atthe 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 specifiedby the Count event parameter occurs at the optionalRst input port. When theInc/Dec and Rstports receive trigger events simultaneously, the block first resets the counter andthen increments or decrements the counter appropriately. If you do not need to resetthe counter during simulation, you can disable the Rst port byclearing the Reset input check box.
The Output parameter allows you to specify which valuesthe block outputs:
Count
enables a Cnt outputport on the block. The Cnt port provides the currentvalue of the counter as a scalar value. The Cnt outputport has the same port sample time as theInc/Dec input port.Hit
enables a Hit outputport on the block. The Hit port produces zeros whilethe value of the counter does not equal any of the integers you specify forthe Hit values parameter. You can specify an integer ora vector of integers for the Hit values parameter. Whenthe counter value does equal one or more of the values you specify for theHit values parameter, the block outputs a value of1
at the Hit output port. TheHit output port has the same port sample time asthe Inc/Dec input port.Count and Hit
enables both theCnt and Hit outputports.
Vector Input Operation
The block treats vector inputs to theInc/Dec port as a frame. Vectoroperation is the same as scalar operation, except that the block increments ordecrements the counter by the total number of trigger events contained in theInc/Dec input vector. Thus, thecounter may change multiple times during the processing of a singleInc/Dec input vector.
When the block has a Hit port, the block outputs a value of1
if any of the Hit values match any ofthe counter values during the processing of theInc/Dec input vector.
When a trigger event splits across two consecutive vectors, that event is countedin the vector that contains the conclusion of the event. When theRst port receives a trigger event at the same time as theInc/Dec port, the block first resetsthe counter. The block then increments or decrements the counter by the number oftrigger events contained in the Inc/Decinput vector.
When the input to the Inc/Dec port is alength N vector, the port sample time of theInc/Dec input port is equal to theframe period of the input, or N times the sample time of theinput signal. The port sample time of the Cnt andHit output ports equals that of theInc/Dec input port.
Free-Running Operation
The block operates in free-running mode when you select Freerunning
for the Count eventparameter.
The Inc/Dec input port is disabled inthis mode, and the block simply increments or decrements the counter at the constantinterval, Ts, which you specify using theSample time parameter.
In this mode, the Rst port always behaves as if theCount event parameter were set to Non-zerosample
. Thus, the block triggers a reset event at each sample timethat the Rst input is not zero.
In this mode, the Cnt output is anM-by-1
vector containing the count valueat each of M consecutive sample times, where Mis the value you specify for the Samples per output frameparameter. The Hit output is anM-by-1
vector containing the hit status(0
or 1
) at each of thoseM consecutive sample times. Both the Cntand Hit output ports have a port sample time ofMTs.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using Simulink® Coder™.
Version History
Introduced before R2006a
See Also
Blocks
- Edge Detector | N-Sample Enable | N-Sample Switch
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:
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
- Deutsch
- English
- Français
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本 (日本語)
- 한국 (한국어)
Contact your local office