Shop OBEX P1 Docs P2 Docs Learn Events
Up - Down Counter — Parallax Forums

Up - Down Counter

pogerttpogertt Posts: 33
edited 2009-10-02 04:49 in Propeller 1
Is there a counter mode that will:

Count up on a low to high transition of A, When B is high,

Count down on a low to high transition of A, When B is low?

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-10-01 16:30
    No

    The counter modes all count up only. Read the application note on the counters (AN001) for details. It's on the Propeller downloads webpage.

    If you have a spare cog, it's easy to write a short routine either in Spin or assembly (depending on the timing needed) to do what you want.

    You could use two counters and an external quad NAND gate to provide A & B and A & !B, then use the NEGEDGE mode so that one counter counts low to high transitions when B is high and the other counts low to high transitions when B is low and you subtract the 2nd count from the 1st count to get your result.

    Note that B can only change when A is low or you'll get false counts.
  • pogerttpogertt Posts: 33
    edited 2009-10-01 16:47
    Thanks for verifing that the counters won't count down.

    I am watching a quadrature encoder. There are several objects that I have found and are usable, the fastest determines if movment has ocured in 48 clock pulses. While 48 / 80 000 000 (1,666,666.666 samples a second) is way faster than required, the can I figure a way to do it faster bug has bitten me.
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2009-10-01 18:56
    pogertt said...
    Thanks for verifing that the counters won't count down.

    I am watching a quadrature encoder. There are several objects that I have found and are usable, the fastest determines if movment has ocured in 48 clock pulses. While 48 / 80 000 000 (1,666,666.666 samples a second) is way faster than required, the can I figure a way to do it faster bug has bitten me.

    I think you have 3 too many 6's. 1.6 million times per second is very different than 1.6 billion. I definitely think you can do it in less than 48 cycles. I just put together a mock program that should be able to count cycles and add or subtract based on a pin in 26 cycles (~3.1MHz).

    This is untested...but maybe code this this?
    :loop                   WAITPNE inp, inp        ' wait low  
                            WAITPEQ inp, inp        ' wait high
                            TEST    updn, INA WC    ' check up or down pin
                  IF_C      ADD     frq  #1         ' up
                  IF_NC     SUB     frq, #1         ' down
                            JMP     #:loop
    


    I'm guessing you'd need a DJNZ to every certain number of cycles you can WRLONG the value to a usable location. Or you add a WRLONG in the loop (make the whole think considerably lower resolution, but then instant updating). I guess that would make the whole thing 48 cycles (worst case). That might be where that is coming from.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    April, 2008: when I discovered the answers to all my micro-computational-botherations!

    Some of my objects:
    MCP3X08/4 ADC Driver - Programmable Schmitt inputs, frequency reading, and more!
    Simple Propeller-based Database - Making life easier and more readable for all your EEPROM storage needs.
    String Manipulation Library - Don't make strings the bane of the Propeller, bend them to your will!
  • pogerttpogertt Posts: 33
    edited 2009-10-02 04:49
    Thanks Bobb

    I will have time over the weekend to set up my demo board and try this.
Sign In or Register to comment.