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.

Absolute value of signed constants inside a macro

Status
Not open for further replies.

atferrari

Well-Known Member
Most Helpful Member
PIC 18F family - Assembler
There is a macro enabling, at compiling time, two other macros based on the sign of these parameters, ranging from 127 to -127
Code:
CONSTANT PARAM0 -3 
CONSTANT PARAM1 124 
CONSTANT PARAM2 -56 
CONSTANT PARAM3 -7 
CONSTANT PARAM4 78 
CONSTANT PARAM5 -6
For that I used the expresion PARAM#v(i) where i is a local variable. So far, so good.

My next (unsolved) problem is to get the absolute value of those constants to be used in further code inside the same macro. The solution has eluded me for the last three days.

Any help appreciated.

(Having the awful feeling that the solution is just in front of me)
 
Something like,

Code:
    if n>127
        n=0-n
    endif
;or
    if n>127
        n=~n
        n=n+1
    endif

The above is pseudo because I've never used the C18 macros.

Mike.
 
Last edited:
Solved

Thanks Mike for your time.

Finally solved as follows:

Code:
ABS_TO_TABLE  MACRO TAPS
              LOCAL INDEX
              LOADREG FSR2L,LOW TABLE_COEFFS
              LOADREG FSR2H,HIGH TABLE_COEFFS
 
              INDEX=0
 
              WHILE INDEX<TAPS           ;INDEX =0 to TAPS-1
 
              IF COEFF#v(INDEX)>=0      ;positive coefficient
                MOVLW COEFF#v(INDEX)    ;absolute value already
              ELSE                      ;negative coefficient
                MOVLW -(COEFF#v(INDEX)) ;we get absolute vale
              ENDIF
 
              MOVWF POSTINC2            ;absolute value goes to table.
 
              INDEX=INDEX+1
              ENDW
 
              ENDM
 
Status
Not open for further replies.

Latest threads

Back
Top