Menu

Strange problem with #DEFINE

Gigi
2024-09-23
2024-09-23
  • Gigi

    Gigi - 2024-09-23
    #chip 12F675,4      'Velocit in Mhz oscillatore interno
    #config INTRC_OSC_NOCLKOUT, WDT_OFF, MCLR_Off, BOD_on, PWRT_on
    #option explicit 
    
    #define Freq    70      ' regolazione ritardo VARIA FREQUENZA
    #define Freq1   Freq-10     ' FREQ - 10
    
     Do
    
       wait freq uS
       nop
      Wait 2 s       'time to enjoy the result
    
       wait freq1 uS
    
     Loop
    

    Example to understand problem

    From inaccurate delay error!
    Instead with; #define Freq1 60
    It works perfectly

     
  • Anobium

    Anobium - 2024-09-23

    Interesting. I should trap this issue, and, issue a warning.

    Open up the CDF for the program.

    Search for Freq1, you will find

    CODE/Constant: Line 6 FREQ1 0

    So, the constant is 0.

    Why? #define Freq1 Freq-10 when it should be #define Freq1 = Freq-10 but that does not work either. This needs to be resolved.

    Use a script as a temp fix.

    #script
        FREQ = 70      ' regolazione ritardo VARIA FREQUENZA
        FREQ1 = INT(FREQ-10)
    #endscript
    

    Now I get this in the CDF.

    FINAL/CONSTANT :FREQ   70
    FINAL/CONSTANT :FREQ1  60
    
     
  • Gigi

    Gigi - 2024-09-23

    How do you open CDF ?
    I had searched the ASM file but couldn't find anything

     
    • Anobium

      Anobium - 2024-09-23

      Open the Preferences Application, select the Compiler Tab, select and check 'Create CDF output file', select 'OK' to save.

      The CDF is not created automatically.

       
  • Anobium

    Anobium - 2024-09-23

    This issue is resolved in build 1432 of the compiler.

    The issue was the method Hugh implemented to create the second constant and he assumed that no constant would be used in the creation of subsequent constant.

    • When an = existed: This forced the removal of the = sign and evaluation of the constant string. There was no replace of existing constant(s). So, this created a constant with a string of FREQ-10
    • When an = did not exist: There was no replace of existing constant(s). So, this also created a constant with a string of FREQ-10

    Build 1432

    The preprocessor now properly evaluates the constant string.
    * If =exist, removes the =
    * If contains "+-*\/&" then replace any constants with the existing constant string then evaluate the string,

    Note: The constants are handled in-sequence. The order is strict. A constant must be defined before any calculation can use it.

    This now works, as so do many other mathematical structures.

    #define Freq 70
    #define Freq1 = FREQ - 10
    

    or

    #define Freq 70
    #define Freq1 FREQ - 10
    

    both of the above constructs generates this ASM

    ;wait Freq1 uS
        movlw   20
    

    where wait 60 us would generate

    ;wait 60 uS
        movlw   20
    
     

    Last edit: Anobium 2024-09-23
  • Gigi

    Gigi - 2024-09-23

    OK, perfect thanks for the very quick resolution.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.