I have two functions that can possibly be executing while a timer interrupt occurs. After the interrupt occurs, I don't want program execution to resume where it left off in a function. I want the program to resume at the beginning of the main loop. Is there a way to do this? I know I can use "return" in the function to go back to the main loop, but I don't want to have to set a flag in the interrupt service routine that I will have to check throughout the function to check if the interrupt occurred.
An interrupt can only return to the code point where the interrupt occurred.
Having interrupts set flags is very common. And managing program flow based on flags (or state variables) is generally good practice.
I have no idea, from your limited information, whether the program really needs to use interrupts.
...R
Well you could use setjmp(), but that's a problem waiting to happen and you would have to learn how to unroll the stack. Best left for another day I think.
The better way is to structure your code so flags can work.
Can you explain why it's imperative that the interrupted code does not execute a single extra instruction after the interrupt?
Rob
whatever9:
I don't want program execution to resume where it left off in a function. I want the program to resume at the beginning of the main loop.
"Software reset" in the interrupt routine?
My application is like a game that is being played until the interrupt occurs, and then it's a "game over" and I want the game to re-initialize.
How do I do a software reset?
I looked up the reset topic and people were using this code:
void softReset()
{
asm volatile (" jmp 0");
}
This gives me a compiler error that "jmp 0" is a bad instruction. I'm using the Arduino Due, so maybe the inline assembly instructions are different?
whatever9:
My application is like a game that is being played until the interrupt occurs,
You haven't said what cause the interrupt. If it is just based on time it should be easy to get the same functionality without an interrupt using millis().
If it is some external device that brings one of the Arduino pins high or low then your code could easily use polling to check it.
I can't imagine a game where microsecond timing matters.
Even if you insist on using an interrupt all it needs to do is set a flag variable that can be checked by your code.
Also, I can't imagine you want the Arduino to reset at the end of a game and lose the ability to keep track of previous game scores. A software reset should be entirely unnecessary.
...R
The game doesn't keep track of scores. I'm using a timer interrupt because it's best for my application. I don't want to have to keep track of time using millis() and have to continually subtract and check for elapsed time. It could be done, but a timer interrupt is ideal. I just have to figure out how to either do the assembly instruction "jmp 0" for the Arduino Due or a routine that causes the watchdog to time out and it would restart.
What's stopping you from just re-initializing the game and starting over? No need to reset to do that.
I haven't tried it myself but I have read elsewhere in the Forum that the watchdog timer interferes with uploading a new version of the code because it triggers while the bootloader runs.
...R
whatever9:
My application is like a game that is being played until the interrupt occurs, and then it's a "game over" and I want the game to re-initialize.How do I do a software reset?
I looked up the reset topic and people were using this code:
void softReset()
{
asm volatile (" jmp 0");
}This gives me a compiler error that "jmp 0" is a bad instruction. I'm using the Arduino Due, so maybe the inline assembly instructions are different?
Link:
http://www.nongnu.org/avr-libc/user-manual/group__avr__watchdog.html
<avr/wdt.h>: Watchdog timer handling
Defines
#define wdt_reset() asm volatile ("wdr")
#define wdt_enable(value)
#define wdt_disable()
#define WDTO_15MS 0
#define WDTO_30MS 1
#define WDTO_60MS 2
#define WDTO_120MS 3
#define WDTO_250MS 4
#define WDTO_500MS 5
#define WDTO_1S 6
#define WDTO_2S 7
#define WDTO_4S 8
#define WDTO_8S 9
#include <avr/wdt.h>
and in your interrupt routine
wdt_enable(WDTO_1S);
will do WDT *software reset" in 1 second
lar3ry: I'm reinitializing the game by going to the beginning of the main loop, accomplished by reset.
Vaclav, where did you get <avr/wdt.h> from? Now I'm trying to understand if the macros are valid for the Arduino Due...
Thanks for all the responses!
whatever9:
lar3ry: I'm reinitializing the game by going to the beginning of the main loop, accomplished by reset.Vaclav, where did you get <avr/wdt.h> from? Now I'm trying to understand if the macros are valid for the Arduino Due...
Thanks for all the responses!
I was just starting using interrupts and got it from searching the web. Was checking some importnat timing during debugging so I used WDT timeout and WDT reset.
It should work, just put wdt_enable in start of loop and put some Serial print followed by some delay at the head of setup to try it. You will get nice infinite loop...if it works.
Good luck
#define wdt_reset() __asm__ __volatile__ ("wdr")
I don't think this will work unless WDR is a valid SAM opcode (which I doubt)
As for the enable and disable, they are empty, I'm not sure what they are supposed to do.
Rob
Robin2:
I haven't tried it myself but I have read elsewhere in the Forum that the watchdog timer interferes with uploading a new version of the code because it triggers while the bootloader runs....R
This was true of some older bootloader code but was upgraded to prevent WDT interrupts being a problem for the bootloader process.
you need to design the program correctly in the first place - see blink without delay.
Mark
holmes4:
you need to design the program correctly in the first place - see blink without delay.Mark
\
..and post your entire code in quotation marks after you auto format it...
Vaclav:
holmes4:
you need to design the program correctly in the first place - see blink without delay.Mark
\
..and post your entire code in quotation marks after you auto format it...
. . . though I, and most other users, would prefer it if you used code tags instead of quote tags.
Vaclav:
holmes4:
you need to design the program correctly in the first place - see blink without delay.Mark
\
..and post your entire code in quotation marks after you auto format it...
And CERTAINLY not in quotation marks!
whatever9, your method of restarting the game by a reset of the Arduino is not the right approach, IMO. Properly structuring yiur code would serve the same purpose.
Sounds like you plan to use a timer interrupt to indicate 'end of game' and force the Arduino to reboot to start a new game. Both those approaches seem like bad choices to me. The polling solution you "don't want" is the way I'd do it.
PeterH:
Sounds like you plan to use a timer interrupt to indicate 'end of game' and force the Arduino to reboot to start a new game. Both those approaches seem like bad choices to me. The polling solution you "don't want" is the way I'd do it.
Please show how you can accomplish what the OP wanted (!) , not what most replies wanted, in ONE line of code as can be done using WDT.
Personally I have a small issue with "gurus" here repeatedly coming up with stupid suggestions like " read blink without delay " and not paying attention to OP requirements.
Cheers