Interrupt On ShutDown

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 6.

Moderator: Benj

Post Reply
George_B
Posts: 128
Joined: Wed Jul 04, 2012 11:21 pm
Location: Greece
Has thanked: 51 times
Been thanked: 19 times
Contact:

Interrupt On ShutDown

Post by George_B »

Hello, i hope you all be well.

I am working on a project that i need to keep some important variables saved in Eeprom in case of power off of the device. At the moment i save the variables in Eeprom in approximately every 1 minute.
I worry that i use the Eeprom write command too often and i expect that my device will exceed the manufacturer's write times and fail with unwanted results soon.


The first solution that comes in my mind, is to have a pin (from PORTB) connected to the main power supply which will trigger an interrupt while changing status(from HI to LOW for example).
Unfortunately i cannot redesign my device's circuit either modify it and at the moment there is not available option for external interrupt connection.


I am wondering if i could use somehow the Brown Out Reset register and trigger an interrupt routine(where i will save some variables in Eeprom) before and every time that the power goes off?

Any other suggestion or solution in mind ?

Thanks in advance!

Regards
George

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Interrupt On ShutDown

Post by Benj »

Hi George,

It depends what your saving but a common trick is if the value doesn't change too often then write the value to the eeprom on change. Of course if the changes are happening all the time then this is not possible.

Unfortunately I don't think you can detect an impending brown out condition. If there is a lot of capacitance already then you can maybe monitor the VCC against a fixed reference but this probably isn't going to help on a fixed circuit.

A typical EEPROM can be erased and written around 100,000 times up to 1,000,000 times on some devices. 100,000 times at once per minute gives you just under 70 days of run time before potential failure, not ideal for a long running application. Often manufacturers state worst case so it's not a hard rule and may go on for many multiples of this.

A good tip is to check if the value in the eeprom has changed. If not then do not overwrite.

Another good solution is to have the first eeprom location contain a pointer to the information your storing. The first eeprom location at the pointer offset address is the count of how many writes has been performed in that location. Then comes your saved values. Once the number of writes goes past a certain value change the pointer offset address and write to the new location offsets. This helps to spread the wear of the EEPROM locations across the entire memory which again helps to prolong the expected operation life. e.g. you have 16 values (15 + count value) and 256 bytes of EE memory you can theoretically get (100,000 * (256 / 16)) erase write cycles or around 3 years at 1 minute write intervals (not accounting for the pointer location).

Some very quick hashed together example code...

Code: Select all

//On power up read the eeprom pointer
eePointer = EERead(0)
if (eePointer = 255)
  eePointer  = 1

Code: Select all

//Reading values from the pointer
eeWriteCount = EERead (eePointer * 16)
eeVal1 = EERead ((eePointer * 16) + 1)
eeVal2 = EERead ((eePointer * 16) + 2)
...
eeVal15 = EERead ((eePointer * 16) + 15)

Code: Select all

//Writing values from the pointer
if (eeWriteCount = 200)
{
  eePointer = (eePointer + 1)    //eePointer = 1 - 15
  if (eePointer == 16)
  {
    eePointer = 1
  }
  EEWrite(0, eePointer)
  eeWriteCount = 0
}
else
{
  eeWriteCount = eeWriteCount + 1
}
EEWrite (eePointer * 16, eeWriteCount )
EEWrite ((eePointer * 16) + 1, eeVal1)
EEWrite ((eePointer * 16) + 2, eeVal2)
...
EEWrite ((eePointer * 16) + 15, eeVal15)

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Interrupt On ShutDown

Post by medelec35 »

What I have done is supply microcontroller VDD via a diode from +5v.
So anode is conneted to +5V power supply and cathode is connected to VDD of microcontroller.
Connect a 470uF capacitor across microcontroller VDD and GND
Connect an interrupt (I used INT0 on falling edge) To +5V supply.
When power goes off, INT0 is triggered, which in turn calls the macro that saves all variables to EEPROM
The 470uF kept VDD high for long enough.
If a fair few variables then you may have increase Cap but at least you have a staring point.

You need to take into account that INT0 will more than likely be triggered more than once.
So you need to have a way of any subsequent triggering within a short space of time.

If you would like to look at flowchart I did to take the above in to account, then let me know.

Martin
Martin

George_B
Posts: 128
Joined: Wed Jul 04, 2012 11:21 pm
Location: Greece
Has thanked: 51 times
Been thanked: 19 times
Contact:

Re: Interrupt On ShutDown

Post by George_B »

Hi Benj and Martin, thank you for your replies both.

Benj, really good idea to move up on a new location in eeprom once you reach the write times limit! I will make some tests on this, however i need to study you reply again and again because i didn't catch the way you mention properly. I would prefer a flowcode component illustration. :roll: JJ


Martin, this is a technique that i was tested on another project in the past and it works like a charm, so thanks for a good suggestion! I was a bit confused while i was reading your reply at this point:

<<You need to take into account that INT0 will more than likely be triggered more than once.
So you need to have a way of any subsequent triggering within a short space of time. >>

Could you please explain more ?


George

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Interrupt On ShutDown

Post by medelec35 »

I created a flowchart to count the number of times the INT0 macro was accessed and store in EEPROM on power failure.
Result was twice.
You you must do is just ensure the EEPROM components only send the data once only.
A way to do that is is set up a variable called DisableDataSend or something like that.
Initialise with a value of 1.
Within the INT0-ISR macro use a decision branch:
Save EEPROM on power fail.png
(44.89 KiB) Downloaded 4569 times
The secrete is to clear DisableDataSend in Main by placing that the bottom of the flowchart within main loop:
Save EEPROM on power fail 2.png
(18.34 KiB) Downloaded 4569 times
As you can see the variable with decision branch does not have =
What that means if the decision will be true if the values is any number greater than 0
So

Code: Select all

if DisableDataSend = 1
or

Code: Select all

if DisableDataSend > 0
would all work as well.
Martin

George_B
Posts: 128
Joined: Wed Jul 04, 2012 11:21 pm
Location: Greece
Has thanked: 51 times
Been thanked: 19 times
Contact:

Re: Interrupt On ShutDown

Post by George_B »

Hi Martin, thank you very much for your detailed reply.

Until now your description looks understandable to me... except one thing. I just cannot understand why you are saying that the Interrupt macro is accessed twice? Could you please explain that in low level? :wink:




Thanks
George

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Interrupt On ShutDown

Post by medelec35 »

Hi George,
I don't know the actual reason, that's why cant answer question.
All I know is interrupt pin is set to trigger on a falling edge.
The voltage on the pin must fall, rise then fall again.

Martin
Martin

Post Reply