Problem counting pulses
Benj
Posts: 66
I am attempting to get my prop to count pulses on a pin. I started with the Motor Minder spin code and read the ctra and associated passages in the manual about 15 times and I think I understand what it going on. I extracted out a part of the code that should (I think) monitor pulses on pin 7 and accumulate them in phsa. I have an optocoupler that is putting out a 60Hz square(ish) wave as my pulse source from an A/C input. In principle, this code works. If I turn the A/C on, it begins counting, off and it stops. The problem is that the count is growing at a much faster rate than 60 per second.
I was assuming that it would only increment phsa once per positive edge. Is it incrementing every clock cycle that the pin is high? or?? I ran it for 10 seconds (using my watch, not code) and the count was just over 38,000, where it should have been 600.
Does anyone have any ideas? Does the pulse source need to be cleaner? Is it a simple coding problem?
PUB Main LCDStart LCD.tx($11) ' backlight on LCD.tx($16) ' LCD on; cursor off, blink off LCD.tx($0C) ' clear LCD cognew(ClockInput, @ClockInputStack) repeat LCD.tx($94) LCD.dec(pulsecount) PUB ClockInput dira[7]~ ' set ClockPulsePin to input 'start A/C Hz counter in counter A frqa := 1 ctra := 0 ' stop counter phsa := 0 ' zero counter ctra := (%01010 << 26 ) | (7) ' start counting positive edges in counter A repeat pulsecount := phsa
I was assuming that it would only increment phsa once per positive edge. Is it incrementing every clock cycle that the pin is high? or?? I ran it for 10 seconds (using my watch, not code) and the count was just over 38,000, where it should have been 600.
Does anyone have any ideas? Does the pulse source need to be cleaner? Is it a simple coding problem?
Comments
I have to be honest and say I don't fully understand the counters so debugging them is hard for me, but the following code works for me and I think we're doing similar things. I want to watch the number of pulses on 2 input pins, then once a second write out the values. I think all you need to do is remove the accumulator reset and 1 second delay stuff.
Comparing your code to mine the only difference I see is a "+" instead of an "|" when setting up ctra which seem to be the same thing in this instance.
What about editing your stuff to only run for 10 seconds then output?
Sorry I'm not more helpful. Hopefully someone else can chime in.
Peter
I hope that helps
Try posting all of your code either within the code brackets or using the ( File->Archive->Project...) from the Propeller IDE and attaching it.
The Counter mode that you are using is correct ... %01010 - POSEDGE detector which compares the input delayed by 1 clock with the inverted input delayed by 2 clocks. A logical AND is performed within the counter logic and if the result is "1" then a transition from LOW to HIGH occurred and phsa is incremented by the amount specified in frq. This way it doesn't accumulate phsa on every single clock when the input is HIGH.
Here is some code that is basically the core of what you posted, but modified to display on the Parallax Serial Terminal. The test setup for the code below: Pin 5 is connected to Pin 7 via a 330 Ohm resistor. In this code, it runs for 10 seconds, and the final displayed output is 600 as you indicated in your first post.
can you show your schematic? It can be a hardware proplem if the voltage at the input pin rises too slow and is noisy then the fast pos-detector counts more than 1 edge.
You can try to replace the ClockInput routine with this code: it does the same as the counter but much slower (with Spin speed).
Andy
and all of the code:
Ariba, your code works with or without the current limiting resistor. On the one hand, I'm inclined to just go with it, but would also like to know why my code doesn't work.
Andy
[Edit] I see Andy suggested the same thing.
Your code works, so the problem 'must' be in hardware. The Scope image that you attached, was that off of P7 of your circuit?
The 330 Ohm series resistor was for DEBUG only and was meant to be tied between P5 and P7... the example I posted provided a 60Hz square wave reference from P5.
You can eliminate needing to use a cog for the counter function (see code below)
I am going to try a schmitt trigger next if I can get my hands on one.
Tracy, 120V. I don't have an output resistor, unless you are talking about the 4.7k. If so, would I put the positive on the collector side and the negative on the P7 side? or the positive on the collector and negative on ground?
and change the counter initialization to: A schmitt trigger needs a positive feedback to the input. The Counters have only a negative feedback in PosEdge mode, so you can use an auxiliary pin and a second counter to generate a positive feedback. This is not ideal because the feeback is delayed by 2 clock pulses, I hope the noise is not at so high frequencies that this matters. Otherwise a little filter as Tracey Allen described would help.
Andy
...where the Prop Threshold would be switching, the signal looks absolutely fine. A Schmitt Trigger in my honest opinion would only be necessary if there were a lot of noise, and that I don't see.
Benj,
Can you zoom in any closer to one of the pulses, and post that image?
Also, just a wild guess, what does the supply voltage noise look like? ... by the numbers that you are reporting it seems that there is a frequency component of about 3.8 kHz ... 4 kHz coming from somewhere, most likely not the AC line. What else do you have connected to the Propeller?