Electronic – Count the clock cycle using LTSpice

capacitorcounterltspicemodelingsimulation

I had been using these modeling technique for counting number of clock cycles.

Delay is created and inverted – to get small positive edge of the clock and count numbers is charged based on the number of pulses. But this needs to change value of R2 and C2 and counts are sometimes greater or less than integers.
Is there any other alternatives so that we can count number of clock cycles and reset it?

Schematic

Waveform

Best Answer

What you need is a resettable integrator. You have a nice approach, but you should really take time to read the manual, you'd find out that there are quite a few surprises in there.

For example, if an A-device is referenced directly to the ground, you don't need to connect the unused inputs to the 8th pin, they can be left floating. Tying them up to the 8th pin is done internally (i.e. the gate does not act like a real-life case with floating inputs).

Another one is that behavioural if() expressions introduce discontinuities around the jump and may cause, more often than not, timestep too small errors, or similar. You could use a bi source, instead of a bv, which allows internal parasitics Rpar/Cpar, which give you a time constant which smooths out a bit the sharp transition. The better solution is to use an A-device, which is far superior both in terms of convergence and runtime (the builtin parameters allow for many tweaks).

These said, here is a version of a resettable integrator:

test

A1 and A2 form the integrator, driven by the complementary clocks generated by A4. The tau is neded to allow for smooth rise/fall times whose trigger points for the two S&Hs fall after the trigger point of the input signal. This can be done with setting ref in the S&H, but this way is clearer and also affects both S&Hs at once. A2 receives the output of A1 on its inverting input, so that the output can go back into A1's inverting input, creating the needed positive feedback for an integrator. A3 is a mota (multiplying OTA) that takes care of the reset function (active low). The reset signal should be strictly 0 V or 1 V, since A3 is a multiplier. If the external signal is not with these limits, it can be adjusted by either an A-device gate, or by the u(), or buf() (or inverting inv()) functions.

A few things to notice: LTspice's S&H has a default upper/lower limit of +/-10 V, which don't act as hard limits, rather they attenuate by a factor of 1000 (or so) everything going beyond the limits. This means that a 1001 V input and a limit of vhigh=1 will result in a 2 V output, not 1 V. The 2nd S&H has vlow set, because it's in an inverting configuration. A3 has the linear flag, which makes its output to vary linearly until vhigh/vlow, after which, similar to its S&H cousin (both A-devices), it does not hard-limit the output, rather it attenuates by a factor of around 2 (same link as for the S&H). Lastly, the output starts from 1, since it's considered that a trigger can only come after a pulse, but if this behaviour is not wanted, add a current source of 1 mA at the output, drawing current to the ground. This, combined with the default output resistance for the S&H A-devices of 1 kOhm (an exception to the rest of the A-devices, can be changed by setting rout), creates a -1 V voltage that is subtracted from the output.