Well, I think you need to rewrite your question because I interpreted it differently again. I thought you want to count the number of times power was removed.
If this is the case, just count the number of times it is powered up, powered down is one less than this! You would still need early warning of supply failure though. But you always need this anytime you write to FLASH.
Another possibility if my interpretation is wrong and you want to count something else is to use batteries or supercap to keep RAM alive when power is removed and keep count there. You can always count while the power is "off" using this method too. You can write it to flash when power is back up.
Regards David
Hello,
My understanding of your requirement is that you want to count pulses while the power is on, and store the current count value just prior to power being removed, and then recall this value when power is re-applied. Am I correct?
If so, power would need to be maintained for a period sufficient to program the value to flash memory. This would be relatively simple to do if the removal of power is controlled by the equipment, so the actual removal of power can be sufficiently delayed.
However, if the removal of power is external to the equipment, the situation would be more complex. You would need "early warning" of the removal, prior to drop-out of the regulator supplying the MCU. The capacitors associated with the regulator would then need to supply the MCU for a period, perhaps a few milliseconds - the LV reset circuit of the MCU must not operate until after the data has been programmed to flash. The early warning voltage detection circuit would need to be isolated from the input capacitor of the regulator, perhaps by means of a series diode.
Another issue that your may need to consider is the limited number of erase cycles allowed for the flash memory. Assuming only a few bytes of data need to be saved, there are techniques available for cycling the data throughout a flash page, so that many power cycles could occur before a page erase was required.
Regards,
Mac