Sign in

username:

password:



Not a member?

Search piclist



Search tips

Subscribe to piclist



piclist by Keywords

12F675 | 16F628 | 16F84 | 16f877 | 16F877A | 16F88 | 18F458 | ADC | AVR | Bootloader | CAN | CCS | CRC | EAGLE | EEPROM | ICD | ICSP | IDE | JDM | LED | Macros | Microchip | MPLAB | PCB-CAD | PIC10F | Pic12f675 | PIC16F84 | PIC16F84A | PIC16F877 | PIC18 | PIC18F452 | PicBasic | PICC | PICSTART | PWM | RS-485 | RS232 | SMT | SPI | UART | USART | USB | Wireless | Wisp628 | Xilinx

Discussion Groups

A discussion group for the PICMicro microcontroller. Also called the Microchip PIC, this list is dedicated to the use and abuse of this fine, simple, microcontroller. Close to topic posts are welcome, ie. general electronics.

macros - Author Unknown - Feb 23 14:35:00 2003


I do all of my PIC software in assembly using MPLAB.  I use a lot of macros in my code writing in order to make the coding simpler and easier to read and troubleshoot.  The basic set of macros that I started with mimic the Parallax programming language that I first used when I started programming PICs.  I have greatly expanded the macro set to add more functions many of which allow for working with 16, 24, and 32 bit functions as if they were simple 8 bit operations.  Here is an example of one I have written.  It is part of a closed loop actuator controller. 

;This macro is for a 16 bit absolute value subtraction.
;The smaller of the two numbers will be subtracted from
;the larger. A bit will be set if the first of the two
;numbers is larger. The bit will be cleared if the second
;of the two numbers is larger. The results of the subtraction
;will be placed in the first set of registers (fr0,fr1) if the
;first number is the higher, The results will be placed in both
;register if the second number is higher.

subabs macro fr0,fr1,fr2,fr3,fr4,bit

local above,label
local abovelo,label
local below,label
local belowlo,label
local equal,label
local out,label

movf     fr2,0
subwf   fr0,0
btfsc _Z
goto equal
above
movf fr2,0
subwf fr0,0
btfss _C
goto below

movf fr3,0
subwf fr1,1
btfsc _C
goto abovelo
decf fr0,1
abovelo
movf fr2,0
subwf fr0,1
bcf fr4,bit
goto out
below
movf fr1,0
subwf fr3,1
btfsc _C
goto belowlo
decf fr2,1
belowlo
movf fr0,0
subwf fr2,1
movf fr3,0
movwf fr1
movf fr2,0
movwf fr0
bsf fr4,bit
goto out
equal
movf fr1,0
subwf fr3,0
btfss _C
goto above
goto below
out
endm

Here is the line of code I actually use

       subabs     cmdhi,cmdlo,poshi,poslo,PORTB,2

I use this to find the difference between the commanded position of the actuator and current position.  The calculated error is then used to find a value from a look up table that is output to a motor controller IC via the PWM output (CCP1).  The direction of rotation input on the IC is output from PORTB.  I have reused this macro in several different designs.

Here is another one that I have used much more extensively.  It will compare the 16 bit value stored in fr, fr-1 to a 16 bit literal number and then jump to an address is the value in the registers is higher than the literal.

cjalwd macro fr1,L1,L2,addr
movf fr1,W
sublw L1
btfss _C
goto addr

movlw L1
subwf fr1,W
btfss _C
goto $+5

movf fr1-1,W
sublw L2
btfss _C
goto addr
endm

Here is an example of this macro that uses the 16 bit value stored in temp_hi,temp_lo, note that the lower byte is not in the line of code but is used anyway.  Care must be taken to insure the lower byte is one address space lower in memory than the higher byte.

       cjalwd       temp_hi,0x01,0xA9,store_temp

If the value in temp_hi,temp_lo is greater than 0x10A9 then the program will jump to the address location labeled  store_temp.

If anyone else has any useful macros I would appreciate seeing them. 

Alan the Rocket Scientist
Reality is for those who can't handle Science Fiction.

http://hometown.aol.com/aseesf4/myhomepage/newsletter.html





(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )

Re: macros - Author Unknown - Feb 23 16:45:00 2003

What Alan has suggested is a great idea.  I am afraid, since I haven't performed PIC or any other kind of programming for about 9 years, I can't contribute anything to the macros other than some general comments.

The use of macros and/or subroutines to perform tasks that will be repeated makes it much easier to write, debug, and maintain code.

Even though it seems easy enough to just turn a bit on and off and on with a  little time delay several places in the program to generate a pulse.  Each time you write the code you run the chance of making another error.

Whereas, if you have a macro and/or subroutine to do it, you will have the sem code everytime.  Once that piece of code is well debugged, it can usually be ignored for the most part in the debugging process.

If your code isn't re-entrant, you may want to use a subroutine to perform many of the tasks that way you save code space.  In assembly language you may want to have a macro sometimes to set up the call to the subroutine to have all your varaibles put in the right registers for your subroutine to process them.

An additional very powerful tool in assembly language with the PIC and with some other processors is the use of tables.  The tables can either be used to take a value and map it to some other value or to determine what routine to run.

Often the use of a table can take a lot of somewhat random looking statements and force them into a very structured manner.  This can greatly simplify the process of debugging and even writing the code.

Hope these comments are of some use and I am sure they would be of more use, if I provided some code for the table construction, but that is not to be today, as I am way over extended on the amount of things to do.

Best Regards,

Larry J. Manson
Englewood, Ohio

http://hometown.aol.com/ljmasdfgh/indx.html




(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )