NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » How to store a variable to flash memory?

April 05, 2011
by leedawg
leedawg's Avatar

So I have a little problem I need my MCU to be able to remember a variable after it has been powered off. Is there any way to a write a variable into its flash memory or how would one store something like a count of cycles so that you keep adding to it after you power the system off and then back on? Or is the MCU just not capable of doing this? If not how would anyone suggest accomplishing this? Thanks for any input or help.

lee

April 05, 2011
by bretm
bretm's Avatar

You don't use flash memory for that, you use EEPROM. Search for that and you should find a number of threads showing how to use it.

April 05, 2011
by Ralphxyz
Ralphxyz's Avatar

Over at AVRFreaks there is a guy named Dean Camera thats has done a number of great tutorials.

He has a very good one on how to use the ATmega internal EEPROM.

I just looked at AVRfreaks but could not find their Tutorial section so if you want it and can not find it on the AVRfreaks site I have a copy I can get to you.

Ralph

April 05, 2011
by leedawg
leedawg's Avatar

Thanks for all the great replys.

Upon searching the correct terms I found the link you speak of to the tutorial for the use of eeprom. http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=38417&highlight=eeprom

Um sounds like you can only use it so many times however which is a little disappointing guess ill have to rethink how important it is to store the count of my variable or set up some type of system where you push a button to store it before you power off so that it is not constantly writing to it to update the variable. Thanks again for the pointer in the right direction.

Lee

April 06, 2011
by Ralphxyz
Ralphxyz's Avatar

There are schemes to alternate the memory used that I have seen mentioned. Apparently it's the individual bits that have to be thought of not the whole EEPROM.

Ralph

April 06, 2011
by bretm
bretm's Avatar

Individual bytes, not bits. Someone did a test where they re-wrote the same byte over and over for hours (days?) and it lasted a lot longer than the specified lifetime but eventually did fail. The very next address was unaffected.

One thing that I have done is create a "soft power" system to power the microcontroller. This allows you to write to the EEPROM only once per power cycle. You press a button to turn it on, and then press the button again to request to turn it off. The microcontroller sees this request, saves its state to EEPROM, and then shuts itself off.

The bottom schematic here shows the circuit I used. When you press the button it switches on a transistor which controls the power. The first thing your program has to do is pull a pin high to keep that transistor on even after the user releases the button. Then wait for them to release the button.

When they press the button a second time it does nothing to the power but the MCU can respond to the button press. I made it respond by writing state information to EEPROM and then pulling the power-control pin low to shut off its own power supply. It's pretty cool, actually.

April 07, 2011
by leedawg
leedawg's Avatar

Thanks so much for posting that. Very clever design much like using relay motor control starters with buttons for start and stop. This would for sure limit the number of write cycles to the chip and extend the life.

Thanks again ive got a lot of tinkering and work to do now to learn the new code and implement it in my project.

Regards

Lee

Post a Reply

Please log in to post a reply.

Did you know that SPDT stands for "Single Pole, Double Throw"? Learn more...