| 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
MARCHING ledS - mrdeb Thursday, 11 March 2010 10:48 - [2 replies]
REALISTIC LED candle flicker - mrdeb Wednesday, 10 March 2010 05:48 - [0 replies]
the LCD demo hello world - mrdeb Wednesday, 10 March 2010 05:41 - [29 replies]
USB problem - jon chandler Tuesday, 09 March 2010 11:31 - [3 replies]
Servo Module - andyo Tuesday, 09 March 2010 03:39 - [3 replies]
pic to ps2 communication - roshan Friday, 05 March 2010 01:30 - [7 replies]
Way Off Topic: Worldwide Ignite Week - jon chandler Wednesday, 03 March 2010 20:33 - [4 replies]



If this wasn't a class project that had to be done on a PIC16F690 u...
I am going to send two data from ADC to USART(async mode) to serial...
I was thinking of battery with powered routers or powered for every...
50 rooms - wow. The XBee modules are very easy to use but I'm not ...
Thanks AndyO. What I have to do is design a device to monitor temp...
The circuit used for the remote sensor can be found here: [url]http...
plz i need help do u know from where i could download it or how cou...
hello everyone i'm trying to finish my final project and i want to ...