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.

eblc1388

Active Member
On entering ISR, one needs to save the context of the PIC environment.

Saving W is easy using MOVWF instruction.

Its a bit tricky when saving the STATUS register. There are two choices. The SWAP instruction definitely works. The other alternative is to use the MOVF instruction, which could affect STATUS bits.

The question is: When one does a MOVF STATUS,W instruction, would W contains the original copy or the changed copy?
 
eblc1388 said:
On entering ISR, one needs to save the context of the PIC environment.

Saving W is easy using MOVWF instruction.

Its a bit tricky when saving the STATUS register. There are two choices. The SWAP instruction definitely works. The other alternative is to use the MOVF instruction, which could affect STATUS bits.

The question is: When one does a MOVF STATUS,W instruction, would W contains the original copy or the changed copy?

Does it matter?, just use the SWAP instruction as specified by MicroChip.
 
Nigel Goodwin said:
Does it matter?, just use the SWAP instruction as specified by MicroChip.

Yes it does matter. I found some program authors were using SWAP while others use the MOVF.

I don't want to randomly change other people's codings unless I absolutely have to.
 
eblc1388 said:
Nigel Goodwin said:
Does it matter?, just use the SWAP instruction as specified by MicroChip.

Yes it does matter. I found some program authors were using SWAP while others use the MOVF.

I don't want to randomly change other people's codings unless I absolutely have to.

Correcting mistakes doesn't come under the heading of "random changes", at least not in my opinion?. MicroChip clearly state to use SWAP to save and restore the STATUS register, and NOT to use MOVF as it can alter the STATUS bits.
 
Yes, Microchip vs Microchip, again.
 

Attachments

  • int_save.gif
    int_save.gif
    22.6 KB · Views: 1,238
eblc1388 said:
Yes, Microchip vs Microchip, again.

From the file f877atemp.asm shipped with MPLAB 7.20 :

Code:
	movwf   w_temp            ; save off current W register contents
	movf	STATUS,w          ; move status register into W register
	movwf	status_temp       ; save off contents of STATUS register
	movf	PCLATH,w	  ; move pclath register into w register
	movwf	pclath_temp	  ; save off contents of PCLATH register

; isr code can go here or be located as a call subroutine elsewhere

	movf	pclath_temp,w	  ; retrieve copy of PCLATH register
	movwf	PCLATH		  ; restore pre-isr PCLATH register contents
	movf    status_temp,w     ; retrieve copy of STATUS register
	movwf	STATUS            ; restore pre-isr STATUS register contents
	swapf   w_temp,f
	swapf   w_temp,w          ; restore pre-isr W register contents
	retfie                    ; return from interrupt

!!!
 
eblc1388 said:
Yes, Microchip vs Microchip, again.

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.
 
Exo said:
when you restore contect THE ONLY RIGHT WAY is to use SWAPF.

And to add to that:

While MOVWF can be used for restoring other registers, the ONLY right way to restore W is to use SWAPF.

I searched the Net and found a comparsion between using MOVF and SWAPF in saving/restore STATUS. Both versions use the same number of instructions and are equally effective. Can't find any solid example that said MOVF can't be used.

But as Microchip recommended the SWAPF method, I'll use it instead.

[edited]: Corrected spelling mistake.
 
Can't find any solid example that said MOVF can't be used.

In fact you need a solid reason, given already in previous posts: MOVF changes the STATUS register contents :!:
 
atferrari said:
In fact you need a solid reason, given already in previous posts: MOVF changes the STATUS register contents :!:

Yes you're right. I don't know for sure when exactly bit changes would happen in the MOVF instruction so the best way is to use the SWAPF which does not have anything for me to worry about.
 
If you read the instruction set, MOVF changes the Z flag of the STATUS register. That is why SWAPF is recommended as none of the STATUS register flags are affected!
 
checkmate said:
If you read the instruction set, MOVF changes the Z flag of the STATUS register. That is why SWAPF is recommended as none of the STATUS register flags are affected!

The correct answer to the wrong question. :(

eblc1388 said:
The question is: When one does a MOVF STATUS,W instruction, would W contains the original copy or the changed copy?
What's your answer to the above?

Edited: I checked the datasheet of PIC and can now confirm that the copying of the STATUS contents into the ALU happens during Q2 and the writing of ALU contents into W & changing of STATUS flags both happens in Q4. Therefore the contents inside W will definitely be the original one.
 
A gotcha to watch out for when saving status in interrupts is the FSR register. Microchip doesn't tell you to save this anywhere, but if you use it in your interrupt and your main code then you'd better save it.

Mike.
 
Pommie said:
A gotcha to watch out for when saving status in interrupts is the FSR register. Microchip doesn't tell you to save this anywhere, but if you use it in your interrupt and your main code then you'd better save it.

Well it's pretty obvious!, anything you use that could affect the main program must be saved - STATUS is the only one that needs mentioning really, because it can (and will!) be affected without directly changing it.
 
Nigel Goodwin said:
Well it's pretty obvious!, anything you use that could affect the main program must be saved - STATUS is the only one that needs mentioning really, because it can (and will!) be affected without directly changing it.

Yes, it is pretty obvious, but I wonder how many people got caught out by not saving PCLATH and got random crashes when they started using the second page. As you use more addressing modes then you need to save more location type counters. PCLATH is mentioned by microchip - FSR isn't.

Mike.
 
eblc1388 said:
Exo said:
when you restore contect THE ONLY RIGHT WAY is to use SWAPF.

And to add to that:

While MOVWF can be used for restoring other registers, the ONLY right way to restore W is to use SWAPF.

I searched the Net and found a comparsion between using MOVF and SWAPF in saving/restore STATUS. Both versions use the same number of instructions and are equally effective. Can't find any solid example that said MOVF can't be used.

But as Microchip recommended the SWAPF method, I'll use it instead.

[edited]: Corrected spelling mistake.

It isnt that hard to understand is it?
To STORE you can use MOVF.
MOVF STATUS, W will copy status to W, intact, and then may modify the Z flag but that doesnt matter, we have a good copy in W.

But to RESTORE you must use SWAPF
if you use MOVF STATUS_TEMP, W then the value of STATUS_TEMP will be copied to W, thats ok. But then what? Any instruction you use -except for SWAPF- to get W copied into STATUS will first copy W to STATUS and THEN change the Z flag.
 
Exo said:
But to RESTORE you must use SWAPF

Not necesarily. There are alternatives.

Exo said:
if you use MOVF STATUS_TEMP, W then the value of STATUS_TEMP will be copied to W, thats ok. But then what? Any instruction you use -except for SWAPF- to get W copied into STATUS will first copy W to STATUS and THEN change the Z flag.

Check the MOVWF instruction, especially which flag it will or will not change.
 
I don't really see the point in which we are debating over? Is this really a debatable issue or is it a matter of nitpicking?
 
checkmate said:
I don't really see the point in which we are debating over? Is this really a debatable issue or is it a matter of nitpicking?

How would I know? I can't possibily expect everybody to understand the issues.

Instead of giving your comment on the original question, you simply suggested one to read the instruction set. I did and had responsed to that.
 
Thank you L Chung for your description of movf and the STATUS register. I recently bought the book 'PIC Microcontrollers' from MikroElectronika, an excellent book although its quirky language and style would make it difficult for a beginner. I was puzzled by his use of movf in the interrupt 'save context' area, as I had always used swapf without question. I realised that it came down to whether or not the Z flag is changed before the move. You have very nicely cleared it up. and it is not an error.

Harry Weston (radio ham, call sign M0SOP)
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top