9 years ago.

Saving and retrieving data from local non volatile memory

I am making a single phase digital energymeter using the mbed LPC1768 platform. The code is ready and tested to be working as expected. But there is a slight problem, every time the microcontroller is powered off, the energy value gets lost as its in the RAM. In an ideal case the meter will always be powered on, but i want to insert a safeguard just in case there is power loss to the meter. So is there any way to efficiently save the energy data to a nonvolatile memory and then retrieve it back whenever i need to? Ideally what i would want to do is save the data to the non-volatile memory after each cycle of calculation and retrieve it at the beginning of the code once such that the saved value is used further in the program in case of a reset. The energy value is getting stored in a variable called "energy" in the program. I need to save this variable only. If anyone has a working code to do something like this please help me out. I dont want to use any external devices like SD card etc.

2 Answers

9 years ago.

If you are using the mbed LPC1768 device then you should be able to store the data in the LocalFileSystem.

Any example code on how to implement? Nothing in the documentation.

posted by Anuvab Biswas 20 Apr 2015

The HelloWorld example on that page shows you how to initialize the LocalFileSystem and then use standard C functions like fopen(), fclose(), etc to access files within the LocalFileSystem. You may also want to perform a Google search for other standard C routines like fread(), fwrite(), fseek(), etc to write and read your data.

posted by Adam Green 20 Apr 2015

From what i understand, accessing the local file system takes time and its blocking. My program has an interrupt routine which reads voltage and current values from the ADC every 0.5ms. How will that be affected if i use the LocalFileSystem ?

posted by Anuvab Biswas 20 Apr 2015

Yes, that would impact your performance. Check out Robert's idea of using the RTC if you are able to add a battery to keep Vb pin supplied with power.

posted by Adam Green 20 Apr 2015
9 years ago.

You can use registers RTC. Look at the user manual, chapter 27.6.6.1
Is very easy to use one of theses registers in one of your programs, register 0 is for instance LPC_RTC->GPREG0

save a char x

LPC_RTC->GPREG0 = x;

Provide a backup battery (pin VB) .