← All exercises

Count Up Down with Bounds

medium

Bidirectional counter with upper and lower bounds, and flag outputs when the limits are hit.

What it teaches

A bidirectional counter that respects a Min and Max. Hit the top — the count pins to Max and the at-max flag turns on. Hit the bottom — pins to Min and the at-min flag turns on. Useful when a tracked balance needs both directions and known boundaries.

Inputs and outputs

TagTypeDefaultDescription
In_CountUpBOOLCount up pulse
In_CountDownBOOLCount down pulse
Cfg_MinDINT0Lower limit for the count
Cfg_MaxDINT3Upper limit for the count
Out_CountDINTCurrent bounded count
Sts_AtMaxBOOLAt maximum limit flag
Sts_AtMinBOOLAt minimum limit flag

Required behavior

  • Out_Count increments by 1 on the rising edge of In_CountUp.
  • Out_Count decrements by 1 on the rising edge of In_CountDown.
  • Out_Count is unconditionally bounded between Cfg_Min and Cfg_Max.
  • Sts_AtMax is 1 when Out_Count is greater than or equal to Cfg_Max.
  • Sts_AtMin is 1 when Out_Count is less than or equal to Cfg_Min.