You are looking at the HTML representation of the XML format.
HTML is good for debugging, but probably is not suitable for your application.
See complete documentation, or API help for more information.
<?xml version="1.0"?>
    <allpages gapfrom="Relay CPU" />
      <page pageid="3035" ns="0" title="Read before write">
          <rev xml:space="preserve">==== Read before Write ====

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[&amp;port-PORTA], no);\

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

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

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

== Further reading ==

* [ The Read-Modify-Write problem]
* [ Circuit guru: &quot;PIC microcontroller read-modify-write bugs&quot;]
* [ Cornerstone Robotics: &quot;Read-Modify-Write (RMW) Problem with Mid-Range PIC Microcontrollers ... and Avoiding the Read-Modify-Write (RMW) Problem with LATx Registers in PIC18Fxxxx&quot;]
* [ Stackexchange: &quot;What causes turning ON an single output pin on a Microchip PIC16F690 to spontaneously turn OFF another pin on the same port?&quot;]

      <page pageid="7098" ns="0" title="Real Time Clock -- Keeping Time with a Microcontroller">
          <rev xml:space="preserve">Often a micro controller project needs to know what time ( perhaps including date ) it is, say for a data logging project, or even more directly a clock.  This article deals with methods of keeping time.

=== Real Time Clock Modules ===

This is a module includes a highly precise oscillator ( often based on a clock crystal ), suitable counters to count the oscillations, and an interface for the micro controller.  There is a good overview at Wikipedia  and an easily available one can be found at Sparkfun  Often these devices are very low power making them practical for battery powered projects, or for mains powered projects with battery powered backup.  Real time clocks information is easy to find on the web, this article will largely focus on other methods.

=== Just Use the Micro controller Clock ===

Micro controllers all have clocks for timing their own processing.  You can use software and the counter timers ( on most chips ) to determine the time.  You will also have to write software to manage the clock.  Accuracy of the oscillator  is important here, and generally crystals are the most accurate.   A  .01 percent accurate crystal will be off by about 10 seconds a day.  This may be accurate enough or not.  With more complex software you can add correction routines to the clock and increase the accuracy of the clock pretty much as much as you want, until you are limited by the stability of  oscillator, one again a .01 percent stability is about 10 seconds a day.  Probably the most important factor for stability is temperature.  So if you temperature is more or less constant stability may be good.  Aging of the crystal can also effect the stability.  You can improve this accuracy by feeding the temperature to an error correction routine, or you can use a temperature controlled “oven” to improve the stability.  ( Some of these techniques can also be used with real time clock modules or may be built into them ).  

=== Use the Power Line ===

The power line ( mains power ) runs at 60 Hz and over the long term is very stable.  If you are using a plug in power supply then this is an easy option.  

Overview:  This assumes that you are using a transformer based power supply.  This will have a low voltage AC output.  Some of this you will rectify, filter, and probably regulate.  Use a second tap on the transform and a diode for half wave rectification.  You do not filter this, instead put a resistor in series with it and a diode to the regulated  micro controller power.  This will give you a 60 Hz wave clipped at 0 volts and the micro controller voltage.  This signal is safe to feed into a micro controller input pin.

There are several ways to process this signal, here is one:

Set the input pin to input and feed it to a counter.  When the counter gets to a count of 60 ( some counters count up, some down, do what is appropriate for you processor ).  it should trigger an interrupt and and count the second.

Full code, schematics, etc. for the PIC microcontroller can be found in the project at: [[PIC based Stepper Motor Dancing Analog Clock]]

[[category: clock]]</rev>