User Tools

Site Tools


pic_asm:pic_push_and_pop_macros_save_the_machine_state

PIC PUSH and POP macros. Save the machine state

Save the machine state - primarily I use these either side of the Interrupt Service Routine (ISR). This is not strictly like these instruction in “proper” CPUs as we have no accessible stack to play with but it takes care of keeping things straight in important places - remember it is only one level deep - if you call PUSH, while PUSHed, you'll lose state and your PIC will behave oddly (unless you mean to do it).

You'll have to arrange the storage registers - try to put them in a globally accessible area of the registers (&h70 on 16f87x) so you can be sure to get to them whichever BANK is currently selected.

Make sure no interrupts happen while you are PUSHing or POPing.


		

;	PUSH/POP save and restore W,PCLATH,STATUS and FSR registers -
;	used on interrupt entry/exit

PUSH	MACRO
	
		MOVWF	SAVED_W			; save w reg
		SWAPF	STATUS,W		; The swapf instruction, unlike the movf, affects NO status bits, which is why it is used here.
		CLRF	STATUS			; sets to BANK0
		MOVWF	SAVED_STATUS	        ; save status reg
		MOVF	PCLATH,W
		MOVWF	SAVED_PCLATH	        ; save pclath
		CLRF	PCLATH
		MOVF	FSR,W
		MOVWF	SAVED_FSR		; save fsr reg
	
	ENDM

POP	MACRO
	
		MOVF	SAVED_FSR,W		; get saved fsr reg
		MOVWF	FSR			; restore
		MOVF	SAVED_PCLATH,W	        ; get saved pclath
		MOVWF	PCLATH			; restore
		SWAPF	SAVED_STATUS,W	        ; get saved status in w
		MOVWF	STATUS			; restore status ( and bank )
		SWAPF	SAVED_W,F		; reload into self to set status bits
		SWAPF	SAVED_W,W		; and restore
	
	ENDM


pic_asm/pic_push_and_pop_macros_save_the_machine_state.txt · Last modified: 2024/01/19 09:40 by 127.0.0.1