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 » Writing into memory while running program

January 24, 2013
by eminthepooh
eminthepooh's Avatar

Hi, I'm looking to save some variable values after a power cycle. I've read around that this could be achieved by EEPROM but i'm not sure if it's right.

I'm looking to have the program running on an m168, for me to define a variable as a certain number thru buttons, and for the avr to save my selection so that when I cycle the power, it remembers my last choice.

Of course, I've got the defining a variable and buttons part down, just don't know how to save that variable past power cycling.

Thank you,

January 24, 2013
by Rick_S
Rick_S's Avatar

The EEPROM is flash memory and it will retain what is written to it through power cycles. You are definitely on the right path. There is even a Library Entry for this to help you see how it can be done.

Rick

January 24, 2013
by pcbolt
pcbolt's Avatar

eminthepooh -

Just one thing to be aware of using EEPROM. You can read EEPROM as much as you like, but you only get 100,000 "writes". It may seem like you'd never reach this limit but if you accidentally put a EEPROM write statement inside a "while(1)" loop, this limit would be reached very quickly. General rule of thumb is to write to it only when the user changes some state in your program.

January 24, 2013
by JimFrederickson
JimFrederickson's Avatar

Technically the EEPROM is good for only "AT LEAST 100,000 write cycles"...

Some will last much longer.

I am pretty sure the EEPROM is "byte oriented" so there are not "page writes".

If you are writing "often" you can set aside EEPROM as a buffer that can hold several values. Then you can "write to the next free area" or use a "sequence number" to determine where the write should be.

However you do it...
The idea for data that may change often is that instead of changing only 1 location over and over you spread your changes around. So if there are 2 locations you effectively get 200,000+ write cycles, 3 locations 300,000+ write cycles, etc...

As noted by PCBolt though...

Be careful, if you do make the mistake of writing data needlessly what seems like a pretty large lifetime can go away pretty quick!

Post a Reply

Please log in to post a reply.

Did you know that you can connect to certain car computers via the OBD-II port with a microcontroller? Learn more...