• AVR Freaks

Hot!CCPR 16 bit read corruption

Author
Cyber
New Member
  • Total Posts : 18
  • Reward points : 0
  • Joined: 2007/07/02 10:48:55
  • Location: 0
  • Status: offline
2019/04/16 03:29:37 (permalink)
0

CCPR 16 bit read corruption

I have found an intermittent bug and traced it down to the CCPR high/low registers changing while reading them. It seems reasonable that I might have a capture event while reading the previous capture. Turning off capture while reading solves the problem, but then I lose captures. I could not find a 16 bit buffered read feature like on timers. I wrote the ccpr read in assembler to reduce this phenomenon and the high and low bytes are read just 4 instructions apart.

Is there a known solution to this problem?
#1

2 Replies Related Threads

    pcbbc
    Super Member
    • Total Posts : 1192
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: CCPR 16 bit read corruption 2019/04/16 03:53:02 (permalink)
    +1 (1)
    CyberTurning off capture while reading solves the problem, but then I lose captures.
    Either way, you will lose captures if they occur faster than you ISR can process them.  Why are you not concerned about the captures you may lose before you even get around to checking the interrupt flag and reading the first CCPR register?
     
     
    If you do not wish to turn off capture, you can clear the interrupt flag prior to reading, read high & low CCPR registers, and finally check the interrupt flag again.  If it is set there was a capture event and your CCPR value may be unreliable, and you know you have missed a capture.  Repeat this cycle until you get a CCPR register pair successfully (without the flag being set).  However it still means you have missed capture values - no way around that.
     
    You don't say what part you are using - there may be ways around this on some parts (e.g. DMA), but none on the 8-bit PIC12/16 parts I work with.
    #2
    Cyber
    New Member
    • Total Posts : 18
    • Reward points : 0
    • Joined: 2007/07/02 10:48:55
    • Location: 0
    • Status: offline
    Re: CCPR 16 bit read corruption 2019/04/26 11:23:30 (permalink)
    0
    I just knew a clever chap would point out "yes, but you're losing captures either way".  And yes, it is a concern that my code can't keep up, but in this particular case, as long as I get the last capture time correctly and associated with the correct transition, it's fine.
     
    Without going into all the details and reasons why, I am reading both transitions and captures in a fast timer ISR rather than in a capture ISR.  I did find a way around the problem, but it still has me baffled.  This is for a multi-quadrature decoder and I changed the capture to trigger on every fourth transition, instead of every second like before.  The strange thing is that I was not missing quadrature transitions, yet I was apparently missing captures running at half that rate.
     
    I'm using an 8-bit PIC16F18857 and almost every peripheral on it.  :P  It's a bit mad.
    #3
    Jump to:
    © 2019 APG vNext Commercial Version 4.5