Pages: [1]   Go Down
Author Topic: Interrupts  (Read 617 times)
0 Members and 1 Guest are viewing this topic.
Brazin
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, I trying to use the interruption to pause my robot. But I when the interrupion ends I want to go to the inicial part of the main, and not come back where the program was.

So I need to change the the Satck point to the address of the first command of the main, or chance de program counter after the intrruption to the inicial os the main.

There's something that I can do? Or just if program in assembly?
Logged

Lucas Cavalcanti

Austin, TX
Offline Offline
Full Member
***
Karma: 2
Posts: 182
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

HI,

I understand what you want to do.  You need a different approach, however.

Let's talk about "main" first.  The actual main() function of an arduino sketch is in the arduino core library.  It sets up the arduino board, calls your setup() function and then calls your loop() function over and over again, forever.  To really re-do main() would be the equivalent of a reset. Changing the PC or stack to jump there is not a good idea.  Things will get left on the stack, globals and static variables will not get initialized, and memory will have junk in it.

You can try to do a hardware reset (there are threads in the forum on this) but it is slow and could leave any devices attached to the arduino in an unknown or dangerous state.

What you really need to do is carefully structure your loop() function so that it puts your robot into a known good inactive state when an flag (set by the interrupt function) is in the right state.
Code:
#define NO_INTERRUPT 0
#define INTERRUPTED 1
#define INTERRUPT_ENDED 2

volatile int flag;
loop()
{
    while (flag == INTERRUPTED)
          delay(50);
    if (flag == INTERRUPT_ENDED)
          reinitialize();
   ...
   rest of loop function
   ...
}

Where reinitialize() will stop whatever the sketch is doing, put any attached hardware into a good state (turn off motors and sensors, etc), reset all global variables to their initial values, and make sure the arduino is ready to run the rest of loop() again (fix timers and stuff).
This is really the only safe way to do what you want.  It may involve re-writing some of your sketch.  You may have to add additional code to check for the interrupt flag if any of the code in loop() takes a long time, or has loops in it.

Hope this helps,
Logged

Chris J. Kiick
Robot builder and all around geek.

Pages: [1]   Go Up
Jump to: