pic counter/timer

Discussion in 'General Electronics Chat' started by gdallas, May 30, 2014.

  1. gdallas

    Thread Starter Member

    Apr 25, 2012
    74
    0
    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?
     
  2. Dodgydave

    Distinguished Member

    Jun 22, 2012
    4,965
    743
    Yes you can, but you may get contact bounce, i would use a de-bounce circuit using a 555 mono timer.
     
  3. gdallas

    Thread Starter Member

    Apr 25, 2012
    74
    0
    what if i was to condition the input via a schmitt trigger logic gate first?
     
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    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.
     
  5. gdallas

    Thread Starter Member

    Apr 25, 2012
    74
    0
    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?
     
  6. gdallas

    Thread Starter Member

    Apr 25, 2012
    74
    0
  7. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    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.
     
  8. gdallas

    Thread Starter Member

    Apr 25, 2012
    74
    0
    ok so what your saying is use a standard switching pull up, and just software debounce.
     
  9. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605

    Yep.


    ...
     
  10. gdallas

    Thread Starter Member

    Apr 25, 2012
    74
    0
  11. Dodgydave

    Distinguished Member

    Jun 22, 2012
    4,965
    743
    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.
     
  12. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    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). ;)
     
  13. MrChips

    Moderator

    Oct 2, 2009
    12,414
    3,353
    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.
     
  14. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    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.
     
Loading...