Propeller: How to be sure input changes state before it is counted
realolman1
Posts: 55
I want to count pulses on an input. I want to be sure that the input has changed state before I
count it . How do I do that?
thanks
count it . How do I do that?
thanks
Comments
-Phil
If the pulse frequency is low you can use waitcnt to wait for the pin to go high (or low) and then wait for the pin to go low (or high) before counting it as a valid pulse. This will stop the program execution for each waitcnt so is not always good if the program needs to be doing other things. Of course you could use another cog for counting if that is the case.
Another option would be to read the state of the pin in a loop and add 1 to the count each time the pin goes from low to high and back to low (or vice-versa).
-Phil
thanks
Welcome to the forums!
Application Note AN001 on this page:
http://www.parallaxsemiconductor.com/appnotes
deals with the counters.
thanks for the info... and the welcome ...if anybody cares I was here before, but I seem to have lost my user name because I have changed email addresses ...so now I have suffixed a 1
I liked that name ... I started using it a long time ago when I went to a BASIC language forum and it seemed like everyone there was about 12 years old
I used a counter to detect the edge transitions and I also used a PASM loop to monitor the input state. I used the two different techniques in order to compare how well they worked at detection transitions. The counter could detect changes much faster than the PASM loop could.
There's some discussion about the differences in this thread.
I wish I had thought to use the waitpne technique so I could have included that technique in the test code.
I used the instructions found in the Propeller Education Kit (PEK) to learn how to use the Propeller's counter for this particular application. The PEK is available in the Propeller Tool's Help menu.
I've collected a list of links to Propeller tutorials in post #3 of my index (see signature).
This sounds like a question about hysteresis to me - if the source of pulses is putting out
a clean logic signal there's no issue (you can't detect input changing till its already changed
due to the fast edges of a logic signal)
If its a slowly varying or noisy analog signal there is an issue and we'd need more information
on the source of pulses to comment on a solution.
I didn't know how I was going to count them ... if for example you were to use a loop - which is probably a lousy way to do it - how would you know the high you see in this iteration is not the same high you saw in the last one...
what I am interested in is a 1200 PPR encoder with quadrature I'd like to keep track of its position by counting its pulses and which direction it's going. I am new at this propeller thing, and I have spent a good deal of time experimenting with the serial communication... I think I'm getting a handle on that, so now it's on to pulse counting ...
anyone want to offer advice on encoders and quadrature, I'd be happy to hear, er, read it
if now HIGH and was LOW, count ++
Or in PASM (which you probably need for speed)
(Although for quadrature you need to monitor two pins, not one).
A1 is the current state, and A2 is the previous state. The AND NOT (&!) logic adds one to the accumulator only when there is a 0 to 1 transition. It can be sampling at 12.5ns intervals with clkfreq=80MHz, so it is capable of detecting rapid transitions.
For an encoder, you need to look at two bits, A and B. First, look for there to be a change, then compare the previous state of A to the current state of B. If they are the same, then the rotation is one way, and if different, the rotation is the opposite way.
http://www.savagecircuits.com/showthread.php?325-Frequency-Counter-SPIN-PASM