Main Content

Counter Free-Running

Count up and overflow back to zero after reaching maximum value for specified number of bits

  • Counter Free-Running block

Libraries:
Simulink / Sources
HDL Coder / Sources

Description

The Counter Free-Running block counts up until reaching the maximum value, 2Nbits – 1, where Nbits is the number of bits. Then the counter overflows to zero and begins counting up again.

After overflow, the counter always initializes to zero. However, if you select the global doubles override, the Counter Free-Running block does not wrap back to zero.

Note

This block does not report wrap on overflow warnings during simulation. To report these warnings, see the Simulink.restoreDiagnostic reference page. The block does report errors due to wrap on overflow.

Examples

expand all

This example shows how to specify the Number of bits in the Counter Free-Running block as a positive integer.

At t = 255, the counter reaches the maximum value of (2^8)-1. If you increase the stop time of the simulation to 256, the counter wraps to zero.

This example shows how to specify the Number of bits in the Counter Free-Running block as an unsigned integer expression.

At t = 254, the counter reaches the maximum value of uint8(2^(uint8(8))-1). If you increase the stop time of the simulation to 255, the counter wraps to zero.

Ports

Output

expand all

Count value, returned as an unsigned integer with a length specified by the Number of Bits parameter. When the number of bits is anything other than 8, 16, 32, or 64, the output type is an unsigned fixed-point data type.

Data Types: uint8 | uint16 | uint32 | uint64 | fixed point

Parameters

expand all

Specify the number of bits as a finite, real-valued. When you specify:

  • A positive integer, for example 8, the block counts up to 28 – 1, which is 255.

  • An unsigned integer expression, for example uint8(8), the block counts up to uint8(2uint8(8) – 1), which is 254.

Programmatic Use

Block Parameter: NumBits
Type: character vector
Values: scalar
Default: '16'

Specify the time interval between samples as a scalar (sampling period), or a two-element vector ([sampling period, initial offset]). To inherit the sample time, set this parameter to -1. For more information, see Specify Sample Time.

Programmatic Use

Block Parameter: tsamp
Type: character vector
Values: scalar | vector
Default: '-1'

Block Characteristics

Data Types

fixed point | integer

Direct Feedthrough

no

Multidimensional Signals

no

Variable-Size Signals

no

Zero-Crossing Detection

no

Extended Capabilities

PLC Code Generation
Generate Structured Text code using Simulink® PLC Coder™.

Fixed-Point Conversion
Design and simulate fixed-point systems using Fixed-Point Designer™.

Version History

Introduced before R2006a