LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Making a counter with enable and reset

sorry forgot the attachement 

0 Kudos
Message 11 of 16
(2,115 Views)

Hi All,

I’m trying to implement a counter with enable and reset.

 

  1. Increment from 0 to “number of pulses” when “enable” is true and output Q is 1.
  2. If enable is false, Q is 0.
  3. Once the counter reaches “number of pulses”, the counter has to reset to 0.
  4. Step 1-2 only happens when enable is true.
0 Kudos
Message 12 of 16
(2,110 Views)

I think the problem you are having is that you have not clearly defined the problem.  I'm not sure what I'm about to say describes the task you are undertaking, but it at least should be unambiguous enough to determine if it does or not ...

  1. You are building a Counter that counts up to some number, then restarts from 0.
  2. Question -- if the count is 5, should it go "1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5", "1, 2, 3, 4, 5, 1, 2, 3, 4, 5", or "0, 1, 2, 3, 4, 0, 1, 2, 3, 4"?  Note that the first example counts to 5, then 6, 6, 6, while the other loops cycle after 5 numbers.
  3. There is a Boolean Enable input that must true for the Counter to Count.
  4. There is an output, Q (Boolean? Numeric?) that seems to be 1 when the Counter is counting and 0 when it is not.
  5. There is a number, N, which is the cycle length of the number (see Point 1, above).

Something like this, where you need to maintain an internal "State" (or "Count"), is something that many LabVIEW users call an Action Engine (if you Google "LabVIEW Action Engine", you can probably find very useful material).  You might consider building this VI as an Action Engine, possibly with the following Actions:

 

  • Initialize -- takes the number N and saves it for later use.  Also resets the Count to 0 and the Enable flag to False.
  • Enable -- sets the Enable flag to True.
  • Disable -- sets the Enable flag to False.  Might also reset the Count to 0.
  • Count -- if Enable is set, increments Count, resetting it as appropriate.

Note that I haven't mentioned the output Q.  Did you also want to output the current Count?  What happens (or what do you want to happen) if you Count before you Enable, or Enable before you Initialize?

 

Note that all of this can be handled by a single VI, consisting of a "Do Once" While Loop (a While Loop with the constant True wired to the Stop indicator), some Shift Registers, and a Case Statement.  An Enum that takes on the values Initialize, Enable, Disable, and Count, is also useful, especially for wiring to the Case Selector and making your code somewhat self-documenting.

 

Bob Schor

0 Kudos
Message 13 of 16
(2,081 Views)

@roseh33 wrote:

Hi All,

I’m trying to implement a counter with enable and reset.

 

  1. Increment from 0 to “number of pulses” when “enable” is true and output Q is 1.
  2. If enable is false, Q is 0.
  3. Once the counter reaches “number of pulses”, the counter has to reset to 0.
  4. Step 1-2 only happens when enable is true.

  • Please attach the actual VI, not a picture. We cannot debug a picture.
  • Where is the terminal for "q"?
  • What is the purpose of the loop on the right? Just to burn the CPU by running ot at 100% forever?
  • Where is the terminal for the stop button?
  • What is the point of all these local variables? Virtually none are needed.
  • What is the point of the 10ms delay in the small case? Seems pointless.
  • Please define the problem better.
  • There is a primitive for "+1".
  • Why is the shift regsiter initialized with the wrong datatype?
  • ...

All you need is a shift register containing the count. Divide by the number of pulses using Quotient&remaider and only keep the remainder in the shift register. Increment if the switch is true, else don't increment. Code would take 10% of what you currenty have. Keep it simple!

 

Here's a draft. Modify as needed.

 

 

0 Kudos
Message 14 of 16
(2,062 Views)

Sorry I am new to labview community.  Here's my vi.  

0 Kudos
Message 15 of 16
(2,037 Views)

@roseh33 wrote:

Here's my vi.  


  • Get rid of these local variables, they don't serve any purpose and just introduce race conditions.
  • Place the "q" terminal outside the small case and wire it directly to the AND operation. No case structure or local variables needed.
  • Place the number of pulses before the case structure and branch the wire to the two locations where it is needed.
  • remove the "enable" .local variables and brach the wire from the "enable" terminal. There is no need to wire the inverted value to the or operation in the upper left. Makes no difference.
  • If you would tap into the wire before the +1 for the equal comparison, you can also delete the +1 in the orange wire.
  • Make "number of pulses" an integer datatype, there should be no orange anywhere. (right-click..representation).
  • Initialzie the shift register with the correct datatype. Make that red got go away.

Have a look at my code above, much simpler! You can easily add a reset function.

0 Kudos
Message 16 of 16
(2,029 Views)