pic counter/timer

Thread Starter

gdallas

Joined Apr 25, 2012
74
I have an application that requires to measure a frequency of a pump strokes using a whisker switch, has one NO, and one NC contact. I [plan to use the 5 timer/counters to measure 5 pump rates using my pic18f. do I treat this like any other push switch, i.e. the external circuitry being pull up resistor to VCC, with the whisker switch grounding the counter input on each actuation?
 

ErnieM

Joined Apr 24, 2011
8,377
Even with the Schmitt you get bounce. You don't need any additional components, there are many many ways to write code to ignore switch bouncing.

This would mean giving up on using the timers as counters, but it does mean you could measure as many pump strokes as pins on the device.
 

Thread Starter

gdallas

Joined Apr 25, 2012
74
Hi ErnieM,

So if we use a softare deboucne, say a timer 15ms, would it also be desirablet o use a Schmitt trigger ingverter or simillar to condition the input signal?
 

ErnieM

Joined Apr 24, 2011
8,377
If you wish to keep on adding parts to do function done just as well in software then go ahead and buy all the parts you want.

"Switch bounce" is a general term for any device which has an output that may change states several times for any given transition. To guard against this all you really need to do is check the input on a periodic basis.

The problem comes when the PIC can process the input faster then the bounce time, and that definately includes a hardware timer counter. A counter will see each and every bounce and count it, and as it is hardware it needs other hardware to support it.

The same problem is there if you use software to count the input, but here you can insert delays and some validation testing to keep things straight.

For example, a signal with a 5Hz rate will only change once every 100 mS, or 10 times a second. That is for both edges, up and down. There should be a shorter time that the output is in doubt; this time should be much smaller or you will not be able to pull the signal out of the noise.

To validate such a signal one could check the inputs every 25 mS. If the input does not agree with the last samlple it is ignored, but saved for the next check. Thus you must get the same stable signal for 25mS to register an input.

The time required can and does vary depending on the device (how long does it bounce) and how responsive you want your system to be. I oft use that 25mS for manual switch debouncing. I cannot preceive the delay, and never see extra button presses.

Your device may need a different timeout, but since now it is in software it is easy to change.
 

Dodgydave

Joined Jun 22, 2012
11,285
If you use TMR1L with a debounce chip , all you need is a 1 second delay in the software then the total number of pulses will be counted in the TMRL, that will give you the frequency/strokes,then just read the values in TMR1 and display it.
 

THE_RB

Joined Feb 11, 2008
5,438
Considering that "5 pumps" are likely to be at a distance to the PIC, I would use 5 series resistors and 5 caps with the caps directly at the PIC input pins to ground.

Assuming they are ST type digital input pins, they make 5 RC filters which will fix the contact bounce AND remove the bulk of HF noise etc AND save the PIC input pins from transients.

5 R and 5 C is very low cost and gives a lot of benefits.

(And then I'd still debounce in software). ;)
 

MrChips

Joined Oct 2, 2009
30,719
Using a Schmitt trigger will likely make the problem worse. That is because you have to remove the transients first before applying it to a Schmitt trigger which will then clean up the signal.

Use the software solution with 10-50ms delays.
 

THE_RB

Joined Feb 11, 2008
5,438
Using a Schmitt trigger will likely make the problem worse. That is because you have to remove the transients first before applying it to a Schmitt trigger which will then clean up the signal.
...
Sorry MrChips I don't understand that?

Using an RC filter into a ST input pin works perfectly, the RC filter removes any fast noise transients, and the ST input gives reliable triggering on the slowly ramping slope of the filtered signal.
 
Top