Simulink counter block having an initial starting value of -1
26 views (last 30 days)
Hello MathWorks Simulink members!
I'm just trying to make a counter that counts from 1 to 10 repeatedly.
Can I ask if Simulink counter blocks have the option for making the 'initial' count to be -1?
I notice that there is a Simulink counter block that we can set up that counts from 0 to 9, but lowest initial count we're allowed to set is '0'. So I can't use that particular block right away for doing what I need to do, which is namely to count from 1 to 10 repeatedly.
The problem is described as follows:
If I have a square-wave input to the counter, then the 0 to 9 counter (initialised to a count of 0) must initially wait for a trigger edge. The first trigger edge will then make the counter jump to 1. So, for the first full set of counts, the counter actually counts from 1 through to 9, which results in nine counts for the very first set of counts.
The counter will then cycle back to 'zero' at the start of the next trigger edge. So the second set of counts will involve 0 through to 9, which results in the desired number of TEN counts.
But --- going back to the very first set of counts, I actually want to count ten values, which cannot be achieved because there appears to be no option for the initial count to be set to -1.
If there were the option for setting an initial count of -1 for the Simulink counter block, then my very first set of counts (after detecting the first incoming trigger pulse) would be from 0 to 9 ----- giving my desired full set of 10 counts (since 0 to 9 gives 10 values).
I'm just trying to get a counter that counts from 1 to 10. I could get this done with a Simulink counter block that counts from 0 to 9 (with first-time startup initial count of -1, but it looks like Simulink counter blocks don't allow for an initial count of -1).
Can existing Simulink counter blocks be made to provide a '-1' initial count?
My aim is to just count incoming pulses, such as pulse 1 to pulse 10 ------ and then carry out some software activity immediately once the 10th pulse comes in. And then repeatedly count the next 10 pulses (1 to 10), and so on.
I don't think I can get this done so easily with a 0 to 9 counter that has no feature for setting the initial count to be -1.
Could a Simulink member recommend a nice approach for doing what I'm aiming to do? At the moment, I'm thinking of using two standard Simulink 0 to 9 counters, and using simulink Switch, and If-Action blocks, and Data Memory Store features for achieving a "1 to 10" counter. But it might be overkill. If there is an easier method --- then I'll be happy to go for the simpler and more efficient ways.
Jim Riggs on 29 Nov 2020
Edited: Jim Riggs on 30 Nov 2020
Yes, the two counter blocks in the library do not allow you to set the initial value.
Whenever I need to use a counter, I usually build my own simple counter:
The memory block will accept an initial value, so you can set the staring value to whatever you want. Simply set the initial value of the memory block to the desired start value -1 (since 1 gets added from the start). (e.g. if you want the output to be -1 at the start, set the memory initial condition to -2)
This counter can then be modified to reset at a specified threshold:
The added constant block and switch now allow you to specify the maximum count value (inside the switch) and the value to "reset" to in the constant block.