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