0
\$\begingroup\$

I am writing my code in BASCOM-AVR. It has 2 if loops and 1 for loop inside the 1st if loop. I will give DC supply to ATmega16 controller. Now, what I want is, if the power is cut off when code is in for loop, the functioning should continue from that very step inside for loop when power is switched on. Is that possible?

If anyone is unable to comprehend my question, please let me know, I'll try to explain it in a different way.

Kindly waiting for help.

\$\endgroup\$
  • \$\begingroup\$ @DavidPostill okay. thankyou.. let me know if you find something. \$\endgroup\$ – Deep Chhowala Jan 23 at 18:33
  • 1
    \$\begingroup\$ why do you want that behavior? \$\endgroup\$ – jsotola Jan 23 at 18:40
  • \$\begingroup\$ I think your discussion of two loops is not relevant to your problem. If so then I suggest you remove it from your question. Can you add a short term supply extension with a capacitor and put the unit to sleep when power is lost and wake from sleep on power restore? \$\endgroup\$ – Transistor Jan 23 at 18:41
  • \$\begingroup\$ Yes, what you describe is possible. \$\endgroup\$ – brhans Jan 23 at 19:22
  • \$\begingroup\$ A shutdown mode allows the MPU to save parameters to flash or EEprom or NVRAM. When it wakes up it reloads index's of where it left off. \$\endgroup\$ – Sparky256 Jan 23 at 19:52
2
\$\begingroup\$

As a practical matter you cannot do this. In order to accomplish what you want it would be necessary to save every register in the processor and every RAM location to some kind of non-volatile memory every time something changes, and then somehow reload all of the registers and RAM from the non-volatile memory when power is restored. You would also need to make sure that the processor didn't lose power while it was in the middle of doing this copying, otherwise you end up with a bizarre state in the non-volatile memory. It would be a good idea to add error checking to make sure you saved a valid image...and all of this must be done after every instruction is executed.

A more reasonable option is to add some kind of battery backup, then detect when the main power is falling and move the processor to a low-power state.

\$\endgroup\$
  • \$\begingroup\$ That's not quite true, the program can be written to checkpoint meaningful operations and save the key information to resume upon power failure. Perhaps it's enough to save the two loop counters; perhaps saving fractional progress through the inner loop is needed too, but it is achievable. Only things with side effects cannot be safely repeated; if you stop partway through a computation it's fine to repeat that from the start, just as long as no permanent action was yet taken. In fact modern processors do this all the time in the form of speculative execution... \$\endgroup\$ – Chris Stratton Jan 24 at 2:47
  • \$\begingroup\$ @ChrisStratton Thanks for the clarification. What you say is true but suggests that the OP is willing and able to perform the kind of analysis that would identify the critical points. I felt that the OP was looking for something much easier and I was trying to emphasize the practical difficulty. \$\endgroup\$ – Elliot Alderson Jan 24 at 2:53
  • \$\begingroup\$ So, is that thing possible or not? If yes, can you help me with a snippet? \$\endgroup\$ – Deep Chhowala Feb 3 at 6:22
  • \$\begingroup\$ It's not a "snippet", it's a difficult problem. First, modify your hardware so it detects the loss of main power but has enough energy left in the power supply capacitors to do some work. That work is to save the complete state of the processor...whatever that means to you. Then write a reset handler that detects that power was lost, reloads the state, and starts from there. \$\endgroup\$ – Elliot Alderson Feb 3 at 13:27
0
\$\begingroup\$

Off topic for Superuser, but will give a brief answer.

In order for your code to recover from a power loss, you need add code that logs its progress to a file. Depending on your needs, you might need to log the start and/or completion of of each action. Then you would add code to the beginning of the script that would read to log and resume where it left off. Obviously, with a power loss, something might not get properly logged, so you might have write code to verify if things were done.

\$\endgroup\$
  • \$\begingroup\$ Yes, but is it possible to do this thing in ATmega16 controller? I am very new in this field. \$\endgroup\$ – Deep Chhowala Jan 23 at 18:35
  • \$\begingroup\$ @DeepChhowala The ATmega cannot recover from a power loss.in that way \$\endgroup\$ – Keltari Jan 23 at 18:50
  • \$\begingroup\$ Your claim that an ATmega cannot do this is factually false. If anything, it's easier on a small system, as you can run off a capacitor long enough to save state, and you have on-chip EEPROM to save it in. You could use a larger storage device too, but those often don't like unexpected power loss, regardless of the system they are connected to. \$\endgroup\$ – Chris Stratton Jan 24 at 2:44
  • \$\begingroup\$ So, is the thing that I want, possible or not? \$\endgroup\$ – Deep Chhowala Feb 3 at 6:17
  • \$\begingroup\$ I can paste my whole code if you want to see that, its very small, max 50 lines I guess. \$\endgroup\$ – Deep Chhowala Feb 3 at 6:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.