Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
LinkBack Thread Tools Display Modes
Old 31st August 2004, 07:11 PM   (permalink)
Default Difference between Macro and Subroutine?

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?
NewGeek is offline  
Old 31st August 2004, 07:25 PM   (permalink)
Default

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.
John Sorensen is offline  
Old 31st August 2004, 07:41 PM   (permalink)
Default

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?
NewGeek is offline  
Old 31st August 2004, 07:49 PM   (permalink)
Default

It depends on the language you use. His example was a C example. The syntax for MPAssembler is diffirent.
Exo is offline  
Old 31st August 2004, 08:13 PM   (permalink)
Default

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.
John Sorensen is offline  
Reply

Bookmarks

Thread Tools
Display Modes





All times are GMT. The time now is 11:11 AM.


Electronic Circuits  |  Learning Electronics
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.

eXTReMe Tracker