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.

Difference between Macro and Subroutine?

Status
Not open for further replies.

NewGeek

New Member
Whats the difference? Are macros only for high-level languages? If no, then how do you use a macro in PIC assembly language. Are these two different names for the same thing?
 
A macro is just short hand. A subrountine is "called."

Subroutines can save memory by reducing repeated code, but take more time because you have to push arguments and return addresses, and push the result and pop back.

A macro is the code literally inserted where you put it. It doesn't save any memory but is as fast as the bit of code you put there, saves typing, and is easier to read.

For example, if I define a macro

#define TOGGLE_PIN1 PORTD.1=0;PORTD.1=1;PORTD.1=0

and put it in my code like so

putsf("I'm going to toggle PIN1 now");
TOGGLE_PIN1;
putsf("PIN1 was toggled");

it would be exactly the same, to the processor, as

putsf("I'm going to toggle PIN1 now");
PORTD.1=0;PORTD.1=1;PORTD.1=0;
putsf("PIN1 was toggled");

j.
 
Ok I understand. Its mostly just to save typing then.

So in your example, the text "#define" is the instruction to make a macro called "Toggle_PIN1" , which contains the instructions "Portd.1=0, ..."

How do you signify the end of the macro?

And then you use "putsf", is this instruction availble to a PIC16Fxxx, which is the only PIC I know. Ive never seen it before.

Why do you use "PUTSF" before and after the macro?

Could you give an example for a PIC16 if the code is different?
 
It depends on the language you use. His example was a C example. The syntax for MPAssembler is diffirent.
 
I don't know nuttin' about no pics :wink: . I was just trying to show the difference between a subroutine (function) and a macro.

My example was kinda in C for an AVR. Pretend it was pseudocode. :wink:

j.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top