Log in

View Full Version : Interrupt timer, executing code asap?


SeanCassidy
02-02-2004, 19:50
Thanks for Innovation First for that good white paper on timers, without that I'd be lost. :)

Well I got the interrupt timer working, looks good. So I wrote this function to use the loop code. Here is the code:

void example(unsigned int seconds, char startEnd)
{
static char runOnce1 = 0, runOnce2 = 0;
if(startEnd == 1 && runOnce1 == 0)
{
int snapSecondCnt = secondCnt; //Take a snapshot of the seconds
runOnce = 1; //Make sure it doesn't run again
//Run any command to start here
}
else if(startEnd == 0 && runOnce2 == 0)
{
if(secondCnt == (snapSecondCnt + seconds))
{
snapSecondCnt = 0;
runOnce2 = 1;
//Run any other stopping command here
}
}
else
{
printf("example() error!\n");
}
}

void Process_Data_From_Local_IO(void)
{
/* Add code here that you want to be executed every program loop. */
if (updateDisplay)
{
INTCONbits.GIEL = 0; /* Disable Low Priority Interrupts */
updateDisplay = 0;
INTCONbits.GIEL = 1; /* Enable Low Priority Interrupts */
rc_dig_out04 ^= 1; /* Pin4 toggles every 1s */
secondCount++;
example(10,1); //Start - My edit!
example(10,0); //End - My edit!
printf("Pin 4 = %d, Elapsed Time (s) = %d\n",(int)rc_dig_out04,secondCount);
}
}

Now let me explain briefly what this code does if you do not understand, it is vital to my question. The normal loop executes, until example(). It tells example to have a timer for ten seconds, and the second example is to stop the timer. I'm sure I could've done that a little better. So in example() if it's starting and it's its first time running (runOnce#) then it starts a motion, like say going forward (pwm01 = 254; pwm02 = 254; just say those are our motors at full throttle forward). The loop executes until the satisfied time is up (if(secondCnt == (snapSecondCnt + seconds))). Then it stops.

My question: does this approach even work in practice? I know that you have to be zip zip in and out of interrupts, but in the whitepaper it says that the interrupt od is the most effective timer. So does my example() start and end always take less than one second? Have I approached this totally wrong? Any tips?

I wouldn't recommend anyone to take this code for their own, it most likely doesn't work. :)

SeanCassidy
05-02-2004, 21:17
Was I meandering in my meaning? I apologize, sometimes I do that.

My question is purely that if a function is executed, does everything else stop while that is exected?

Random Dude
05-02-2004, 21:36
Was I meandering in my meaning? I apologize, sometimes I do that.

My question is purely that if a function is executed, does everything else stop while that is exected?


Yes, only one thing can be happening at a time. When an interrupt occurs, whatever is currently happening stops, and the data being used gets set aside (onto the stack actually) Then the interrupt handler runs, and handles the interrupt (obviously). Then whatever code was running before resumes where it left off, without any real knowledge of the interruption.

Let me know if this doesn't answer you question.

SeanCassidy
05-02-2004, 21:48
So my example(), if it took 5 seconds (start and end each), then the timer itself would be off by 10 seconds? Well I need a fork(). :)

deltacoder1020
05-02-2004, 22:33
technically, even fork() only does one thing at a time ;) it just interlaces the needs of multiple things, so that part of one function executes, then part of the next, et cetera.

solution? no function in your bot code should last 5 seconds ;)

KevinB
05-02-2004, 22:41
solution? no function in your bot code should last 5 seconds ;)
No function in your bot code should last > 26.2 milliseconds. :D

deltacoder1020
05-02-2004, 23:42
that too ;) heck, no individual function should last anywhere near 26ms :)

Astronouth7303
06-02-2004, 15:22
except sine ;):);):) Well, if you forgot the lookup table!

deltacoder1020
07-02-2004, 00:10
wouldn't be that useful a sine function if it took near 26ms - remember, you have do do something with it too ;)

Daniel
07-03-2004, 00:07
Real time programming requires the use of a "state machine", which is a programming technique where one state starts the motors, for example, and the second state monitors for a certain amount of time to pass before proceeding to the third state.

The second state can be entered multiple times without causing any problems. So the second state checks the time, then reutrns to the code where the data packets can be handled. Then the same fuction is called to execute the seconds state which looks at the time again.

State machines are best implemented using the C "switch" statement, and some long hard thinking.

Kevin Watson
07-03-2004, 01:47
State machines are best implemented using the C "switch" statement, and some long hard thinking.As Dan pointed out, software state machines are a very powerful concept (I cannot possibly over emphasize this). I wrote a serial transmitter example that uses a state machine to generate the waveforms. The code can be found here (http://kevin.org/frc).

-Kevin