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.

Context Saving During Interrupt

Status
Not open for further replies.
Hey everyone.

I've just been looking through microchips PIC Assembly templates and have a question regarding the code the use for context saving.

Code:
        ORG     0x000                   ; Processor reset vector go to beginning of program
        GOTO    SETUP                   ; 

        ORG     0x004                   ; Interrupt vector location. When an Interupt occurs, the program jumps here
        MOVWF   W_TEMP                  ; Save current W register contents
        MOVF    STATUS,W                ; Move STATUS register into W register
        MOVWF   STATUS_TEMP             ; Save contents of STATUS register in temporary register

; Interrupt Service Routine goes here

        MOVF    STATUS_TEMP,W           ; Retrieve saved status 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

Why are there 2 swapf instructions together?
Isn't the objective to load the W register to its pre-isr state?
So wouldn't a simple MOVF W_TEMP,W do the same job?
Why are the swapf instructions used?
 
Why are there 2 swapf instructions together?
Isn't the objective to load the W register to its pre-isr state?
So wouldn't a simple MOVF W_TEMP,W do the same job?
Why are the swapf instructions used?

Before exiting the ISR, you restore the STATUS register, that was previously saved. At that point you must not change the STATUS register anymore inside the ISR.
The swapf instruction is then used to restore the W register because it does not affect the STATUS register.
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top