Background Pulse Counter


Closed Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2017
    Posts
    17

    Default Background Pulse Counter

    I am creating an application which will need to count 2uS pulses while running a control loop.
    This will be implemented with the PIC18F4680
    The control loop uses DT Interrupts to control some pins used for output.
    I have an input which feeds the 2uS pulses in a frequency proportional to speed and can be from 1khz to a max of 30 khz.
    I want to have these pulses clock a counter and within my control loop monitor this value and when it reaches specific values execute a sub.

    I am not sure if there is a way to setup the timers to be externally clocked then just read the timer register.
    I also am not sure if this would impact the DT Interrupts since they use some of the timers as well.

    I didn't want to just test the input pin for high and low because since the pulse is just 2uS it could get missed.
    Maybe there is a way to set a flag using the 2uS pulse, I read the flag in my loop and increment my own counter then reset the flag.
    Is this part of the capture and compare pins?

    Thanks for any input or opinion if this is even a reasonable task to accomplish with a PIC18F.

  2. #2
    Join Date
    Sep 2017
    Posts
    17


    Did you find this post helpful? Yes | No

    Default Re: Background Pulse Counter

    I should add that with the DT Interrupts, I am only using Timer 1 for an internal event timer.
    I would presume this would mean anything I use timer 2 or 3 for would leave DT Interrupts unaffected under this situation.

    Thanks
    George

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,516


    Did you find this post helpful? Yes | No

    Default Re: Background Pulse Counter

    TMR0 can be setup as a 8 or 16 bit counter clocked by your signal attached to the T0CKI-pin, you can choose rising or falling edge. It can also be set to interrupt on overflow so if you want to run your sub every 2000 counts simply preload the TMR0 register pair with 63536 and after 2000 counts an interrupt will occur. Remember to reload the TMR0 register pairs again, otherwise it'll keep counting from 0.

    Or you can just read the count at your leasure if that's better/easier/more suitable. When using 16bit mode there's a special order in which you must read/write the TMR0 register pairs, make sure to read the datasheet.

  4. #4
    Join Date
    Sep 2017
    Posts
    17


    Did you find this post helpful? Yes | No

    Default Re: Background Pulse Counter

    This turned out the be far easier than I thought it would be.
    I set T0CON for $E8
    TMR0ON = Enable Timer 0
    T08BIT = Timer0 8 bit
    T0CS = Transition on TOCK1
    T0SE = Low to High transition (beginning of 2uS pulse)
    PSA = Timer0 prescaler is not assigned
    T0PS2:T0PS0 all 0

    Then I set the register to 0
    TMR0L = $00

    In my control loop which enables the device which provides the 2uS speed proportional pulses I read the TMR0L and when it equals to a predetermined value the sub is called and when the sub is finished the register is again set to zero.

    It counts in the background and the control loop doesn't have to consume that many cycles reading the register so it really doesn't burden the control loop.

    Thanks

Similar Threads

  1. pulse counter
    By daydream in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 7th May 2012, 12:17
  2. Pulse Counter
    By srspinho in forum Code Examples
    Replies: 13
    Last Post: - 5th May 2012, 22:56
  3. pulse counter 16f84a
    By daydream in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 1st May 2012, 19:51
  4. Pic16f84 based pulse Counter
    By anzarsalam in forum General
    Replies: 5
    Last Post: - 4th August 2008, 03:50
  5. Pulse Counter in Assembly
    By orlandoG in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 21st March 2007, 02:23

Members who have read this thread : 2

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