Shop OBEX P1 Docs P2 Docs Learn Events
Counter module misses some edges. Why? — Parallax Forums

Counter module misses some edges. Why?

Daniel NagyDaniel Nagy Posts: 26
edited 2010-01-02 00:36 in Propeller 1
Hello everyone, I'm a new propeller user, with my first question posted to the forum.

I made a simple test code, to perform edge detection on a simple switch connected to PIN6. (The PIN is pulled to ground with a 100k resistor.)
The code speaks for itself.

What I experience is, that in some cases (within a few secs of continous switch tapping) the edge detection fails, leaves the LED connected to PIN7 goes out of sync compared to LED PIN 15 (used with the with feedback option). The LED PIN7 remains on with the switch released, or remains off with the switch pressed. This means the code missed to detect an edge.

Why?

CON
  _clkmode = xtal1 + pll16x
  _xinfreq = 5_000_000

PUB Main

dira[noparse][[/noparse]6]~
dira[noparse][[/noparse]7..15]~~

CTRA[noparse][[/noparse]5..0]:=6
CTRA[noparse][[/noparse]14..9]:=15

CTRB[noparse][[/noparse]5..0]:=6

CTRA[noparse][[/noparse]30..26]:=%01011
CTRB[noparse][[/noparse]30..26]:=%01110

FRQA:=1
FRQB:=1

repeat
  if PHSA<>0 
    outa[noparse][[/noparse]8]:=1
    phsa~
    
  if PHSB<>0  
    outa[noparse][[/noparse]8]:=0
    phsb~


Comments

  • AribaAriba Posts: 2,690
    edited 2010-01-01 02:49
    Perhaps it's the bouncing of the switch.
    If bouncing is faster then the Spin loop, then it can be that phsa counts 2 positive edges, but phsb only 1 negative for example.
    Because you clear phsa and phsb one count gets lost.

    Try this version:
    ...
    PHSA~
    PHSB~
    repeat
      if PHSA>0 
        outa[noparse][[/noparse]8]:=1
        phsa--
        
      if PHSB>0  
        outa[noparse][[/noparse]8]:=0
        phsb--
    
    



    Andy
  • Daniel NagyDaniel Nagy Posts: 26
    edited 2010-01-01 19:46
    Dear Ariba,
    Thank you for your suggestion.

    Your theory is right I believe, but in practice, decrementing the register reduces the flaws only, not eleminates them.
    Continuing your theory, I came up with:
    repeat
      if PHSA>PHSB   
        outa[noparse][[/noparse]8]:=1
      else
        outa[noparse][[/noparse]8]:=0
    



    It never misdetect, but has two drawbacks in this form:
    1. If one register reaches the positive maximum, it will fail.
    2. If the propeller is started with the button pressed, it wont work.

    I'm continuing to solve this, but if anyone has the perfect solution, please post it. It must be a basic thing...
  • Tracy AllenTracy Allen Posts: 6,666
    edited 2010-01-01 20:06
    A simple hardware fix can often help. Add a capacitor of around 0.01 uF in parallel with the 100k resistor, to filter out the switch bounce.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Daniel NagyDaniel Nagy Posts: 26
    edited 2010-01-02 00:36
    Thanks, Tracy.
Sign In or Register to comment.