Difference between revisions of "Read before write"

From OpenCircuits
Jump to navigation Jump to search
(yet another solution)
(yet more read-modify-write problems)
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
==== Read before Write ====
 
==== Read before Write ====
  
The issue with 'read before write' is that, unlike a PIC18, the PIC16 does not store the state of it's output pins anywhere.
+
The issue with 'read before write' is that, unlike a PIC18, it is impossible for a program running on the PIC16 to read the state of its output port latch.
  
 
The classic issue occurs when individual port bits are set, or toggled, causing outputs on the pins to behave unexpectedly.
 
The classic issue occurs when individual port bits are set, or toggled, causing outputs on the pins to behave unexpectedly.
Line 9: Line 9:
 
For example with trisb set to zero, if you set portb.1=1, then later set portb.2=1. If portb.1 is excessively loaded, the PIC16 will read portb.1 as a zero and write 4 (0b100) to portb, instead of the expected 6 (0b110) to portb.
 
For example with trisb set to zero, if you set portb.1=1, then later set portb.2=1. If portb.1 is excessively loaded, the PIC16 will read portb.1 as a zero and write 4 (0b100) to portb, instead of the expected 6 (0b110) to portb.
  
There are two solutions to the above issue:
+
There are three solutions to the above issue:
  
 
One solution is to use a processor that does allow us to read the state of its output pins -- such as the LATx register on the PIC18 processors.
 
One solution is to use a processor that does allow us to read the state of its output pins -- such as the LATx register on the PIC18 processors.
  
The first, and by far the best is to ensure the output pins are not excessively loaded.
+
The second, and by far the best is to ensure the output pins are not excessively loaded.
 
 
 
This is the best solution, as code not necessarily under your control, such as UART and I2C code will not affect the remainder of your application.
 
This is the best solution, as code not necessarily under your control, such as UART and I2C code will not affect the remainder of your application.
 +
Unfortunately, this doesn't work when implementing an open-collector bus.
  
The second solution is to use some kind of 'shadow' register, in which the individual bits can be manipulated, then the resulting CHAR values can then be written to the port.
+
The third solution is to use some kind of 'shadow' register, in which the individual bits can be manipulated, then the resulting CHAR values can then be written to the port.
 +
You use this shadow register to emulate the LATx registers on other processors.
  
 
The various _port macros below, if called in place of the _bit macros, should do the job:
 
The various _port macros below, if called in place of the _bit macros, should do the job:
Line 24: Line 25:
  
 
#define set_port(port, no)\
 
#define set_port(port, no)\
set_bit(sport[&porta-PORTA], no);\
+
set_bit(sport[&port-PORTA], no);\
port=sport[&porta-PORTA];
+
port=sport[&port-PORTA];
  
 
#define clear_port(port, no)\
 
#define clear_port(port, no)\
clear_bit(sport[&porta-PORTA], no);\
+
clear_bit(sport[&port-PORTA], no);\
port=sport[&porta-PORTA];
+
port=sport[&port-PORTA];
  
 
#define test_port(port, no)\
 
#define test_port(port, no)\
test_bit(sport[&porta-PORTA], no)
+
test_bit(sport[&port-PORTA], no)
  
 
#define toggle_port(port, no)\
 
#define toggle_port(port, no)\
toggle_bit(sport[&porta-PORTA], no);\
+
toggle_bit(sport[&port-PORTA], no);\
port=sport[&porta-PORTA];
+
port=sport[&port-PORTA];
 
</pre>
 
</pre>
  
Line 42: Line 43:
  
 
* [http://massmind.org/techref/readmodwrite.htm The Read-Modify-Write problem]
 
* [http://massmind.org/techref/readmodwrite.htm The Read-Modify-Write problem]
 +
* [http://circuitguru.com/79/pic-microcontroller-read-modify-write-bugs/ Circuit guru: "PIC microcontroller read-modify-write bugs"]
 +
* [http://www.cornerstonerobotics.org/curriculum/lessons_year2/erii_rmw_problem.pdf Cornerstone Robotics: "Read-Modify-Write (RMW) Problem with Mid-Range PIC Microcontrollers ... and Avoiding the Read-Modify-Write (RMW) Problem with LATx Registers in PIC18Fxxxx"]
 +
* [http://electronics.stackexchange.com/questions/7684/what-causes-turning-on-an-single-output-pin-on-a-microchip-pic16f690-to-spontane Stackexchange: "What causes turning ON an single output pin on a Microchip PIC16F690 to spontaneously turn OFF another pin on the same port?"]
  
  
 
[[Category:BoostC]][[Category:PIC]]
 
[[Category:BoostC]][[Category:PIC]]

Latest revision as of 06:33, 14 February 2012

Read before Write[edit]

The issue with 'read before write' is that, unlike a PIC18, it is impossible for a program running on the PIC16 to read the state of its output port latch.

The classic issue occurs when individual port bits are set, or toggled, causing outputs on the pins to behave unexpectedly.

This happens because the PIC16 reads the state of the port pins to calculate the value to write to the port. When the PIC16 writes to the port, it writes the value for all the pins at the same time.

For example with trisb set to zero, if you set portb.1=1, then later set portb.2=1. If portb.1 is excessively loaded, the PIC16 will read portb.1 as a zero and write 4 (0b100) to portb, instead of the expected 6 (0b110) to portb.

There are three solutions to the above issue:

One solution is to use a processor that does allow us to read the state of its output pins -- such as the LATx register on the PIC18 processors.

The second, and by far the best is to ensure the output pins are not excessively loaded. This is the best solution, as code not necessarily under your control, such as UART and I2C code will not affect the remainder of your application. Unfortunately, this doesn't work when implementing an open-collector bus.

The third solution is to use some kind of 'shadow' register, in which the individual bits can be manipulated, then the resulting CHAR values can then be written to the port. You use this shadow register to emulate the LATx registers on other processors.

The various _port macros below, if called in place of the _bit macros, should do the job:

unsigned char sport[2]; // amend to # PIC ports

#define set_port(port, no)\
	set_bit(sport[&port-PORTA], no);\
	port=sport[&port-PORTA];

#define clear_port(port, no)\
	clear_bit(sport[&port-PORTA], no);\
	port=sport[&port-PORTA];

#define test_port(port, no)\
	test_bit(sport[&port-PORTA], no)

#define toggle_port(port, no)\
	toggle_bit(sport[&port-PORTA], no);\
	port=sport[&port-PORTA];

Further reading[edit]