HDL Counter

Free-running or count-limited hardware counter

Library

HDL Coder / HDL Operations

Description

The HDL Counter block models a free-running or count-limited hardware counter that supports signed and unsigned integer and fixed-point data types.

The counter emits its value for the current sample time.

Control Ports

By default, the counter does not have input ports. Optionally, you can add control ports that enable, disable, load, reset or set the direction of the counter.

The table shows the priority of the control signals and how the counter value is updated in relation to the control signals.

Local reset, rstLoad trigger, loadCount enable, enbCount direction, dirNext Counter Value
1initial value
01load_val value
000current value
0011current value + step value
0010current value - step value

Count direction

The Step value parameter and optional count direction port, dir, interact to determine the actual count direction.

dir Signal ValueStep Value SignActual Count Direction
1+ (positive)Up
1- (negative)Down
0+ (positive)Down
0- (negative)Up

Dialog Box and Parameters

Counter type

Counter behavior.

  • Free running (default): The counter continues to increment or decrement by the Step value until reset.

  • Count limited: The counter increments or decrements by the Step value until it is exactly equal to the Count to value.

Initial value

Counter value after reset. The default is 0.

Step value

Value added to counter at each sample time. The default is 1.

Count to value

When the count is exactly equal to Count to value, the count restarts at the Initial value. This option is available when Counter type is set to Count limited. The default is 100.

Count from

Specifies the parameter that sets the start value after rollover. When set to Specify, the Count from value parameter is the start value after rollover. The default is Initial value.

Count from value

Counter value after rollover when Count from is set to Specify. The default is 0.

Local reset port

When selected, creates a local reset port, rst.

Load ports

When selected, creates a load data port, load_val, and load trigger port, load.

Count enable port

When selected, creates a count enable port, enb.

Count direction port

When selected, creates a count direction port, dir.

Counter output data is

Output data type signedness. The default is Unsigned.

Word length

Bit width, including sign bit, for an integer counter; word length for a fixed-point data type counter. The minimum value if Output data type is Unsigned is 1, 2 if Signed. The maximum value is 125. The default is 8.

Fraction length

Fixed-point data type fraction length. The default is 0.

Sample time

Sample time. The default is 1.

This parameter is not available, and the block inherits its sample time from the input ports when any of these parameters is selected:

  • Local reset port

  • Load ports

  • Count enable port

  • Count direction port

Ports

The block has the following ports:

rst

Resets the counter value. Active-high.

This port is available when you select Local reset port.

Data type: Boolean

load

Sets the counter to the load value, load_val. Active-high.

This port is available when you select Load ports.

Data type: Boolean

load_val

Data value to load.

This port is available when you select Load ports.

Data type: Same as count.

enb

Enables counter operation. Active-high.

This port is available when you select Count enable port.

Data type: Boolean

dir

Count direction. This port interacts with Step value to determine count direction.

  • 1: Step value is added to the current counter value to compute the next value.

  • 0: Step value is subtracted from the current counter value to compute the next value.

This port is available when you select Count direction port.

Data type: Boolean

count

Counter value.

Data type: Determined automatically based on Counter output data is, Word length, and Fraction length.

Was this topic helpful?