# Simulink counter block having an initial starting value of -1

조회 수: 88 (최근 30일)
Kenny 2020년 11월 27일
댓글: Kenny 2020년 12월 1일
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.
Thanks all!
##### 댓글 수: 3이전 댓글 1개 표시이전 댓글 1개 숨기기
Kenny 2020년 11월 30일
Hi Paul! The counter I was using is the 'count up' counter. The one with the Rst and Inc inputs, and the outputs are 'Cnt' and 'Hit'.
Kenny 2020년 12월 1일
Hi Jim! I decided to post this Simulink matlab R2019b file based on your example - just to let you know that I am making good use of your example for my 1 to 10 counter. Thanks so much again for your help and time!

댓글을 달려면 로그인하십시오.

### 채택된 답변

Jim Riggs 2020년 11월 29일
편집: Jim Riggs 2020년 11월 30일
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.
##### 댓글 수: 2없음 표시없음 숨기기
Jim Riggs 2020년 11월 29일
Oh, and I forgot to mention, just drop this counter into a triggered subsystem block and it will count triggered events.
Kenny 2020년 11월 30일
Jim! Thanks so much for your time and help - for teaching me that! That is really fantastic, and I'll learn (and have actually learned) from that. That is a really excellent method and setup that you showed. I'll pass this method that you taught to others too whenever they need to find a way to do what I needed. Thanks again Jim. Greatly appreciated! Thanks as well for that extra note about triggered subsystem!

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 General Applications에 대해 자세히 알아보기

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by