[FAQ]Read-Modify-Write solution

Author
rtarbell
Junior Member
  • Total Posts : 96
  • Reward points : 0
  • Joined: 2005/12/30 03:29:55
  • Status: offline
2006/03/09 19:08:54 (permalink)
0

Read-Modify-Write solution

I know this problem has been addressed on the forum before, but I'm still a little confused as to when extra precaution is needed.  Can someone give me a couple of examples as to exactly when a "shadow" register is needed when operating on a PORT register? 
 
==> In my current program, I simply set or clear each bit in TRISA and TRISB at the beginning of the program, and then I do not touch the direction of any pin thereafter. 
#1

10 Replies Related Threads

    ric
    Super Member
    • Total Posts : 16444
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    RE: Read-Modify-Write solution 2006/03/09 19:30:51 (permalink)
    0
    The most usual cause of a R-M-W problem is when you do two successive BSF or BCF operations on the same port register.
    If there is significant capacitance on the first pin you changed, then it will still be changing when the second instruction is executed.
    Due to the way the PIC works, the "read" part of the second cycle will happen one quarter of an instruction cycle after the write of the preceding cycle.
    Because these instructions work by reading the port, modifying one bit, and rewriting the entire port, you can inadvertantly rewrite the previous state of the pin.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
     
    #2
    kalpak
    Super Member
    • Total Posts : 3271
    • Reward points : 0
    • Joined: 2004/03/12 23:01:40
    • Location: India
    • Status: offline
    RE: Read-Modify-Write solution 2006/03/09 19:35:22 (permalink)
    0
    The "shadow" reg is useful when you want to change the state of just a pin, not the whole port. 18F series have this implemented in the form of separate latches and port registers. For the 16F you can emulate the same solution.
    For reading, no precaution is needed per se. But if you read a pin that is in output mode, again for the same reason, you can get erroneous result.
    post edited by kalpak - 2006/03/09 19:36:50
    #3
    rtarbell
    Junior Member
    • Total Posts : 96
    • Reward points : 0
    • Joined: 2005/12/30 03:29:55
    • Status: offline
    RE: Read-Modify-Write solution 2006/03/09 20:23:22 (permalink)
    0
    Could I prevent this using extra NOP operations after all bsf, bcf, and read functions?
    #4
    bob_barr
    Super Member
    • Total Posts : 5428
    • Reward points : 0
    • Joined: 2003/11/07 12:35:23
    • Location: Morgan Hill, CA
    • Status: offline
    RE: Read-Modify-Write solution 2006/03/09 20:43:42 (permalink)
    0
    Could I prevent this using extra NOP operations after all bsf, bcf, and read functions?

    That depends on the speed of the processor and the circuit connected to the pin. Sometimes, doing that will be enough; other times, it won't provide reliable operation. Temperature and supply voltage variations may also affect reliability.
     
    Using a shadow register and writing to the entire port is, IMHO, the best way to ensure that R-M-W problems won't bite you when you least expect it.

    While it's always good to learn from one's mistakes, it's much easier to learn from the mistakes of others.
    Please don't PM me with technical questions. I'll be quite happy to help (if I can) on the forums.
    #5
    Ron Hayes
    Super Member
    • Total Posts : 1305
    • Reward points : 0
    • Joined: 2003/11/07 12:38:10
    • Location: Ontario, Canada
    • Status: offline
    RE: Read-Modify-Write solution 2006/03/09 21:31:28 (permalink)
    0
    Have a look at this, it's a little exaggerated but it should allow you to understand the issue.

    If Porta A,0 is rising slowly due to Capacitance on the next instruction cycle the read might be read as a low and then written back as a low on the following write.

    Ron

    Attached Image(s)

    #6
    bob_barr
    Super Member
    • Total Posts : 5428
    • Reward points : 0
    • Joined: 2003/11/07 12:35:23
    • Location: Morgan Hill, CA
    • Status: offline
    RE: Read-Modify-Write solution 2006/03/09 21:49:09 (permalink)
    0
    Here's an actual scope picture showing the same thing. Admittedly, the capacitance is quite high but note that the time-base is 10 usec per division.

     
    <added
    The voltage on the switched pin stays below 1 V for quite some time after it's turned on. Any bsf or bcf instruction performed during that time will read this pin back as a zero and clear it while you're trying to set or clear another pin.
    /added>
    post edited by bob_barr - 2006/03/09 21:53:20

    Attached Image(s)


    While it's always good to learn from one's mistakes, it's much easier to learn from the mistakes of others.
    Please don't PM me with technical questions. I'll be quite happy to help (if I can) on the forums.
    #7
    Guest
    Super Member
    • Total Posts : 81021
    • Reward points : 0
    • Joined: 2003/01/01 00:00:00
    • Location: 0
    • Status: online
    RE: Read-Modify-Write solution 2006/03/09 21:56:23 (permalink)
    0
    Great picture.
     
    Although inserting NOPs is not the best solution, it is a quick and easy fix.
    #8
    bob_barr
    Super Member
    • Total Posts : 5428
    • Reward points : 0
    • Joined: 2003/11/07 12:35:23
    • Location: Morgan Hill, CA
    • Status: offline
    RE: Read-Modify-Write solution 2006/03/09 22:15:47 (permalink)
    0
    Although inserting NOPs is not the best solution, it is a quick and easy fix.

    Unfortunately, it could take quite a few NOPs to get the delay necessary to ensure that the output voltage has risen enough to be reliably read as a '1'. This is especially true with a 20 MHz PIC executing an instruction every 200 nanoseconds.
     
    The number of cycles required could also vary with the pin's load, possibly with the supply voltage, and probably with temperature.
     
    All in all, I'd usually choose to go with a shadow register for safety sake.

    While it's always good to learn from one's mistakes, it's much easier to learn from the mistakes of others.
    Please don't PM me with technical questions. I'll be quite happy to help (if I can) on the forums.
    #9
    kalpak
    Super Member
    • Total Posts : 3271
    • Reward points : 0
    • Joined: 2004/03/12 23:01:40
    • Location: India
    • Status: offline
    RE: Read-Modify-Write solution 2006/03/09 22:27:11 (permalink)
    0
    ORIGINAL: bob_barr

    Although inserting NOPs is not the best solution, it is a quick and easy fix.

    Unfortunately, it could take quite a few NOPs to get the delay necessary to ensure that the output voltage has risen enough to be reliably read as a '1'. This is especially true with a 20 MHz PIC executing an instruction every 200 nanoseconds.

    The number of cycles required could also vary with the pin's load, possibly with the supply voltage, and probably with temperature.

    All in all, I'd usually choose to go with a shadow register for safety sake.

    Stands to reason, which is why they made those shadow regs real in 18F and called them LATx.
    #10
    Guest
    Super Member
    • Total Posts : 81021
    • Reward points : 0
    • Joined: 2003/01/01 00:00:00
    • Location: 0
    • Status: online
    RE: Read-Modify-Write solution 2006/03/10 03:29:45 (permalink)
    0
    Hey Bob, that is one of my favourite scope pictures.
    IMHO, the shadow register approach is the safest way to completely elliminate the r-m-w issues of the system.
    The problem with r-m-w is that it will not happen always, and most times you can just delay the accesses to allow charge settling, so many people are lured by the NOP or delay approach. But this may lead to obscure field failures. A boundary case of 'good' code can fail in the field due to process or temperature variations and even can fail in the same condition as the prototype.
     
    But there is a case that is not being addressed here: When a pin is sourcing or sinking substantial current to a load, the pin voltage may never reach VIH or VIL due to FET channel resistance dropout. PIC pins can drive heavy loads, and it is not uncommon to see currents of 10mA to 20mA being drawn from pins. A typical PIC output pFET channel resistance for the PIC16F873 can be 100ohms to 120ohms, and when sourcing 10mA will have a dropout of 1V to 1.2V in a 5V VDD system. This is enough to drive the pin voltage below VIH for a schmitt trigger input. If that pin has a ST-type input front-end, and a r-m-w op is made at that port, the pin will be read back as a LOW, even if you allow an infinite settling delay. Most LEDs will be driven at those current levels.
     
    A port buffer approach will never have that issue.
     
     
    #11
    Jump to:
    © 2014 APG vNext Commercial Version 4.5