PDA

View Full Version : Context saving help in assembly - 16f690



Megahertz
- 28th January 2013, 17:36
Hi, I am trying to write an ISR in assembly, but when simulating, I am having some errors in proteus. Can someone please have a look into this context saving part of my code and advise me if it is OK? I originally have it for 16f676, but trying to adapt it to 16F690. Thanks


' RAM Variables are declared here
wsave var byte $70 SYSTEM ' safe for W in all banks
ssave var byte BANK0 SYSTEM ' save STATUS
psave var byte BANK0 SYSTEM ' save PCLATH
fsave var byte BANK0 SYSTEM ' save FSR

ISR:
asm
movwf wsave ; Save WREG
swapf STATUS, W
clrf STATUS ; Point to bank 0
movwf ssave ; Save STATUS
movf FSR,w
movwf fsave ; save FSR
movf PCLATH, W ; Save PCLATH
movwf psave


; get ready to jump within the ISR page
movlw ((INTHAND) >> 8) ; Set PCLATH for jump
movwf PCLATH
.
.
.
.
isr routine
.
.
.
EndInt ; restore the machine state and return from interrupts
movf fsave,w
movwf FSR ; restore FSR
movf psave,w
movwf PCLATH ; restore PCH
swapf ssave,w
movwf STATUS ; restore Status
swapf wsave,f
swapf wsave,w ; restore WREG
retfie
endasm

Darrel Taylor
- 28th January 2013, 20:06
You should only save context on chips with 2K or less program space.
The 16F690 has 4K.

When there's more than 2K, PBP has to do the context saving before jumping to the ISR.
At that point, the registers have been changed. So if you try to save context again, you're saving the changed values.

You always have to restore context, no matter how much program space there is.

Megahertz
- 28th January 2013, 23:35
Hello Darrel, if I do not have to save it, what variables I will use to restore it or how it is restored. Currently I have the following restoration code-


EndInt ; restore the machine state and return from interrupts
movf fsave,w
movwf FSR ; restore FSR
movf psave,w
movwf PCLATH ; restore PCH
swapf ssave,w
movwf STATUS ; restore Status
swapf wsave,f
swapf wsave,w ; restore WREG
retfie


2) If PBP saves the context then, can I start straightaway by execution of my code assuming I will be in BANK0?
Thanks

Darrel Taylor
- 29th January 2013, 00:27
You restore it the same way when PBP saves it.
PBP uses the wsave, ssave and psave variables that you create.
PBP does not save the FSR register, and you don't need to save FSR unless your ISR uses it.

Yes, it will be in BANK0.

Megahertz
- 31st January 2013, 21:18
Thanks Darrel. It works.