rtarbell
-
Total Posts
:
96
-
Reward points
:
0
- Joined: 12/30/2005
-
Status: offline
|
Read-Modify-Write solution
Thursday, March 09, 2006 7:08 PM
( permalink)
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.
|
|
|
|
ric
-
Total Posts
:
12865
-
Reward points
:
0
- Joined: 11/7/2003
- Location: Australia, Melbourne
-
Status: offline
|
RE: Read-Modify-Write solution
Thursday, March 09, 2006 7:30 PM
( permalink)
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.
|
|
|
|
kalpak
-
Total Posts
:
3279
-
Reward points
:
0
- Joined: 3/12/2004
- Location: India
-
Status: offline
|
RE: Read-Modify-Write solution
Thursday, March 09, 2006 7:35 PM
( permalink)
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.
<message edited by kalpak on Thursday, March 09, 2006 7:36 PM>
|
|
|
|
rtarbell
-
Total Posts
:
96
-
Reward points
:
0
- Joined: 12/30/2005
-
Status: offline
|
RE: Read-Modify-Write solution
Thursday, March 09, 2006 8:23 PM
( permalink)
Could I prevent this using extra NOP operations after all bsf, bcf, and read functions?
|
|
|
|
bob_barr
-
Total Posts
:
5428
-
Reward points
:
0
- Joined: 11/7/2003
- Location: Morgan Hill, CA
-
Status: offline
|
RE: Read-Modify-Write solution
Thursday, March 09, 2006 8:43 PM
( permalink)
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.
|
|
|
|
Ron Hayes
-
Total Posts
:
1304
-
Reward points
:
0
- Joined: 11/7/2003
- Location: Ontario, Canada
-
Status: offline
|
RE: Read-Modify-Write solution
Thursday, March 09, 2006 9:31 PM
( permalink)
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 [image]local://7928/348A17CFAEDB4164B7E71B18F6D7A22C.jpg[/image]
|
|
|
|
bob_barr
-
Total Posts
:
5428
-
Reward points
:
0
- Joined: 11/7/2003
- Location: Morgan Hill, CA
-
Status: offline
|
RE: Read-Modify-Write solution
Thursday, March 09, 2006 9:49 PM
( permalink)
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. [image]local://1372/E48709D204894DEAB5BE7EB7F674C144.jpg[/image] <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>
<message edited by bob_barr on Thursday, March 09, 2006 9:53 PM>
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.
|
|
|
|
Guest
-
Total Posts
:
52888
-
Reward points
:
0
- Joined: 1/1/2003
- Location: 0
-
Status: online
|
RE: Read-Modify-Write solution
Thursday, March 09, 2006 9:56 PM
( permalink)
Great picture. Although inserting NOPs is not the best solution, it is a quick and easy fix.
|
|
|
|
bob_barr
-
Total Posts
:
5428
-
Reward points
:
0
- Joined: 11/7/2003
- Location: Morgan Hill, CA
-
Status: offline
|
RE: Read-Modify-Write solution
Thursday, March 09, 2006 10:15 PM
( permalink)
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.
|
|
|
|
kalpak
-
Total Posts
:
3279
-
Reward points
:
0
- Joined: 3/12/2004
- Location: India
-
Status: offline
|
RE: Read-Modify-Write solution
Thursday, March 09, 2006 10:27 PM
( permalink)
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.
|
|
|
|
Guest
-
Total Posts
:
52888
-
Reward points
:
0
- Joined: 1/1/2003
- Location: 0
-
Status: online
|
RE: Read-Modify-Write solution
Friday, March 10, 2006 3:29 AM
( permalink)
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.
|
|
|
|