Count up or down through specified range of numbers (2024)

Table of Contents
Description Examples Count Down Through Range of Numbers Ports Input Inc/Dec — Input signal to trigger count eventscalar | vector Rst — Reset signal scalar Max — Maximum counter size scalar Output Cnt — Current value of counterscalar | vector Hit — Hit status scalar | vector Parameters Count direction — Count up or down Up (default) | Down Count event — Type of trigger event Rising edge (default) | Falling edge | Either edge | Non-zero sample | Free running Counter size — Range of integer values to count through 8 bits (default) | 16 bits | 32 bits | User defined | Specify via input port Maximum count — Maximum value of counter 255 (default) | positive integer Initial count — Initial value of counter 0 (default) | integer ≥ 0 Output — Output count value, hit value, or both Count (default) | Hit | Count and Hit Hit values — Count values to flag 32 (default) | scalar | vector Reset input — Enable Rst input port on (default) | off Samples per output frame — Number of samples in each output vector 1 (default) | positive integer Sample time — Sample time in Free Running mode 1 (default) | -1 | scalar ≥ 0 Count data type — Data type of Cnt port double (default) | single | int8 | uint8 | int16 | uint16 | int32 | uint32 Hit data type — Data type of Hit port Logical (default) | Boolean Block Characteristics Algorithms Scalar Input Operation Vector Input Operation Free-Running Operation Extended Capabilities C/C++ Code Generation Generate C and C++ code using Simulink® Coder™. Version History See Also Blocks MATLAB Command Americas Europe Asia Pacific FAQs

Main Content

Count up or down through specified range of numbers

expand all in page

  • Count up or down through specified range of numbers (1)

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

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.

Count up or down through specified range of numbers (2)

The following figure shows the first 22 samples of the four-column output, dsp_examples_yout.

Count up or down through specified range of numbers (3)

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

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

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

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

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 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

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 toUp 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 toDown 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

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).

    Count up or down through specified range of numbers (4)

  • 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).

      Count up or down through specified range of numbers (5)

  • Either edge — Triggers a countor reset operation when the input to theInc/Dec orRst port is a Risingedge or Fallingedge.

  • 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.

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 toUp and the counter reaches the upperlimit of the counter range, the block restarts the counter atzero.

  • When you set the Count direction parameter toDown 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 of 0 to255.

  • 16 bits — Specifies a counterwith a range of 0 to65535.

  • 32 bits — Specifies a counterwith a range of 0 to 2321.

  • 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 from 0 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 from 0 to the value you specify asan input to the Max port.

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.

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

Select the output ports to enable. You can choose to enable theCount, Hit, orCount and Hit ports.

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 Hitor Count and Hit.

Tunable: Yes

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.

Specify the number of samples, M, in each output vectoras a positive integer.

Dependencies

To enable this parameter, set the Countevent to Free running.

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.

Specify the data type of the output at the Cntport.

Dependencies

To enable this parameter, set the Outputparameter to Count or Count andHit.

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

Boolean | double | integer | single

Direct Feedthrough

no

Multidimensional Signals

no

Variable-Size Signals

no

Zero-Crossing Detection

no

Algorithms

expand all

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.

Count up or down through specified range of numbers (6)

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

Contact your local office

Count up or down through specified range of numbers (2024)

FAQs

What does the counter function do in Matlab? ›

Counter returns a counter System object™, count , that counts up when the input is nonzero. count = dsp. Counter( Name,Value ) returns a counter System object, count , with each specified property set to the specified value.

How to use counter block in simulink? ›

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.

How to use memory block in simulink? ›

The Memory block holds and delays its input by one major integration time step. When placed in an iterator subsystem, it holds and delays its input by one iteration. This block accepts continuous and discrete signals. The block accepts one input and generates one output.

What is up counter and down counter instructions? ›

The Up / Down Counter (UDC) instruction is used to accumulate OFF to ON and ON to OFF state changes of input logic, counting upward from 0 toward an optional preset value and counting downward from a second, optional preset value toward 0.

What is the combination of up and down counter? ›

Up/Down counter is the combination of both the counters in which we can perform up or down counting by changing the Mode control input. Design of 3 bit Asynchronous up/down counter : It is used more than separate up or down counter. In this a mode control input (say M) is used for selecting up and down mode.

What is the function of down counter? ›

In Up counter when contact change over takes place accumulator value increments by one. While in down counter when changeover takes place accumulator value decrements by one. The instruction blocks of up counter and down counter are as shown below.

How does counter () work? ›

Counter is a subclass of dict that's specially designed for counting hashable objects in Python. It's a dictionary that stores objects as keys and counts as values. To count with Counter , you typically provide a sequence or iterable of hashable objects as an argument to the class's constructor.

What is counter function used for? ›

counter() The counter() CSS function returns a string representing the current value of the named counter, if there is one. The counter() function is generally used within pseudo-element through the content property but, theoretically, it can be used wherever a <string> value is supported.

What is a counter block? ›

A counter block increments its value by a specified amount each time the simulation passes through it, and then passes the recorded value to the next block in the flowchart. It may have multiple incoming and outgoing paths.

How do you use counter variables? ›

The counter variable is declared and used in the same way as the normal variables are declared and used. The counter variable can be of only integer type because it is very easy to increase the value of integer type variable. Before the repeating cycle, we have to initialize it to zero or one.

What is clock block in Simulink? ›

The Clock block outputs the current simulation time at each simulation step. This block is useful for other blocks that need the simulation time. When you need the current time within a discrete system, use the Digital Clock block.

What is bus block in Simulink? ›

Each Bus Creator block groups the elements connected to its input port into a bus. A bus represents a set of elements, analogous to a bundle of wires tied together. For example, the bus created by the Bus Creator block named Bus Creator 1 contains the signals sine and chirp , which are connected to its input ports.

How do you flip blocks vertically in Simulink? ›

To flip a block, select the block and, on the Format tab, click Flip left-right or Flip up-down . You can flip a block horizontally or vertically based on the orientation of the block ports.

Top Articles
Latest Posts
Article information

Author: Ouida Strosin DO

Last Updated:

Views: 5788

Rating: 4.6 / 5 (56 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Ouida Strosin DO

Birthday: 1995-04-27

Address: Suite 927 930 Kilback Radial, Candidaville, TN 87795

Phone: +8561498978366

Job: Legacy Manufacturing Specialist

Hobby: Singing, Mountain biking, Water sports, Water sports, Taxidermy, Polo, Pet

Introduction: My name is Ouida Strosin DO, I am a precious, combative, spotless, modern, spotless, beautiful, precious person who loves writing and wants to share my knowledge and understanding with you.