Professional Documents
Culture Documents
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. The Inc and Dec ports accept real-valued scalars or vectors. If the input to the Inc or Dec port is a vector, the block treats the vector as a frame. The Rst port only accepts real-valued scalars. The Rst port must have the same port sample time as the Inc or Dec input port. If you enable the optional Max input port, you must provide an unsigned integer input that the Count data type can represent. See the following topics for more information: Setting the Count Event Parameter Setting the Counter Size and Initial Count Parameters Scalar Input Operation Vector Input Operation Free-Running Operation
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 a Rising edge or Falling 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.
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 of 0 to 255 . 16 bits Specifies a counter with a range of 0 to 65535 . 32 bits Specifies a counter with a range of 0 to 2 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 from 0 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 from 0 to the value you specify as an input to the Max port.
32
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 M*Ts.
Examples
In the following model, ex_counter_ref, the Simulink 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 model's four-column output, 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.
Dialog Box
Count direction 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 in Simulink Normal mode. Count event 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. 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. For more information on all the possible settings of this parameter, see Setting the Count Event Parameter. Counter size Specify the range of integer values the block counts through. For more information about the valid values of this parameter, see Setting the Counter Size and Initial Count Parameters. Maximum count Specify the maximum value of the counter as any unsigned integer representable by the data type you specify for the Counter data type parameter. This parameter appears only when you set the Counter size parameter to User defined. Tunable in Simulink Normal mode. Initial count Specify the initial value of the counter. 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. Output Select the output ports to enable. You can choose to enable the Count, Hit, or Count and Hit ports. Hit values 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. Reset input
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 Specify the number of samples, M, in each output vector. This parameter appears only when you set the Count event parameter to Free
running .
Sample time Specify the constant interval, Ts , at which the block increments or decrements the counter when in free-running mode. 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 M*Ts . This parameter appears only when you set the Count event parameter to Free running. Count data type Specify the data type of the output at the Cnt port. This parameter appears only when you set the Output parameter to Count or Count and Hit. Hit data type Specify the data type of the output at the Hit port. This parameter appears only when you set the Output parameter to Hit or set it to Count and
Hit with the Count data type parameter set to Double .
Inc/Dec
Double-precision floating point Single-precision floating point Boolean 8-, 16-, and 32-bit signed integers 8-, 16-, and 32-bit unsigned integers
Rst
Double-precision floating point Single-precision floating point Boolean 8-, 16-, and 32-bit signed integers 8-, 16-, and 32-bit unsigned integers
Max
Cnt
Double-precision floating point Single-precision floating point 8-, 16-, and 32-bit signed integers 8-, 16-, and 32-bit unsigned integers
Hit
Logical Boolean The block might output Boolean values from the Hit output port depending on the setting of the Hit data type parameter.
See Also
Edge Detector N-Sample Enable N-Sample Switch DSP System Toolbox DSP System Toolbox DSP System Toolbox