Searching \ for 'MACROS' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/index.htm?key=macros
Search entire site for: 'MACROS'.

Truncated match.
PICList Thread
'macros'
1998\05\07@152322 by Dwayne Reid

flavicon
face
G'day piclisters

I have just discovered a couple of simple macros that I'd like comments on.
But first the problem.

I use #define to define my port pins because I don't want to have to
remember which port they belong to (plus it is easier!).

example:
   #define SDATA   RB,5

This is well and good.  The problem arises when I want to manipulate a bit
mask based on that pin.  The wonderful expression someone came up with:

   iorlw       b'00000001' << pin

requires the form of port,pin to work.  So, for all these years, I've been
mixing the two styles in my code and avoiding port pin bit masks whenever
possible.

It finally occured to me today that a #define statement already has the
information needed to make a bit mask.  All it takes is a couple of one line
macros as follow:

sethi           MACRO   port,pin
   iorlw       b'00000001' << pin
   endm

setlo           MACRO   port,pin
   andlw       ~ (b'00000001' << pin)
   endm

Usage is simple:  way up near the front of my source:

   #define     POT     GPIO,5
   (rest of pin definitions)
   DDR         EQU     b'00110000'     ;default port pin DDR

down somewhere in the code:
;generate bit mask for RC timer pin
   movlw       DDR             ;set pin to o/p
   setlo       POT
   tris        GPIO
       .
       .
       .
   movlw       DDR             ;set pin back to input
   sethi       POT
   trisi       GPIO

The nice thing about this is that it doesn't matter what the DDR constant
gets changed to - I explicitly set the tris bit for that pin.

Comments anyone?  I'm not particularily happy with my chosen macro names
(sethi and setlo) but thats what I came up with off the top of my head.
I've tested this with the DOS version 1.40 of MPASM - it generated the same
opcodes as explicit andlw and iorlw statements.

dwayne


Dwayne Reid   <spam_OUTdwaynerTakeThisOuTspamplanet.eon.net>
Trinity Electronics Systems Ltd    Edmonton, Alberta, CANADA
(403) 489-3199 voice     (403) 487-6397 fax


'MACROS'
2000\03\28@105728 by John Orhan
flavicon
face
Dear PICsters,
I have a question for someone who can tell me about macros. How many times
can a macro be called. Can a macro be called from inside another macro? What
is the difference between a macro and a subroutine? If I happen to call a
macro from outside the page limits, what will happen?


                                               Please advise

Regards John

2000\03\28@123002 by Andrew Warren

face
flavicon
face
John Orhan <.....PICLISTKILLspamspam@spam@MITVMA.MIT.EDU> wrote:

> How many times can a macro be called.

   As many times as you like.

> Can a macro be called from inside another macro?

   Yes.

> What is the difference between a macro and a subroutine?

   A subroutine exists at just one place in your PIC's memory; each
   time you want to use it, you CALL the subroutine from somewhere
   else in your program.

   When you define a macro, NOTHING gets stored in your PIC's
   memory.  When you USE the macro, though, a full copy of it is
   inserted in your program at the point where you used it.

   An analogy might help... If you're composing a document with
   text and pictures, you can refer to the pictures two ways:

   The first way is to have just one copy of the picture, with "See
   Figure 1" in your text wherever you want the reader to look at
   the picture.  That's a subroutine.

   The other way is to paste a copy of the picture at each point in
   the document where you want the reader to see it.  That's a
   macro.

   Make sense?

> If I happen to call a macro from outside the page limits, what will
> happen?

   I'm not sure what you're asking.  You don't "call" macros; when
   you use them, you're just asking the assembler to insert the
   body of your macro there in your program... So they don't behave
   any differently than if you simply cut-and-pasted code into your
   program at that point.

   -Andy


=== Andrew Warren - fastfwdspamKILLspamix.netcom.com
=== Fast Forward Engineering - San Diego, California
=== http://www.geocities.com/SiliconValley/2499

2000\03\28@153918 by l.allen

picon face
John Wrote...

