• AVR Freaks

MACROS and calculated decimal values?

Author
AndersG
Super Member
  • Total Posts : 208
  • Reward points : 0
  • Joined: 2008/08/05 04:51:24
  • Location: 0
  • Status: offline
2008/10/30 01:53:03 (permalink)
0

MACROS and calculated decimal values?


;**********************************************************************
; Helper macro for 32-bit numbers. Puts byte number 'byte' of a 32-bit literal 'var'
; into register 'reg'
;
movbyte macro reg,var,byte
movlw (var >>(D'08' * byte) & H'FF')
movwf reg
endm


Works fine, as long as var is a simple number, if I try something like:

#define AFACTOR ((.4096 * .1000)/(GVFC * RSENSE * .3600))

Then I get an error:

00799
00800 MOVBYTE REGB0,AFACTOR,0
Error[112] : Missing operator
0138 3000 M MOVLW (((.4096 * .1000)/(.22.2 * .0.01 * .3600)) >>(D'08' * 0) & H'FF')
0139 00?? M MOVWF REGB0


Is there any way to get around that?
#1

13 Replies Related Threads

    BitWise
    Super Member
    • Total Posts : 1238
    • Reward points : 0
    • Joined: 2004/11/09 13:24:20
    • Location: UK
    • Status: offline
    RE: MACROS and calculated decimal values? 2008/10/30 02:14:09 (permalink)
    0
    GVFC and RSENSE expand into values with two decimal points (.22.2 * .0.01 * .3600). The assembler only works with integer expressions, this is throwing it.

    Throughout your life advance daily, becoming more skillful than yesterday, more skillful than today. This is never-ending.

    Yamamoto Tsunetomo (1659-1719)
    #2
    AndersG
    Super Member
    • Total Posts : 208
    • Reward points : 0
    • Joined: 2008/08/05 04:51:24
    • Location: 0
    • Status: offline
    RE: MACROS and calculated decimal values? 2008/10/30 03:00:30 (permalink)
    0
    OK. Thanks, I just have to do the maths by hand then ;)
    #3
    Olin Lathrop
    Super Member
    • Total Posts : 7463
    • Reward points : 0
    • Joined: 2004/02/26 17:59:01
    • Location: Littleton Massachusetts
    • Status: offline
    RE: MACROS and calculated decimal values? 2008/10/30 05:22:55 (permalink)
    0
    OK. Thanks, I just have to do the maths by hand then

    Or use my MPASM preprocessor, PREPIC.  It is part of my PIC development environment, which can be downloaded from http://www.embedinc.com/pic/dload.htm.  PREPIC can do floating point calculations.  You can then convert to integer or fixed point as the last step to make MPASM constants.
    #4
    MBedder
    Circuit breaker
    • Total Posts : 6919
    • Reward points : 0
    • Joined: 2008/05/30 11:24:01
    • Location: Zelenograd, Russia
    • Status: offline
    RE: MACROS and calculated decimal values? 2008/10/30 05:39:33 (permalink)
    0
    ORIGINAL: AndersG

    OK. Thanks, I just have to do the maths by hand then ;)
    Don't. Just use a conventional decimal format - 0.1234 instead of .1234, and set default radix as decimal (radix dec directive) to get rid of the leading '.' ambiguity
    #5
    AndersG
    Super Member
    • Total Posts : 208
    • Reward points : 0
    • Joined: 2008/08/05 04:51:24
    • Location: 0
    • Status: offline
    RE: MACROS and calculated decimal values? 2008/10/30 08:30:29 (permalink)
    0
    Don't. Just use a conventional decimal format - 0.1234 instead of .1234, and set default radix as decimal (radix dec directive) to get rid of the leading '.' ambiguity


    Nope, does not work.


    radix dec
    #define RSENSE 0.01 ; Sense resistor value in ohms
    #define GVFC 22.2 ; VFC gain factor in Hz/V

    #define AFACTOR ((4096 * 1000)/(GVFC * RSENSE * 3600)) ; Multiply Charge/DeltaT by this to get mA



    Produces the same error
    #6
    DarioG
    Allmächtig.
    • Total Posts : 54081
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: Oesterreich
    • Status: offline
    RE: MACROS and calculated decimal values? 2008/10/30 12:48:11 (permalink)
    0
    Hmm, strange, I always do as MBedder suggests.

    GENOVA :D :D ! GODO
    #7
    Olin Lathrop
    Super Member
    • Total Posts : 7463
    • Reward points : 0
    • Joined: 2004/02/26 17:59:01
    • Location: Littleton Massachusetts
    • Status: offline
    RE: MACROS and calculated decimal values? 2008/10/30 17:12:25 (permalink)
    0
    Just use a conventional decimal format - 0.1234 instead of .1234, and set default radix as decimal (radix dec directive) to get rid of the leading '.' ambiguity

    While ditching the very misleading "." notation for decimal is a good idea, MPASM doesn't have floating point or fractional number handling built in.  You can't say "0.1234" no matter what the radix.  MPASM only handles integers and can only do integer math.
    #8
    Olin Lathrop
    Super Member
    • Total Posts : 7463
    • Reward points : 0
    • Joined: 2004/02/26 17:59:01
    • Location: Littleton Massachusetts
    • Status: offline
    RE: MACROS and calculated decimal values? 2008/10/30 17:24:02 (permalink)
    0
    radix dec
    #define RSENSE 0.01 ; Sense resistor value in ohms
    #define GVFC 22.2 ; VFC gain factor in Hz/V

    #define AFACTOR ((4096 * 1000)/(GVFC * RSENSE * 3600)) ; Multiply Charge/DeltaT by this to get mA
    Produces the same error

    Of course, just as one would expect from reading the manual.
     
    Once again, this is one reason I created my preprocessor:
    /const   rsense  real = 0.01 ;sense resistor value in ohms
    /const   gvfc    real = 22.2 ;VFC gain in Hz/V

    /const   afactor real = [/ [* 4096 1000] [* gvfc rsense 3600]]
    /show "afactor = " afactor

    afactor  equ     [rnd afactor] ;round to nearest int to make MPASM constant
    Running this thru PREPIC yields:
    afactor = 5125.125
    Which means the MPASM constant AFACTOR got set to 5125.
    #9
    DarioG
    Allmächtig.
    • Total Posts : 54081
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: Oesterreich
    • Status: offline
    RE: MACROS and calculated decimal values? 2008/10/31 02:46:45 (permalink)
    0
    Hi Olin, just a related question:

    since porting to Relocatable Mode one old project, I found that my macros


    EXTENDED_CALL MACRO x                      ; "lcall" fa relativamente schifo...
        errorlevel -306
        #if M_CUR_BANK != high(x)        ; non va...
            PAGESEL x
        #endif
        call x
    M_CUR_BANK SET high(x) >> 3            ; non va... + con relocatable...
        errorlevel +306
        ENDM

    MY_PAGESEL MACRO x
        #if M_CUR_BANK != ( high(x) >> 3)
            pagesel x
        #endif
    M_CUR_BANK SET high(x)
        ENDM


    don't work anymore (I wrote about this in another thread
    http://forum.microchip.com/tm.aspx?m=378093
    )

    I wonder how did you make your own to create a "smart PAGESEL" with tracks the current PCLATH.

    I searched for your solution at your website, but could not (easily) find it: can you give me a hint here or shall I have to go downloading your whole environment? grin

    thank you!

    GENOVA :D :D ! GODO
    #10
    Olin Lathrop
    Super Member
    • Total Posts : 7463
    • Reward points : 0
    • Joined: 2004/02/26 17:59:01
    • Location: Littleton Massachusetts
    • Status: offline
    RE: MACROS and calculated decimal values? 2008/10/31 04:50:32 (permalink)
    0
    EXTENDED_CALL MACRO x                      ; "lcall" fa relativamente schifo...
        errorlevel -306
        #if M_CUR_BANK != high(x)        ; non va...
            PAGESEL x
        #endif
        call x
    M_CUR_BANK SET high(x) >> 3            ; non va... + con relocatable...
        errorlevel +306
        ENDM
    don't work anymore

    Right.  That is because the target address is not known at assembly time.  I assume that all calls to other modules could be on other pages.  My GCALL (Global CALL) macro sets PCLATH before the call and restores it to the current page after.  It doesn't know that the target is on the same page when it is, but it does know how many pages the PIC has and the PCLATH manipulations are omitted on PICs with only one page.
     
    I wonder how did you make your own to create a "smart PAGESEL" with tracks the current PCLATH.

    I didn't.
     
    I searched for your solution at your website, but could not (easily) find it: can you give me a hint here or shall I have to go downloading your whole environment? grin

    Downloading the whole environment is the safe thing to do, but all my general utility macros are in STD.INS.ASPIC.  GCALL and MCALL are the two for calling global and in-module subroutines, respectively.
    #11
    DarioG
    Allmächtig.
    • Total Posts : 54081
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: Oesterreich
    • Status: offline
    RE: MACROS and calculated decimal values? 2008/10/31 10:02:46 (permalink)
    0
    Ok, thank you Olin.

    So you did not do that: do you believe it might work? And, more important, that there is a way to get it working in Relocatable mode?

    I'm writing "it may" because I wrote it (in Absolute mode), I checked it to some extent, then I could not completely test that on a 16F648 since I kept on working on the 628 and so...


    GENOVA :D :D ! GODO
    #12
    Olin Lathrop
    Super Member
    • Total Posts : 7463
    • Reward points : 0
    • Joined: 2004/02/26 17:59:01
    • Location: Littleton Massachusetts
    • Status: offline
    RE: MACROS and calculated decimal values? 2008/11/01 06:19:33 (permalink)
    0
    So you did not do that: do you believe it might work? And, more important, that there is a way to get it working in Relocatable mode?

    Most of the time you're not going to care whether there is PCLATH manipulation to and from a subroutine.  For the rare cases when I need the extra few cycles calling between modules, I force the two modules onto a specific page.  In the linker file I set up ".PAGEx" sections for each page, then used those section names with the CODE directive to force the code to the particular page.  That's still a lot better than manually allocating all routines to pages.  If you force just a few, there is probably enough room for the linker to fit the remaining routines on that page and the other pages.
     
    I'm writing "it may" because I wrote it (in Absolute mode), I checked it to some extent, then I could not completely test that on a 16F648 since I kept on working on the 628 and so...

    Why can't you just replace all external calls with my GCALL macro, or something similar?  First get it working and make sure there aren't other problems.  Then you can optimize by forcing two modules onto the same page if you really need to.
    #13
    DarioG
    Allmächtig.
    • Total Posts : 54081
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: Oesterreich
    • Status: offline
    RE: MACROS and calculated decimal values? 2008/11/01 19:06:03 (permalink)
    0
    ORIGINAL: Olin Lathrop

    Most of the time you're not going to care whether there is PCLATH manipulation to and from a subroutine.  For the rare cases when I need the extra few cycles calling between modules, I force the two modules onto a specific page.


    Actually the need for that did arise when trying to squeeze a lot of code into a 16F628, before 16F648 came out and moreover before I could realize that PAGESEL won't output any code if used on a part with less than 2Ksad


      In the linker file I set up ".PAGEx" sections for each page, then used those section names with the CODE directive to force the code to the particular page.  That's still a lot better than manually allocating all routines to pages.  If you force just a few, there is probably enough room for the linker to fit the remaining routines on that page and the other pages.


    Got it, yep. I also was not aware of the capabilities of the Linker, and relocatable code (that pwoject had been developed in absolute mode).

    Why can't you just replace all external calls with my GCALL macro, or something similar? 


    I simply did not know you at the time I wrote my macros Smile
    I'll give them a look.

    thanks Olin

    GENOVA :D :D ! GODO
    #14
    Jump to:
    © 2020 APG vNext Commercial Version 4.5