Capture, 16F690

Discussion in 'Embedded Systems and Microcontrollers' started by FroceMaster, Sep 18, 2014.

  1. FroceMaster

    Thread Starter Member

    Jan 28, 2012
    have this where i would capture an amount of time,
    ex. when the input goes "low", Falling edge, and to NeXT time Falling edge,
    Timer1 is set to 125khz. FOSC/4 / prescaler 8.

    how should i set it up, , start the timer1 on first interrupt, and at NeXT interrupt then stop timer Again.
    C Code.
    Just the part with capturing, the setup i have control over.
  2. elabsp

    New Member

    Oct 23, 2013
  3. JohnInTX


    Jun 26, 2012
    I do it like this:

    Set TIMER 1 to free-run with no interrupts. The capture interval must fit into the 16 bits of the timer. Don't worry about the value of the timer for now, you won't be starting or stopping it.
    Configure the capture for your falling edge with interrupt on capture. For fast, repetitive signals, consider using capture on every 4th or 16th edge as appropriate.
    Clear CCP1IF and enable CCP1IE.

    When the first edge happens, the value of TMR1 will be captured in CCPR1H/L and the PIC interrupted. In the interrupt routine copy the capture registers to a temporary register, clear CCP1IF and return.
    When the next edge happens the new value of TMR1 will be in CCPR1H/L and the PIC interrupted. Since this the second time, disable the capture function (CCP1M=0000) and the interrupt. Set a flag so that the main routine knows that the two captures are complete and that the first capture is in the temp registers and the second is in CCPR1H/L.

    In the main routine, subtract: CCPR1 - temp register. A positive result is the number of TMR1 tiks between the captures. If negative, its the 2's complement of the actual value, complement the result.

    That's about it. The key concept is that by not stopping/starting TMR1, you don't have to worry about lost counts due to interrupt latency, overhead etc. How you tell the first capture/IRQ from the second is left to you, a simple flag will suffice. Note that processing the two values is left to the main (not interrupt) routines, signaled by a flag. You always want to minimize processing in a PIC interrupt routine, especially in C.

    Have fun.
    Last edited: Sep 24, 2014
  4. FroceMaster

    Thread Starter Member

    Jan 28, 2012
    Since it is allost a week ago. I did do it this way.
    Set interrupt int. Ra2 . When interrupt it starts timer1 . Next time interrupt the timer1 stops. And ready for next. Think it is precise enough.