PIC- Detect switch rising edge

Thread Starter

Dritech

Joined Sep 21, 2011
907
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?
 

ErnieM

Joined Apr 24, 2011
8,415
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.
 

Thread Starter

Dritech

Joined Sep 21, 2011
907
You can also connected the pin to a hardware counter in the PIC. Be sure to debounce your switch.
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.
 

John P

Joined Oct 14, 2008
2,068
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.
 

ErnieM

Joined Apr 24, 2011
8,415
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.
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.)
 

MaxHeadRoom

Joined Jul 18, 2013
30,808
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:

Thread Starter

Dritech

Joined Sep 21, 2011
907
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.
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.

DC5-24V wide voltage power supply
Why are open collector encoders not ideal please? Also, how do these encoders work? do they use hall sensors?

Thanks in advance.
 

MaxHeadRoom

Joined Jul 18, 2013
30,808
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.
 

MaxHeadRoom

Joined Jul 18, 2013
30,808
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.
 

John P

Joined Oct 14, 2008
2,068
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.
 

MaxHeadRoom

Joined Jul 18, 2013
30,808
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. .
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.
 

MaxHeadRoom

Joined Jul 18, 2013
30,808
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.
 
Top