• AVR Freaks

Hot!Using the CCP to measure pulse width?

Author
AndersG
Senior Member
  • Total Posts : 179
  • Reward points : 0
  • Joined: 2008/08/05 04:51:24
  • Location: 0
  • Status: offline
2019/12/21 07:50:59 (permalink)
0

Using the CCP to measure pulse width?

I have a requirement to decode a pulse train. The train starts with a sync burst of 8 pulses 20us high, 20us low. Then follows pulses that are 100us hign and 28us low. I thought I could use the CCP for this (PIC24FJ64GB002) but cannot figure out how. Here is how I have it configured:
 
    // Capture/compare
    RPINR7bits.IC1R = 7;
    IC1CON1bits.ICTSEL = 7; // Fosc/2
    IC1CON1bits.ICI = 0; // interrupt on every one
    IC1CON1bits.ICM = 2; // capture on falling edge
    IC1CON2bits.SYNCSEL = 20; // Sync on IC1 input
    IC1CON2bits.ICTRIG = 0; // Trigger ICx from source designated by SYNCSELx bits
    TRISBbits.TRISB7 = 1; // input

 
Following teh diagram on page 157 (INPUT CAPTURE BLOCK DIAGRAM) my idea was to have the times count fosc/2, have the INPUT on RB7 trigger the capture and then reset the counter, but this does not happen. I get random values on IC1BUF. What am I doing wrong?
#1

5 Replies Related Threads

    NorthGuy
    Super Member
    • Total Posts : 5881
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: Using the CCP to measure pulse width? 2019/12/21 08:42:49 (permalink)
    0
    Don't reset the ICBUF. If anything, reset the timer, but it's a really bad idea too.
     
    Read ICBUF for rising edge and ICBUF for falling edge, then subtract the two.
    post edited by NorthGuy - 2019/12/21 08:44:52
    #2
    AndersG
    Senior Member
    • Total Posts : 179
    • Reward points : 0
    • Joined: 2008/08/05 04:51:24
    • Location: 0
    • Status: offline
    Re: Using the CCP to measure pulse width? 2019/12/21 09:03:16 (permalink)
    0
    Read ICBUF for rising edge and ICBUF for falling edge, then subtract the two
     
    You mean to have it interrupt on both edges and do it in the ISR? In that case I do not even need the CCP, just a timer. I thought I could do more with the CCP in hardware.
    #3
    NorthGuy
    Super Member
    • Total Posts : 5881
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: Using the CCP to measure pulse width? 2019/12/21 09:17:27 (permalink)
    0
    AndersG
    You mean to have it interrupt on both edges and do it in the ISR? In that case I do not even need the CCP, just a timer. I thought I could do more with the CCP in hardware.



    If you want, you can get only one interrupt. Not to mix falling and rising edges you can use one CPP for rising and one CPP for falling edges.
     
    CPP is much more accurate that reading timer from the interrupt. It latches the timer immediately. If you read timer from the interrupt there's interrupt latency to deal with.
    #4
    AndersG
    Senior Member
    • Total Posts : 179
    • Reward points : 0
    • Joined: 2008/08/05 04:51:24
    • Location: 0
    • Status: offline
    Re: Using the CCP to measure pulse width? 2019/12/21 10:44:16 (permalink)
    0
    OK. I guess it is my lack of understanding on how the CCP is used to work. Ie when I saw this diagram:
     
     CCP_2019-12-21_19-41-08.jpg
    I assumed that I could have the CCP reset the counter as well, but apparently not.
     
    So I guess I will just have to trigger on both and take the difference between the values.

    Attached Image(s)

    #5
    NorthGuy
    Super Member
    • Total Posts : 5881
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: Using the CCP to measure pulse width? 2019/12/22 07:36:49 (permalink)
    0
    I may be wrong, but I don't think the IC module can be its own SYNCSEL.
    #6
    Jump to:
    © 2020 APG vNext Commercial Version 4.5