View Single Post
  #1   Spotlight this post!  
Unread 02-02-2004, 19:50
SeanCassidy's Avatar
SeanCassidy SeanCassidy is offline
Antiregistered User
#0263 (Aftershock)
Team Role: Programmer
 
Join Date: Oct 2003
Location: Holtsville, NY
Posts: 37
SeanCassidy is an unknown quantity at this point
Interrupt timer, executing code asap?

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:

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.