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.

Interrupts Question

Status
Not open for further replies.
Hello guys, I'm a beginner in the programming pic department but, I'm getting to the point to where interrupts are starting to look more appealing but I have a couple of brief question about them first.

This piece of code is from the pic mcu ref manual and I was wandering why you have to SWPF the status reg when you save them.

Also, could I get a good explanation of what a macro is? Is it basically just a kind of subroutine, and why is there no call?



Code:
PUSH_MACRO MACRO ; This Macro Saves register contents
MOVWF W_TEMP ; Copy W to a Temporary Register
; regardless of current bank
SWAPF STATUS,W ; Swap STATUS nibbles and place
; into W register
MOVWF STATUS_TEMP ; Save STATUS to a Temporary register
; in Bank0
ENDM ; End this Macro
;
POP_MACRO MACRO ; This Macro Restores register contents
SWAPF STATUS_TEMP,W ; Swap original STATUS register value
; into W (restores original bank)
MOVWF STATUS ; Restore STATUS register from
; W register
SWAPF W_TEMP,F ; Swap W_Temp nibbles and return
; value to W_Temp
SWAPF W_TEMP,W ; Swap W_Temp to W to restore original
; W value without affecting STATUS
ENDM ; End this Macro

Thanks for any help you can give.
 
As I understand it a MACRO is just a text substitution thing, so the compiler just adds all the macro text lines any time is sees the macro name, so no need to call the code, as it is embedded automatically by the compiler.
 
But doesn't SWPF move the 7-4 bits to the 3-0 bit spot and vice versa?
and why not just movf and leave out the two swpfs?
 
hotrodhed120 said:
But doesn't SWPF move the 7-4 bits to the 3-0 bit spot and vice versa?
and why not just movf and leave out the two swpfs?

MOVF changes the status bits (actually the Z bit), so can't be used, SWAPF doesn't affect status at all, and if you notice uses the destination of W, so is swapping nibbles and saving it elsewhere.

Check the instructions list in the datasheet, which shows what status bits each instruction affects.
 
OIC! Thanks for point in the right direction! I didn't that realize that some commands had such affects.
So the SWPF acts as a buffer from the MOVF to record it before its actually moved and affected right???
I found the section you were talking and it has/will help alot. With such a large amount of ref. material its kinda hard to know exactly where to go to find the answers or the right explanation and this forum helps alot. Thanks!
 
The macros that you posted above are not good examples as both would only be used once, at the start and end of a interrupt (although they would tidy up the code). Macros are used to save typing and tidy up or make code more readable.

For example, you may be familiar with a processor that uses Branch if Equal, in this case the following macro,
Code:
beq	Macro	k
	btfsc	STATUS,Z
	goto	k
	endm
would enable you to type beq Label instead of the more cumbersome btfsc etc.

As for the swapf instruction, yes, it does swap the nibbles around but as it is used twice then the correct value goes back in Status and W.

Mike.
 
so I could put the interrupt routine in a macro but, i would also have to have different macros for different interrupts, such as a/d int. or tmr0 int., correct? Or could the above Macro apply to all interrupts?
 
hotrodhed120 said:
so I could put the interrupt routine in a macro but, i would also have to have different macros for different interrupts, such as a/d int. or tmr0 int., correct? Or could the above Macro apply to all interrupts?

I don't really see much point in a macro?, a macro is basically a text substition mechanism, so only worth doing if it's used multiple times in the code. There's only one ISR, so not really much point in a macro for it?.
 
Why don't you skip the section on macros and just write some code. Once you have done this, macros will make a lot more sense.

Mike.
 
Pommie said:
Why don't you skip the section on macros and just write some code. Once you have done this, macros will make a lot more sense.

Mike.

I agree, macros will make far more sense once he's used to writing code - to be honest macro's aren't all that useful when you first start - it's basically just a shortcut.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top