0

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.

2 Answers2

2

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.

Elliot Alderson
  • 31,192
  • 5
  • 29
  • 67
  • 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... – Chris Stratton Jan 24 '19 at 02:47
  • @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. – Elliot Alderson Jan 24 '19 at 02:53
  • So, is that thing possible or not? If yes, can you help me with a snippet? – Deep Chhowala Feb 03 '19 at 06:22
  • 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. – Elliot Alderson Feb 03 '19 at 13:27
-1

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.

Keltari
  • 163
  • 9
  • Yes, but is it possible to do this thing in ATmega16 controller? I am very new in this field. – Deep Chhowala Jan 23 '19 at 18:35
  • @DeepChhowala The ATmega cannot recover from a power loss.in that way – Keltari Jan 23 '19 at 18:50
  • 1
    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. – Chris Stratton Jan 24 '19 at 02:44
  • So, is the thing that I want, possible or not? – Deep Chhowala Feb 03 '19 at 06:17
  • I can paste my whole code if you want to see that, its very small, max 50 lines I guess. – Deep Chhowala Feb 03 '19 at 06:17