| Swordfish Code Snippet - Interrupt Context Saving |
|
|
| Saturday, 04 April 2009 10:26 | |
|
Context Saving and Restoring is vital for interrupts, as the program could get pulled from anywhere at any time. What this means is that the system variables/registers are backed up at the start of the Interrupt Handler, and then when the Interrupt is complete, they are all restored again.
This can be a little memory demanding, so you only want to back up what you need to. Consider this, your Interrupt uses a function that is also used during your Main Program loop. What happens if the interrupt occurs during the function when it was called from the main program loop? Well without context saving, the program would become unstable and unreliable. Consider;
Dim Counter As Word // get value function... Function GetValue(pValue As Byte) As LongWord Result = pValue * pValue * pValue End Function // some interrupt... Interrupt MyInt() Dim Value As LongWord Value = GetValue(10) End Interrupt . . . // part of main program Loop... Enable(MyInt) While True If PORTB.0 = 1 Then Counter = GetValue EndIf Wend
Well, as you can see, this could lead to serious problems. We have to back up system registers and any variables used within the Function 'GetValue'. How? It's easy with Swordfish!
Save(0) will back up all of the system variables default to Swordfish. To back up shared functions/procedure variables, simply add them to the Context Save. ie, Save(0, GetValue). Now when the interrupt occurs, everything will be backed up that is in use within the ISR. And when finished, everything is restored with the commands Restore.
Dim Counter As Word // get value function... Function GetValue(pValue As Byte) As LongWord Result = pValue * pValue * pValue End Function // some interrupt... Interrupt MyInt() Dim Value As LongWord Save(0, GetValue) Value = GetValue(10) Restore End Interrupt . . . // part of main program Loop... Enable(MyInt) While True If PORTB.0 = 1 Then Counter = GetValue(10) EndIf Wend
Search for "swordfish interrupt" here at digital-diy.com, or check the Swordfish help file for more information regarding interrupts. |
|
| Last Updated ( Tuesday, 14 April 2009 10:08 ) |
Whos Online
- Jon Chandler
- MrDEB
Forum Activity
LCD / 18F1320 - mrdeb Friday, 19 March 2010 07:19 - [0 replies]
A 'throw-away" PIC board - mrdeb Thursday, 18 March 2010 06:19 - [32 replies]
Credit Where Credit's Due - jon chandler Tuesday, 16 March 2010 12:01 - [9 replies]
Marching LEDs - mrdeb Saturday, 13 March 2010 22:54 - [7 replies]
USB 8 Channel Servo Controller - andyo Saturday, 13 March 2010 01:19 - [2 replies]
Dedicated Servo Controller - graham Friday, 12 March 2010 17:58 - [0 replies]
Must have....delta temperature glowies! - graham Friday, 12 March 2010 17:31 - [3 replies]



Graham, Thanks for fixing the spacing on the array. This is how i...
Thanks Graham, It is a problem with Flowcode because the demo wa...
Looks like you've pretty much solved every minor issue that was enc...
The low-cost servo does have one other feature. The origina...
I am not familiar with flowcode, though did you try powering the bo...
Hello there, I am still trying to get the LCD to work but I am pro...
Hi Hop, I've finally got some time this weekend to delve into the ...
Very nice - 900,000 packets and 0 errors - can't ask for better tha...