• AVR Freaks

Hot!#if (macroDef == value) not working

Author
The Bean
New Member
  • Total Posts : 21
  • Reward points : 0
  • Joined: 2020/03/03 05:26:03
  • Location: 0
  • Status: offline
2020/05/09 04:12:52 (permalink)
0

#if (macroDef == value) not working

I want to run the same program on 2 different chips. I set up a header file to define the chip and its associated parameters (clock settings, number of pins, etc.) as follows:
 
Sample (actual code is attached):
#define _Device PIC18F45K50
#if _Device == PIC18F2455
#define pins 28
...
#elif _Device == PIC18F45K50
#define pins 40
...
#endif
 
I do this several times throughout the code. 
In all cases, the first option is selected (e.g. #define pins 28) as it's highlighted and the second is not (#define pins 40) as it's gray'ed out. Compiling the code proves the selection is wrong (multiple errors for undefined macros, etc.). Note that I tried using paren's "#if (_Device == PIC18F45K50)" with the same result. Switching the order of the tests still always selects the first option. I've changed the name of the macro ("_Device") to more obscure names to ensure there isn't a duplicate, but get the same results. 
 
Environment:  MPLAB X IDE v5.35
Compiler:        XC8 v2.10
Chips:             PIC18F2455 and PIC18F45K50
Debugger:       ICD4
Any Advice?
#include "pic18f45k50.h"
#define _Device PIC18F45K50
    
// In this test, the "pins 28" is highlighted
#if _Device == PIC18F2455
# define pins 28
#elif _Device == PIC18F45K50
# define pins 40
#endif
    
// In this test, the "pins 40" is highlighted
#if _Device == PIC18F45K50
# define pins 40
#elif _Device == PIC18F2455
# define pins 28
#endif

#if _Device == PIC18F2455
    // OSCCON bit definitions
#define _OSC_External 0x00
#define _OSC_Tmr1 0x01
#define _OSC_Internal 0x02
    
#define _OSC_Int_31khz 0x00
#define _OSC_int_125khz 0x10
#define _OSC_int_250khz 0x20
#define _OSC_int_500Khz 0x30
#define _OSC_int_1Mhz 0x40
#define _OSC_int_2Mhz 0x50
#define _OSC_int_4Mhz 0x60
#define _OSC_int_8Mhz 0x70

#define _OSC_SleepEnable 0x00
#define _OSC_IdleEnable 0x80
    
#elif _Device == PIC18F45K50
#define _OSC_External 0x00
#define _OSC_Tmr1 0x01
#define _OSC_Internal 0x02
    
#define _OSC_int_31.25khz 0x00
#define _OSC_int_250khz 0x10
#define _OSC_int_500Khz 0x20
#define _OSC_int_1Mhz 0x30
#define _OSC_int_2Mhz 0x40
#define _OSC_int_4Mhz 0x50
#define _OSC_int_8Mhz 0x60
#define _OSC_int_16Mhz 0x70

#define _OSC_SleepEnable 0x00
#define _OSC_IdleEnable 0x80
#endif
    
// Interrupts
#define intPriorityEnable (RCONbits.IPEN = 1)
#define intPriorityDisable (RCONbits.IPEN = 0)
    
#define intGlobalEnable() (INTCONbits.GIE = 1)
#define intGlobalDisable() (INTCONbits.GIE = 0)
#define intPerifEnable() (INTCONbits.PEIE = 1)
#define intPerifDisable() (INTCONbits.PEIE = 0)

    // Master Clock is in Mhz
    // Availables at time of writing: 8, 20, and 40
#if _Device == PIC18F2455
#define master_Clock 8
#define _XTAL_FREQ 8000000
#elif _Device == PIC18F45K50
#define master_Clock 48
#define _XTAL_FREQ 48000000
#endif
    
    // Application-Specifc Definitions
#define gyNumADs 1
#define gyPortA_Def 0xff
#define gyPortB_Def 0x00
#define gyPortC_Def 0x00
    
#if pins == 40
#define gyPortD_Def 0x00
#define gyPortE_Def 0x00
#endif

Thank you in advance
post edited by The Bean - 2020/05/09 04:35:26
#1

11 Replies Related Threads

    du00000001
    Just Some Member
    • Total Posts : 3778
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: #if (macroDef == value) not working 2020/05/09 04:33:07 (permalink)
    +1 (1)
    I don't think a string comparison is within the scope of #if.
    Assigning integer values to your various "PIC-something" designators should bring things to work.
    So insert e.g.
    #define PIC18F2455  0
    #define PIC18F45K50 1

    prior your existing #define/#if and try.

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #2
    ric
    Super Member
    • Total Posts : 27630
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: #if (macroDef == value) not working 2020/05/09 04:35:42 (permalink)
    +1 (1)
    As above, or even just
    #define PIC18F2455
    .
    .
    #ifdef (PIC18F2455)

     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #3
    1and0
    Access is Denied
    • Total Posts : 10903
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: #if (macroDef == value) not working 2020/05/09 04:48:45 (permalink)
    +1 (1)
    As above, or add this before your snippet:
    #define PIC18F2455  2455
    #define PIC18F45K50 4550

     
    Edit: Just noticed Du suggested the same.  Where's my coffee. ;)
     
    post edited by 1and0 - 2020/05/09 04:51:44
    #4
    Jim Nickerson
    User 452
    • Total Posts : 6682
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: offline
    Re: #if (macroDef == value) not working 2020/05/09 06:26:13 (permalink)
    +1 (1)
    TheBean
    I want to run the same program on 2 different chips. I set up a header file to define the chip and its associated parameters (clock settings, number of pins, etc.) as follows:
     
    Sample (actual code is attached):
    #define _Device PIC18F45K50
    #if _Device == PIC18F2455
    #define pins 28
    ...
    #elif _Device == PIC18F45K50
    #define pins 40
    ...
    #endif
     
    I do this several times throughout the code. 
    In all cases, the first option is selected (e.g. #define pins 28) as it's highlighted and the second is not (#define pins 40) as it's gray'ed out. Compiling the code proves the selection is wrong (multiple errors for undefined macros, etc.). Note that I tried using paren's "#if (_Device == PIC18F45K50)" with the same result. Switching the order of the tests still always selects the first option. I've changed the name of the macro ("_Device") to more obscure names to ensure there isn't a duplicate, but get the same results. 
     
    Environment:  MPLAB X IDE v5.35
    Compiler:        XC8 v2.10
    Chips:             PIC18F2455 and PIC18F45K50
    Debugger:       ICD4
    Any Advice?
    #include "pic18f45k50.h"
    #define _Device PIC18F45K50
        
    // In this test, the "pins 28" is highlighted
    #if _Device == PIC18F2455
    # define pins 28
    #elif _Device == PIC18F45K50
    # define pins 40
    #endif
        
    // In this test, the "pins 40" is highlighted
    #if _Device == PIC18F45K50
    # define pins 40
    #elif _Device == PIC18F2455
    # define pins 28
    #endif

    #if _Device == PIC18F2455
        // OSCCON bit definitions
    #define _OSC_External 0x00
    #define _OSC_Tmr1 0x01
    #define _OSC_Internal 0x02
        
    #define _OSC_Int_31khz 0x00
    #define _OSC_int_125khz 0x10
    #define _OSC_int_250khz 0x20
    #define _OSC_int_500Khz 0x30
    #define _OSC_int_1Mhz 0x40
    #define _OSC_int_2Mhz 0x50
    #define _OSC_int_4Mhz 0x60
    #define _OSC_int_8Mhz 0x70

    #define _OSC_SleepEnable 0x00
    #define _OSC_IdleEnable 0x80
        
    #elif _Device == PIC18F45K50
    #define _OSC_External 0x00
    #define _OSC_Tmr1 0x01
    #define _OSC_Internal 0x02
        
    #define _OSC_int_31.25khz 0x00
    #define _OSC_int_250khz 0x10
    #define _OSC_int_500Khz 0x20
    #define _OSC_int_1Mhz 0x30
    #define _OSC_int_2Mhz 0x40
    #define _OSC_int_4Mhz 0x50
    #define _OSC_int_8Mhz 0x60
    #define _OSC_int_16Mhz 0x70

    #define _OSC_SleepEnable 0x00
    #define _OSC_IdleEnable 0x80
    #endif
        
    // Interrupts
    #define intPriorityEnable (RCONbits.IPEN = 1)
    #define intPriorityDisable (RCONbits.IPEN = 0)
        
    #define intGlobalEnable() (INTCONbits.GIE = 1)
    #define intGlobalDisable() (INTCONbits.GIE = 0)
    #define intPerifEnable() (INTCONbits.PEIE = 1)
    #define intPerifDisable() (INTCONbits.PEIE = 0)

        // Master Clock is in Mhz
        // Availables at time of writing: 8, 20, and 40
    #if _Device == PIC18F2455
    #define master_Clock 8
    #define _XTAL_FREQ 8000000
    #elif _Device == PIC18F45K50
    #define master_Clock 48
    #define _XTAL_FREQ 48000000
    #endif
        
        // Application-Specifc Definitions
    #define gyNumADs 1
    #define gyPortA_Def 0xff
    #define gyPortB_Def 0x00
    #define gyPortC_Def 0x00
        
    #if pins == 40
    #define gyPortD_Def 0x00
    #define gyPortE_Def 0x00
    #endif

    Thank you in advance


    Maybe this , works for me with xc8 2.10
     
     

    Attached Image(s)

    #5
    1and0
    Access is Denied
    • Total Posts : 10903
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: #if (macroDef == value) not working 2020/05/09 08:06:54 (permalink)
    +1 (1)
    Also, instead of this
    #include "pic18f45k50.h"

    use this
    #include <xc.h>

     
    #6
    RISC
    Super Member
    • Total Posts : 5744
    • Reward points : 0
    • Status: online
    Re: #if (macroDef == value) not working 2020/05/09 09:07:18 (permalink)
    +2 (2)
    Hi,
     
    Actually there is no need to define macros which are already defined by the MPLAB X environment  ;=)
    (It is true for all PIC microcontrollers)
     
    There is a more elegant way to do that :
    Create 2 project configurations and name them 45K50 and 2455 (probably you just want to copy the 45K50 into 2455 one.
     
    then add this code : 
    #if defined __18F45K50__
    // put code specific to PIC18F45K50 here
    #elif defined __18F2455__
    // put code specific to PIC18F2455 here
    #endif

     
    When you select the 45K50 configuration the MPLABX environment defined macro __18F45K50__ will be automatically true (you'll see it as the macro name will turn light blue
     
    then select the 2455 configuration and this time MPLABX environment defined macro __18F2455__ will be true 
     
    The #include <xc.h> is exactly doing this ;=)  and works with any PIC. No need to make conditional macros to include the appropriate header file
     
    Regards
    #7
    1and0
    Access is Denied
    • Total Posts : 10903
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: #if (macroDef == value) not working 2020/05/09 10:07:34 (permalink)
    0
    Curious, what is the difference among _18F45K50, __18F45K50, and __18F45K50__ ?
     
    #8
    RISC
    Super Member
    • Total Posts : 5744
    • Reward points : 0
    • Status: online
    Re: #if (macroDef == value) not working 2020/05/09 10:36:10 (permalink)
    +1 (1)
    Hi,
    1and0
    Curious, what is the difference among _18F45K50, __18F45K50, and __18F45K50__ ?

    You're right. All of them are valid.
    I guess it is just to keep compatiblity with legacy, but I don't expect any of them to have some difference
    Regards
     
    #9
    The Bean
    New Member
    • Total Posts : 21
    • Reward points : 0
    • Joined: 2020/03/03 05:26:03
    • Location: 0
    • Status: offline
    Re: #if (macroDef == value) not working 2020/05/09 11:13:30 (permalink)
    0
    Thank you all. The last method sounds the simplest. 
     
    #10
    crosland
    Super Member
    • Total Posts : 1979
    • Reward points : 0
    • Joined: 2005/05/10 10:55:05
    • Location: Warks, UK
    • Status: offline
    Re: #if (macroDef == value) not working 2020/05/09 13:43:38 (permalink)
    +2 (2)
    RISC
    There is a more elegant way to do that :
    Create 2 project configurations and name them 45K50 and 2455 (probably you just want to copy the 45K50 into 2455 one.



    More elegant still is to get rid of the conditional compilation altogether. Put device specific code in separate files for each device and use the file inclusion/exclusion for each configuration.
    #11
    mlp
    boots too small
    • Total Posts : 930
    • Reward points : 0
    • Joined: 2012/09/10 15:12:07
    • Location: previously Microchip XC8 team
    • Status: offline
    Re: #if (macroDef == value) not working 2020/05/17 17:56:49 (permalink)
    +5 (5)
    Your problem has already been addressed, but I thought I should point out something which may bite you later:
    TheBean
    #define _Device PIC18F45K50

    _Device is an identifier reserved for the Implementer, a.k.a. the compiler writer.
    All identifiers beginning with an underscore followed by an uppercase letter are reserved for the Implementer.
     

    Mark (this opinion available for hire)
    #12
    Jump to:
    © 2020 APG vNext Commercial Version 4.5