Jump to content
Sign in to follow this  

Writting To Eeprom On Mains Failure

Recommended Posts

Hello Wizards,

After fixing the bug in my Timer code, now I am proceeding to next part of it and rather more challenging one. Please refer to the code in my previous post under topic "Precise Timings with Timer0". This timer counts preset time in mins/secs. now if there is a power failure in between the running count, I want the current value stored in eeprom and as soon as the power resumes, the counter must count the remaining value stored (but only once). Any ideas for implementing this?

Thanks in Advance


P.S: I posted this query here as I am concerned about software part of the project rather then the hardware (Any suggestions on how to implement hardware are most welcome though!)

Share this post

Link to post
Share on other sites

Is your specification that the timer's total ON time must remain correct, but that it can be interrupted by a power failure of any duration which it must continue after?


You will need the following hardware: a circuit to maintain power to the PIC for long enough to do a yet to be determined number of worst case EEPROM write, an input that detects the loss of power immediately while you have enough stored power to complete an orderly shut-down and protection against glitches at power up/power down


The software side is mostly handled by your existing EEPROM routines.

At power up, read time remaining from the EEPROM and compute a checksum.

If it is valid, set up the time accordingly and resume timing. If not or the time is zero, continue with your time setting routine.


Power failure must trigger an interrupt. The first thing is to turn off ALL LEDs and the relay to save your remaining 5V power. Then you save the remaining time to EEPROM. compute the checksum and store that as well. finally you loop testing the power detect input until you run out of power and the PIC stops or until the power comes back if it was only a short glitch. If the power does come back you can restore the display and relay status, return and carry on.


When the timer period has finished, it is essential to write zero time remaining to the EEPROM to prevent any repeats.


You cannot just simply write the EEPROM on every pass of the main loop as the repeated very frequent writes will damage the EEPROM which will also be corrupted by power failure during write or possibly even damaged. Worst case, the EEPROM is good for 100 K write cycles to a particular location which means if you write once a second it could fail in little over a day, and the average endurance is 1 M write cycles so you can certainly expect it to fail within a couple of weeks of use. Once its failed there is no way of repairing it, short of replacing the PIC.


The interrupt approach would guarantee you a minimum of 50 K timing cycles even if the power fails once in every one! Assuming a 10 minute cycle with a 5 minute power failure and the next cycle is started immediately, that's two writes per location in a 15 minute interval which gives a worst case endurance of 1 year 5 months of continuous use or an average of 14 years. The relay would wear out first! :-)



Share this post

Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Create New...