TMR1 counter debounce?


Closed Thread
Results 1 to 7 of 7

Hybrid View

  1. #1

    Default TMR1 counter debounce?

    My 16F689 TMR1 counter is counting too well! How does one debounce this? My code:

    TRISC = %00000000
    TRISA = %100100
    TRISB = %0000

    @ DEVICE pic16f689,wdt_off

    HIGH PORTB.4

    Define LCD_DREG PORTC 'Set LCD data port
    DEFINE LCD_DBIT 0 'Set LCD starting data bit
    DEFINE LCD_RSREG PORTC 'Set LCD register select port
    DEFINE LCD_RSBIT 4 'Set LCD register select bit
    DEFINE LCD_EREG PORTC 'Set LCD enable port
    DEFINE LCD_EBIT 5 'Set LCD enable bit
    DEFINE LCD_BITS 4 'Set LCD bus size
    DEFINE LCD_LINES 2 'Set LCD number of lines

    OPTION_REG = %00111000
    ANSEL = 0
    CM1CON0 = %00000000
    LCDOUT $fe,1

    COUNT1 Var Word
    COUNT1 = 0
    TMR1H = 0
    TMR1L = 0

    'Set TMR1 to increment on rising edge of T1CKI pin, no prescaler, no sync to internal clock, TMR 1 is on.
    T1CON = %00000111

    Loop:
    Count1.highbyte = TMR1H 'Get high byte of counter
    Count1.Lowbyte = TMR1L 'Get low byte of counter


    'Make sure that the counter did not roll over between reads of the high and low byte.

    Temp var TMR1H 'Get highbyte again.
    If Temp - Count1.HighByte <> 0 then 'The two values differs so we read it again.
    Count1.HighByte = TMR1H
    Count1.LowByte = TMR1L
    ENDIF

    Lcdout $fe,1
    LCDOUT DEC COUNT1
    PAUSE 100

    Goto Loop

    Any help would be appreciated.

  2. #2
    Join Date
    Mar 2008
    Location
    Texas, USA
    Posts
    114


    Did you find this post helpful? Yes | No

    Default

    What's shown on the LCD when it runs (DEC count1)?

  3. #3
    Join Date
    Feb 2003
    Location
    Salt Lake City, Utah USA
    Posts
    517


    Did you find this post helpful? Yes | No

    Default

    >> Temp var TMR1H

    possibly part of the problem ... this gets redeclared each time through the "loop". You might want to move it up above where your other declaration resides.
    Paul Borgmeier
    Salt Lake City, UT
    USA
    __________________

  4. #4
    Join Date
    Feb 2003
    Location
    Salt Lake City, Utah USA
    Posts
    517


    Did you find this post helpful? Yes | No

    Default

    >>My 16F689 TMR1 counter is counting too well

    After sleeping on your question, it sounds like you need a hardware debounce filter, possibly like

    http://www.elexp.com/t_bounc.htm

    I do not know how to add one in software when using the TMR1 as a counter. You could switch to another software means of counting all together (e.g., Interrupt on change of one of the port pins, etc) where adding software debounce would be a piece of cake.
    Paul Borgmeier
    Salt Lake City, UT
    USA
    __________________

  5. #5


    Did you find this post helpful? Yes | No

    Default

    If he is counting edges for some type of signal, a series R - shunt C filter may be better than the debounce filter. Just pick the 1/RC time constant to be higher than the max frequency of the signal you are trying to count, the nyquist freq (f*2) being a good starting point.
    Tim Barr

  6. #6


    Did you find this post helpful? Yes | No

    Default

    Thanks to every one for your help. Sorry for the delay in responding, our internet connection was down for 12 hours. I am trying to count the number of switch closures of a very fast reed switch. TMR1 is counting edges on T1CKI, just as it's supposed to. Ultimately,this switch will wake the program from SLEEP, and resume counting. I haven't as yet quite figured out how to use DT's Instant Interrupts to do this, but in doing so, maybe this will help with the "debounce" problem.

    For now, I may use Paul's idea of just counting the pin, or try using falingtrea's RC filter.

    Thanks again.

Similar Threads

  1. Conway's Game Of Life
    By wellyboot in forum mel PIC BASIC Pro
    Replies: 45
    Last Post: - 28th May 2020, 06:14
  2. 20 Digit Virtual LED Counter
    By T.Jackson in forum Code Examples
    Replies: 9
    Last Post: - 19th November 2007, 05:02
  3. Replies: 14
    Last Post: - 26th September 2007, 05:41
  4. Microcontroller with 2 way paging application problem
    By oneohthree in forum mel PIC BASIC Pro
    Replies: 30
    Last Post: - 20th April 2007, 17:27
  5. Asynchronous counter mode on TMR1
    By micro in forum General
    Replies: 1
    Last Post: - 8th April 2006, 15:42

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts