View Single Post
  #3   Spotlight this post!  
Unread 08-02-2008, 11:39
dcbrown dcbrown is offline
Registered User
AKA: Bud
no team
Team Role: Mentor
 
Join Date: Jan 2005
Rookie Year: 2005
Location: Hollis,NH
Posts: 236
dcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud of
Re: Interrupts, Interrupts, and more Interrupts!

How can the autonomous routine loop and loop and loop and never
(apparently) return and still work under MPLAB/WPILIB or EasyC?
----------------------------------------------------------------

I've done something similar before. Again, in the system clock routine check to determine when autonomous mode is ending (I was in autonomous last clock tick, but this time I'm not -- so autonomous period just ended). When you detect this, reset the variable stack managed by the C compiler and reset the hardware stack location (STKPTR) to point to the return to main().

For example, in the standard IFI code if:
_entry (reset) jumps to _startup
_startup then calls main()
main() then calls Autonomous()

Code:
main()
{
    Autonomous();
    Operator();
}
Autonomous()
{
     while(1)
     {
         // user code here...
     }
}
The call stack will always look like:

Code:
 0000
 0001  _startup+n  <- main's return will pop and use this
 0002  _main+n     <- autonomous' return will pop and use this
Slamming the stack pointer and frame pointer managed by the C compiler back to their defaults and then changing the STKPTR index to 2 and then returning normally from the interrupt service routine will make it appear as if the Autonomous() routine just returned to main() on its own. Instead, the interrupt service routine hijacked the stack and did it for us...

Yeah, we could do the same thing between iterations through Autonomous(), but this way again we don't really care how long the code path through autonomous is, it will get yanked immediately into the Operator() code when autonomous ends.