Shop OBEX P1 Docs P2 Docs Learn Events
Propeller: How to be sure input changes state before it is counted — Parallax Forums

Propeller: How to be sure input changes state before it is counted

realolman1realolman1 Posts: 55
edited 2014-03-21 10:35 in General Discussion
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

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2014-03-16 16:35
    You can use one of the hardware counters in edge-counting mode. Or, if the pulses are coming slowly enough, you can do it in a program loop: first wait for the input to be low, then wait for it to become high, at which point you add one to the count, then repeat the loop.

    -Phil
  • kwinnkwinn Posts: 8,697
    edited 2014-03-16 16:41
    You can use the counters in each cog for that, or:

    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 Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2014-03-16 17:35
    If the pulse frequency is low you can use waitpeq 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 waitpeq 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.

    -Phil
  • realolman1realolman1 Posts: 55
    edited 2014-03-17 03:18
    could you point me to where I could get some Instructions on using the counter?

    thanks
  • PublisonPublison Posts: 12,366
    edited 2014-03-17 03:26
    realolman1 wrote: »
    could you point me to where I could get some Instructions on using the counter?

    thanks

    Welcome to the forums!

    Application Note AN001 on this page:

    http://www.parallaxsemiconductor.com/appnotes

    deals with the counters.
  • realolman1realolman1 Posts: 55
    edited 2014-03-18 18:09
    Publison wrote: »
    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
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-03-18 18:42
    I posted some simple code to count encoder pulses here.

    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).
  • Mark_TMark_T Posts: 1,981
    edited 2014-03-19 09:23
    realolman1 wrote: »
    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

    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.
  • kwinnkwinn Posts: 8,697
    edited 2014-03-19 16:25
    Thanks for the correction Phil, I should know better than to post when I am that tired.
  • realolman1realolman1 Posts: 55
    edited 2014-03-20 03:08
    Mark_T wrote: »
    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
  • Mark_TMark_T Posts: 1,981
    edited 2014-03-20 11:29
    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...

    if now HIGH and was LOW, count ++

    Or in PASM (which you probably need for speed)
    :loop        waitpeq pinmask, pinmask
                 add     counter, #1
                 wrlong  counter, counter_address
                 waitpne pinmask, pinmask
                 jmp     #:loop
    

    (Although for quadrature you need to monitor two pins, not one).
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2014-03-20 12:26
    As the Propeller counter module does it, it samples the input pin at each clock cycle and compares the previous state with the current state. Here is the counter documentation for the POSEDGE mode, the one that counts 0-->1 rising edges.
    Screen shot 2014-03-20 at 12.13.57 PM.png

    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.
    [INDENT]
    [SIZE=1][FONT=courier new]0     1    3     2     0  state
    ----> ----> ----> ---->    CW
     
    0  _  1  _  1  _  0  _  0  B
      /     /     /     /               change is 0->1 or 1->0
    0  -  0  -  1  -  1  -  0  A 
     
     
     
     
    0     1     3     2     0   state
     <---- <---- <---- <----    CCW
     
    0  _  1  _  1  _  0  _  0   B
        \     \     \     \      change is 0->0 or 1->1
    0  -  0  -  1  -  1  -  0   A
     
     [/FONT][/SIZE]
    [/INDENT]
    The code for this is[INDENT]
    direction := old ^ new ' 1 if CW, 0 if CCW[/INDENT]
    

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-03-20 14:02
    Here's an actual frequency counter program I wrote a few years ago while testing encoders. One of the programs tells you how many pulses occurred within the time period IIRC.

    http://www.savagecircuits.com/showthread.php?325-Frequency-Counter-SPIN-PASM
  • realolman1realolman1 Posts: 55
    edited 2014-03-21 10:35
    You guys are really something. Thanks
Sign In or Register to comment.