On Thu, 18 Sep 1997 21:19:31 -0700 WF AUTOMA=?iso-8859-1?Q?=C7=C3O ?= writes: >Hello Pic Maniacs > > Does someone have a suggestion how may i do the Software for >PIC16F84, that when the power off, i had time to record in EEPROM >DATA: 4 >very important Data? This is more a hardware question than a software one. To ensure "time to record," the hardware needs to give the PIC adequate warning that the power is about to go off. Upon receiving the warning, the PIC starts writing the important data to EEPROM. After the writing is done the PIC should try to stop as fully as possible so when the power goes off it is unlikely to write something else into EEPROM by mistake. The warning required is on the order of 10 ms per byte to be stored. Often this is done by sensing the input voltage to the voltage regulator. For example, if the PIC runs from a 5V supply derived by regulating a 12V supply down, the nominal 12V input would be compared to say 8 or 9 V. When the voltage reaches this level the PIC concludes that the 12V power has been removed, thus the 5V is about to fail. The regulator needs a capacitor on its input sufficient to hold up the PIC supply voltage while the writing is in progress. The size of the capacitor is readily calculated C = I * t / V. For example if the PIC and regulator use 10 mA, the save will take 50 ms, and the capacitor voltage can drop by 3V during the save, the minimum required capacitor value is 166 uF. The voltage drop warning could cause an interrupt or it could just be polled, since a delay of a few ms in recognizing it is not a problem. For cases where it would be difficult for the hardware to provide advance warning that power is failing, it is necessary to maintain a constant "shadow" of the important data in the EEPROM. The algorithm to do this takes into account how often the important data changes, how often the power is to fail, and the EEPROM's rated life. If the important data changes only once each hour, it could be saved to EEPROM everytime it changes (10 years = 87,600 hours). If it changes more rapidly than this then the saving must be done less often to keep from wearing the EEPROM out. Not saving every change of course makes it likely that an old version will be recalled when the power comes back on. Another problem with periodic saving is that if power fails during a save the data may be garbled. One solution to this would be to save two copies with a scheme such as CRC applied to each copy to check if it is valid. If upon turning the power back on, a copy fails the CRC the other one would be used. The bad copy would be fixed by rewriting it from the good one. (If both copies fail CRC, obviously this is a problem. This shouldn't happen if it is just power on/off causing garbled data to be written)