> Dear PICsters,
> I have a question for someone who can tell me about macros. How many times
> can a macro be called. Can a macro be called from inside another macro? What
> is the difference between a macro and a subroutine? If I happen to call a
> macro from outside the page limits, what will happen?
>
>
>                                                 Please advise
>
> Regards John

A Macro is not a PIC command. Its a label for people
writing code.
In the assembler the Macro is some lines of PIC
commands that are given a single name to REPRESENT
those lines.
When the .asm file is assembled those Macro words are
substituted for the lines you originally wrote.
The advantage of this is that you dont have to keep
writing the same few lines over and over again and it can
make more sense to have a descriptive name like
GET_A2D.
A subroutine is different in that the name(label) of the
subroutine is a pointer to the address of where the first
byte of the subroutine is stored in the program memory.


_____________________________

Lance Allen
Technical Officer
Uni of Auckland
Psych Dept
New Zealand

http://www.psych.auckland.ac.nz

_____________________________

2000\03\28@171405 by John Orhan

flavicon
face
Hi Andy,

And many thanks for your help. If I tried to invoke a macro on a PIC from
say ram_bank0  to a place in ram_bank1 memory, ( ram_bank0 and ram_bank1
being memory pages) would the macro work?? Or would I have to switch memory
pages first?
I may very well be using the wrong terminology and am new to ram/ memory
paging systems like on the 16c505 or 16c63.

                                                       Regards John

               {Original Message removed}

2000\03\29@081403 by Daniel Hart

flavicon
face
John,
A small addition to Andy's excellent description:
Subroutines change control flow, MACROs do not. A return cannot be used to exit a MACRO. MACROs
don't modify the stack.
Dan

Andrew Warren wrote:

{Quote hidden}

--

Daniel Hart
Embedded System Design Engineer
NBS Technologies, Inc. (Card Technology Corp.)
70 Eisenhower Drive, Paramus, NJ 07652, USA
+1 201 845 7373 x183    dhartspamspam_OUTnbstech.com

2000\03\29@104505 by jamesnewton

face picon face
A good example of the use of macros for control structures is  Karl Lunts
PIC Macros (FOR...NEXT, CASE, REPEAT, POLL, WAIT, etc..)
http://www.seanet.com/~karllunt/picmacro.htm

And other conditional / program flow code at
http://www.piclist.com/../microchip/routines

---
James Newton (PICList Admin #3)
@spam@jamesnewtonKILLspamspampiclist.com 1-619-652-0593
PIC/PICList FAQ: http://www.piclist.com or .org


{Original Message removed}

2000\03\31@005307 by Quentin

flavicon
face
John Orhan wrote:
>
> Hi Andy,
>
> And many thanks for your help. If I tried to invoke a macro on a PIC from
> say ram_bank0  to a place in ram_bank1 memory, ( ram_bank0 and ram_bank1
> being memory pages) would the macro work?? Or would I have to switch memory
> pages first?
Another explanation to supplement the others:
Do not think of a macro as the same way you store a subroutine. A macro
initially just sits in your assembler program and is not stored in your
PIC memory. When you use a macro in your program, the assembler will
replace the macro label with the lines of code that is in the macro
itself.

Simple example:

You write a macro:
CNSTM   MACRO
       MOVLW   0X020
       MOVWF   CONSTANT
       ENDM
This stays in your assembler editor.
Later in your program:
       .
       .
       CNSTM           ;ask for macro
       MOVLW   0X039
       SUBWF   CONSTANT,W
       .
       .
When you assemble, the assembler will take the lines from your macro,
and insert it where the macro label is:
       .
       MOVLW   0X020
       MOVWF   CONSTANT
       MOVLW   0X039
       SUBWF   CONSTANT,W
       .
       .
You don't see it (you only see the label) but this is the way it will be
stored in your PIC memory.

Hope this helps
Quentin

2000\03\31@010844 by John Orhan

flavicon
face
Hi Quentin,
Many thanks for your detailed explanation. Yes indeed it does help.

                                               John

               {Original Message removed}

More... (looser matching)
- Last day of these posts
- In 2000 , 2001 only
- Today
- New search...