PIC- Detect switch rising edge

Discussion in 'Embedded Systems and Microcontrollers' started by Dritech, Mar 14, 2014.

  1. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    Hi all,

    How can I increment an integer with the rising edge only and not continuing incrementing while at logic high? Is it possible without using the interrupt pin?
     
  2. MrChips

    Moderator

    Oct 2, 2009
    12,440
    3,360
    Use interrupt on rising edge.
     
  3. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Of course it is possible. One way is to increment the variable then wait right there in a loop until the pin goes low.

    If you have other things to do you can increment on the pin test but only when a flag is reset, and then set the flag. The flag is reset when you see the pin low.
     
    Dritech likes this.
  4. Brownout

    Well-Known Member

    Jan 10, 2012
    2,375
    998
    You can also connected the pin to a hardware counter in the PIC. Be sure to debounce your switch.
     
  5. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    Hi, that's a problem that I am encountering. Actually I am using a rotary encoder with internal mechanical switches (not optical). If I do not include a debounce delay, l will have unwanted pulses which will cause the counter to increment. On the other hand, if I include debounde delay I may miss pulses.
    How can I solve this problem please?
    I know that the best option is to replace the mechanical encoders with optical encoders, but they are quite expensive.
     
  6. Brownout

    Well-Known Member

    Jan 10, 2012
    2,375
    998
    Last edited: Mar 15, 2014
  7. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    If your encoder is a 2-phase type, then you can look at the successive states and never miss any counts, though you might see some "chatter" caused by contact bounce. If you had an optical encoder, you might get into a situation where the transitions are too fast for the processor, but that'll never happen with a mechanical type.

    But if it's just a single phase, alternating between on and off, you do indeed have to trade off the length of time you allow for debouncing, versus the length of a cycle. There'd be a point where the unit could never be reliable, as the switch wouldn't have time to settle. And having said that, the same effect would also occur with a 2-phase encoder. You just can't run those things too fast.
     
  8. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,526
    2,369
    Not sure if above your price range, but there are AMT non-mechanical quadrature encoders that can be obtained with a resolution between 48 to 2048 p/rev.
    $28.00 http://www.digikey.ca/product-detail/en/AMT103-V KIT/102-1308-ND/827016
    Max.
     
  9. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    That brings two questions to mind:

    1) How long does the encoder switch bounce?

    2) what is the product of how fast does the shaft turns times how many codes per turn?

    #1 gives the time of the glitch, #2 leads to the time between glitches.

    As long as #2 time > #1 time you can resolve all the pluses.

    Which PIC are you using?

    Is your encoder a quadrature type? (Those are inherently simple to de-glitch.)
     
  10. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    Last edited: Mar 16, 2014
  11. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,526
    2,369
    The encoders I referenced are lower down to 48p/rev. But are through hole not shaft encoders.

    That one is not ideal for your application, it is 24v open collector.
    You ideally need a 5v TTL or RS485 differential, just ignore the /A & /B pulses.
    For the majority of optical type, the lowest you may see is 100p/rev.
    You may get a 32p/rev shaft encoder at US Digital, they have them for around $125.00
    Or http://www.usdigital.com/products/encoders/incremental/rotary/shaft/S6
    Max.
     
    Last edited: Mar 16, 2014
  12. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    Hi Max.
    To be honest $125.00 is way above my budget.
    The specs of the encoder I mentioned states that it can be supplied with 5V.

    Why are open collector encoders not ideal please? Also, how do these encoders work? do they use hall sensors?

    Thanks in advance.
     
  13. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,526
    2,369
    Open collectors are fine but preferably 5v, you could theoretically use the 24v but it would require a 24vdc for the encoder and then use a pull up resistor from the collector to 5v and common up the 5v and 24v supply -ve's.
    These type of encoder use a photo etched glass disc with IR photo sensors.
    (The last linked one was $85.00)
    Max.
     
  14. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,526
    2,369
    If you just want low resolution, why not use a slotted IR sensor and a slotted disc like the T.M. motors do?
    You can also get dual types to produce quadrature pulses.
    Alternatively there is the miniature 4000 series hall sensors from Honeywell for a few cents, but this would also entail magnets on the disc.
    Max.
     
  15. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    I don't know why Max is going on about a 24V supply. You simply don't need it: just wire the thing up to 5V and it's all done. You'd need a pullup resistor on the output lines, but PIC processors typically have these built-in for some of their input pins. One advantage to having the encoder outputs be open-collector is that you can run the encoder and the processor on different power supplies, but if you're on a budget, you aren't likely to do that.

    You can get encoders with far more than 100 pulses per revolution, but this is a cheap one--I don't understand "Two phase 4 frequency doubling to 2400 pulses". I'm pretty sure it would be an optical type, but just possibly it's magnetic. Anyway, you don't need to worry about what's inside, as long as it works.
     
  16. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,526
    2,369
    I am 'Going on' as you put it because the encoder the OP linked to calls for 24v which as I mentioned could be used, but the 24vdc will still be required for the internal optics circuit plus 5v for the output.
    Also there is a misprint, the maximum resolution is 400p/rev not 2400.
    Max.
     
  17. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    Thanks for the replies.
    I am asking because I don't want any debouncing.
     
  18. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,526
    2,369
    With optical or capacitive type encoders you do not require debounce, it is the mechanical type that this occurs.
    Make sure you get incremental and not absolute.
    Max.
     
Loading...