Reset timer / encoder transition


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2007
    Posts
    7

    Default Reset timer / encoder transition

    I need to write a subroutine for my main program where I am looking for a transition from an encoder where there is one transition per second. I am new to PIC programming and I am having trouble figuring out how to use the timers in a 16F628A to accomplish this task. My programming experience has been mostly with PLC’s and I could easily figure out how to do it with the timers or counters in a PLC, but I just haven’t figured out how to do it with a PIC. I am using an external RC oscillator which runs at 6 KHz. The direction I planned to go was to reset a timer and clear the interrupt flag and then look for the edge of a transition on the encoder input. If I see a transition within a 3 second window, I would reset the timer to keep it from overflowing. If I don’t see a transition in this 3 second window then I would go into an ISR. I wanted to use the timer so that it could be running in the background while I also monitored the encoder and some other inputs. The other inputs could determine that it is okay to not get a transition from the encoder. This seems like it should be a simple task but I am have trouble figuring out how to have the timer run in the background while I monitor the encoder input transitions and continue to scan other inputs at the same time. I have used the Search feature on this forum to look at different timer examples but I don’t seem to find any example that will work for me.

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Why 6KHz? Not sure if the PIC may run as this slow... Internal is free, and gives 1-2 more i/o.

    i need another cofee...
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    Join Date
    Jun 2007
    Posts
    7


    Did you find this post helpful? Yes | No

    Default

    The 6KHz oscillator is to get under the 10KHz limit to avoid FCC testing of our product. I do not need a fast clock to handle the inputs involved and clock accuracy is not critical. I have the PIC running at 6KHz and it is running the other portions of my program okay. I have just not yet been able to think of a way to use the PIC timers to confirm that I am getting a transition on 1 input while I am also monitoring other inputs. I need to see that the input has changed within a 3 or 4 second window. It seems like it should be simple but I am not used to this type of timer. I am used to a timer that I could start and then reset with each transition so that the timer does not time out. If it does time out I would go to a fault handling routine. I need this timer running in the background while I am checking other inputs. I am sure it can be done I just haven't worked with PIC's before.

  4. #4
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    It may seem a lot harder than it actually is.

    Try this. We're using Timer0 configured as a counter. When your encoder
    makes a transition on RA4/TOCKI (Timer0 clock input pin), it increments
    the Timer0 count.

    Real simple. Runs in the background all on its own, etc,,.

    Code:
    TRISA.4 = 1 ' RA4/T0CKI = input to TMR0 counter
    TRISB.0 = 0 ' RB0 = output for LED
    CMCON = 7   ' All digital
    
    ' Assign prescaler to WDT for 1:1 prescale on TMR0
    ' TMR0 clock will be from your external input on RA4/T0CKI
    ' Increment on high-to-low transitions
     OPTION_REG = %00111000
     
    ' If you prefer, then increment on low-to-high transitions
    ' OPTION_REG = %00101000
    
    Main:
        TMR0 = 0     ' Clear TMR0 count before start
        
    Loop:
        WHILE TMR0 = 0 ' Wait for high-to-low transition
        WEND           ' on RA4/T0CKI
        PORTB.0 = 1    ' LED on to indicate transition seen
        PAUSE 500
        PORTB.0 = 0    ' LED off
        GOTO Main      ' Start over
    
        END
    Timer0 configured as a counter can count your encoder transitions in the
    background, and your main code can do whatever else it needs to in the
    mean time.

    You can set it to increment on high-to-low or low-to-high transitions by
    flipping a single bit in the OPTION register.

    Just look in your datasheet at the OPTION register & Timr0 sections for
    how/why it works. It's really simple once you tinkered with it a bit.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  5. #5
    Join Date
    Jun 2007
    Posts
    7


    Did you find this post helpful? Yes | No

    Default

    Bruce, thanks for the idea. Your idea would need some adjusting to make it run in the background in my application, but it has got me to thinking about using the timer as a counter and setting it just a couple of counts from overflowing and then my program will need to see it overflow because of the transitions or else jump to a fault routine. I am just used to the smooth operation of timers and counters in PLC programming and the PIC timers just seem awkward to use.

Similar Threads

  1. Resetting a timer in VB6
    By Bruce in forum Off Topic
    Replies: 11
    Last Post: - 26th December 2013, 20:02
  2. Quadrature encoder and ASM Interrupts. questions..
    By godfodder in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 17th March 2013, 14:45
  3. Elapsed Timer Demo
    By Darrel Taylor in forum Code Examples
    Replies: 111
    Last Post: - 29th October 2012, 17:39
  4. Timer interrupt frequency
    By Samoele in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 17th January 2009, 23:49
  5. encoder wowes
    By wallaby in forum mel PIC BASIC Pro
    Replies: 16
    Last Post: - 6th December 2005, 21:56

Members who have read this thread : 1

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