Storing a Value to EEPROM When Power is Shut Off to a ATtiny841

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am having difficulty with Codevision AVR in storing a value to EEPROM by using an Analog Comparator 0 interrupt-service routine.

 

The comparator interrupt-service routine is triggered when the power to the board is shut off and the voltage to the comparator (-) input falls through the value of the on-board band-gap reference (about 1.1V).

 

A voltage divider feeding the analog comparator (-) input is arranged to make that happen before the AVR supply voltage drops below 5.0V.  So, the AVR is still able to "hear" the comparator when it toggles and store a value to EEPROM.

 

I have attached a simple program (EEPROM Dilemma) to illustrate my problem.

 

Either line 49 or line 172 provides the call to the function "location_mode ( )", where a value is assigned to a variable 'dim_cmd'.

 

If line 49 is active (and 172 is commented out), location_mode ( ) is called only ONCE, from main ( ).  I have verified that the correct value of 127 is stored in EEPROM when I shut the power off and Analog Comparator 0 does its job.

 

If line 172 is active (and line 49 is commented out), then location_mode ( ) is called REPETITIVELY from inside the TIMER0 interrupt service routine, repeatedly assigning the value 127 to dim_cmd.  However, when the power is removed and the analog comparator trips, the default value of 255 is stored in EEPROM, not the value (127) which had been repeatedly assigned to dim_cmd inside "location_mode ( )".

 

In my real program, I am assigning a value to dim_cmd from within "location_mode ( )", and  -- as in the case where line 172 is active in the sample program-- it does not work.

 

Why is this so?

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

One possible issue with your scheme is that it takes a fairly long time to write to the EEPROM. I recall a few milliseconds. If the power drops too low before it is finished, it won't be successful. I save when the variable changes, rather than when the power drops. There is an avr-libc function (something update) that compares the existing values to the new ones and only writes the ones that have changed. This avoids repeated writes that can "wear out" the EEPROM.

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Use oscilloscope to find out how quiclky supply voltage drops so you will know how much time you actually have to write EEPROM.

I recommend to use CLKPR register to lower clock speed prior to writing EEPROM. This will allow the microcontroller to work with lower supply voltage so you will have a bit more time. I haven't found minimum supply voltage required to successful EEPROM write, but I think it is lower than voltage required by the microcontroller to work at 8MHz.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Use capacitor bank to lower the voltage drop speed.
But consider the low voltage cut off detector.
.
MG

I don't know why I'm still doing this hobby