Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC Interrupt: Saving Context in 16F pics

Status
Not open for further replies.
You only need to use swapf when you RESTORE the context.
When saving it you can use MOVF because MOVF first copies the contents of STATUS to W before it alters the status bits.

when you restore contect THE ONLY RIGHT WAY is to use SWAPF.

I don't think this is correct at all. SWAPF swaps the nibbles, so if you use SWAPF only to restore you will not have the correct byte.

EDIT: Better explain swap the nibbles: the low nibble becomes the high, and the high nibble becomes the low.
if register foo = 3F,
swapf foo, f
puts F3 into foo.
swapf foo,w
puts F3 into w and leaves foo unchanged.
 
Last edited:
I don't think this is correct at all. SWAPF swaps the nibbles, so if you use SWAPF only to restore you will not have the correct byte.

EDIT: Better explain swap the nibbles: the low nibble becomes the high, and the high nibble becomes the low.
if register foo = 3F,
swapf foo, f
puts F3 into foo.
swapf foo,w
puts F3 into w and leaves foo unchanged.

I think you mean in the last line

"puts 3F into w and leaves foo unchanged."

.. the whole point being that the content of 'foo' finishes up in W

Cheers

Harry
 
Not quite; the original content of foo was 0x3F, so if the register foo was where STATUS was saved to with a file move instruction - MOVF as Exo claims, and then the SWAP instruction used to restore STATUS, STATUS now contains 0xF3, not 0x3F.

Some days, I'm not very eloquent.:eek:
 
Not quite; the original content of foo was 0x3F, so if the register foo was where STATUS was saved to with a file move instruction - MOVF as Exo claims, and then the SWAP instruction used to restore STATUS, STATUS now contains 0xF3, not 0x3F.

Some days, I'm not very eloquent.:eek:

I haven't looked back through the thread, but that sequence is used to restore the Wreg, so we would use:

(unfortunately the editor removes leading spaces, collapses intermediate sequences of spaces
into a single space, and doesn't allow 'tabs'; so this doesn't look at all as it would in a program)

Note; 'movf' affects the Zero flag in STATUS, whereas 'movwf' and 'swapf' do not.

; Save context:
movwf foo ; Saves Wreg in 'foo'
movf STATUS,w ; Copies STATUS reg unchanged into Wreg ..
; .. since although 'movf' affects the Zero flag in STATUS, this change ..
; .. does not happen until after the move into Wreg is done.
movwf boo ; .. and saves STATUS in 'boo'
;
; ---- Body of subroutine ----
;
; Restore context
movf boo,w ; Original value of STATUS into Wreg ..
movwf STATUS ; .. and into the STATUS reg, does not alter STATUS after move.
; Now we need to restore Wreg without using 'movf' ..
; .. which affects the Zero bit in the STATUS register.
swapf foo,f ; Original value of Wreg 'swapped' still in 'foo' ..
swapf foo,w ; .. and swapped again restoring Wreg original value.

I hope this helps

Best wishes, Harry
 
Yes, Harry, that would work, but you used SWAPF twice to get back the original order. Why not just swap the status reg out, then swap it back in, as Microchip suggests?

What Exo was describing would leave the status reg in a very different state, as he is only swapping once!
 
Yes, Harry, that would work, but you used SWAPF twice to get back the original order. Why not just swap the status reg out, then swap it back in, as Microchip suggests?

What Exo was describing would leave the status reg in a very different state, as he is only swapping once!

Hello again,

Yes you can do that, it's a matter of personal choice whether you use 'swapf' or 'movf where either is possible.

I haven't looked back to see what Exo said, but the point here is that we are dealing with the Wreg not STATUS. The STATUS is relatively easy to deal with, using either 'swapf' or 'movf' -- the real problem is that it can't be done without affecting the Wreg, so that has to be saved first and restored last in a way that does not alter the Zflag in STATUS, before it is saved or after it is restored. You can use 'swapf' or 'movf' in some circumstances. Anyway 'swapf's, if use, are required in pairs, the second to undo the first.

Good luck with it, Harry.
 
Hello again,

Yes you can do that, it's a matter of personal choice whether you use 'swapf' or 'movf where either is possible.

I haven't looked back to see what Exo said, but the point here is that we are dealing with the Wreg not STATUS. The STATUS is relatively easy to deal with, using either 'swapf' or 'movf' -- the real problem is that it can't be done without affecting the Wreg, so that has to be saved first and restored last in a way that does not alter the Zflag in STATUS, before it is saved or after it is restored. You can use 'swapf' or 'movf' in some circumstances. Anyway 'swapf's, if use, are required in pairs, the second to undo the first.

Good luck with it, Harry.

After posting that I have gone back and looked through the thread. There was a lot of confusion there, especially over the use of 'movf'. I would not have considered using it instead of 'swapf' until I thought I had found an error in the book I mentioned. I too had previously read that 'movf' changed the Zflag and naively assumed it would do that if you used it in 'movf STATUS,w'. Then I discovered this old thread, where, in the end L Chung (I hope I spelled that correctly!) pointed out that the change of Zflag happens after the move has been done, not before.

Note, too, that whichever way this is done, 'movf' or 'swapf', it takes the same number of instructions, and the same time to execute. Also there are a lot of variations possible when you start saving and restoring the other registers, you don't have to slavishly follow the suggestions in the Microchip data, as long as you know what you are doing.

Best wishes, Harry
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top