I need to know if a variable stored in a non volatile memory, after a Power On Reset do this variable return to its default value, or it keeps the last valid value before the reset
-
It depends how the device and how the non-volatile memory of it works. So without even knowing which device you mean it can't be answered. – Justme Dec 07 '22 at 16:17
-
First sentence of [wikipedia non-volatile memory](https://en.wikipedia.org/wiki/Non-volatile_memory): "Non-volatile memory (NVM) or non-volatile storage is a type of computer memory that can retain stored information even after power is removed. In contrast, volatile memory needs constant power in order to retain data." – Lundin Dec 08 '22 at 15:59
2 Answers
The whole point of a non-volatile memory is that its contents are unaffected by power loss, reset etc and only change when deliberately changed by a system.
So if your memory is an NVM then it is unaffected by power-on reset and will retain its contents.

- 21,742
- 4
- 39
- 62
Typically you don't store variables directly in non-volatile memory because that memory technology often has limited write cycle life and may also be very slow (hundreds of microseconds or milliseconds not hundreds of nanoseconds). So you would store variables for use in (volatile) RAM, save a copy to NV memory under some conditions, and restore them when power is reapplied. That would be done by software or firmware in the system that you would have to write.
If you are writing a program there is usually a way to tell the compiler where to store the variable. In the case of constants you can usually choose to store them in non-volatile memory which means they don't have to be read out from non-volatile memory into RAM at start-up, which also saves RAM space (perhaps with slower access time). The initialization of constants in RAM from NV memory at start-up is typically take care of for you by the compiler start-up code (in C static variables are also cleared).
For variables, it may not be possible to capture exactly the most recent values because you don't want to write too frequently to the NV memory. For example, if you have a controller and the setpoint is only changed manually, it's no problem to store it to EEPROM whenever it is changed, since the erase/write life might be 100,000 cycles. But if you want to capture the value of some rapidly changing variable then some compromise may be necessary.
The above is true of flash and EEPROM memories based on Fowler-Nordheim tunneling. There are other technologies such as ferroelectric RAM (FRAM) and battery-backed RAM that don't have severe write cycle limitations, but they have other disadvantages (such as requiring special IC process technology or requiring a battery which has limited life and has to be changed periodically).

- 376,485
- 21
- 320
- 842