This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Conditional compiling



 

Hi All!

Suppose I have two PCB's :

PCB_REV1 – Led connected to P1.4

PCB_REV2 – Led connected to P3.6

so I want to compile the code according to PCB

 

#define PCB_REV1

 

void main(void)

{

#ifdef PCB_REV1

  blink_led_1_4();

#else

  blink_led_3_6();

#endif

}

 

it's compiling without any error but doesn't work.

  • Hi John7,

    I assume your blink_led routine is the same for both PCB version and only the 'LED pin' differs (--> different hardware layer).

    I would recommend using another type of conditional compiling; have a look at the example below:
    /*-----------------------------------------------------------------------------
    // Compiler switches
    // used as:
    //            #ifdef  PCB_REV1
    //             ...  ; additional code lines used with PCB Revison 1 only
    //            #endif
    // ONLY USE ONE COMPILER SWITCH!
    //---------------------------------------------------------------------------*/
    #define PCB_REV1                           // code lines used with PCB Revison 1
    //#define PCB_REV2                           // code lines used with PCB Revison 2


    // defines for hardware connection
    #ifdef PCB_REV1
    #define LED_1_ON      P1OUT |= BIT4;      // output HIGH --> switch on
    #define LED_1_OFF     P1OUT &= ~BIT4;     // output LOW --> switch off
                                              // LED_1 is connected to P1.4
    #define LED_1_TOGGLE  P1OUT ^= BIT4;      // toggles P1.4
    #endif

    #ifdef PCBREV2
    #define LED_1_ON      P3OUT |= BIT6;      // output HIGH --> switch on
    #define LED_1_OFF     P3OUT &= ~BIT6;     // output LOW --> switch off
                                              // LED_1 is connected to P3.6
    #define LED_1_TOGGLE  P3OUT ^= BIT6;      // toggles P3.6
    #endif

    Be shure to have the I/O pins configured accordingly, i.e.:

    // configure I/Os
    #ifdef PCB_REV1                         
    P1DIR |= BIT4;                          // P1.4 is switched to output mode, 1                  
    LED_1_OFF;                              // output LOW --> LED_1 off
    #endif

    #ifdef PCB_REV2                         
    P3DIR |= BIT6;                          // P3.6 is switched to output mode, 1                  
    LED_1_OFF;                              // output LOW --> LED_1 off
    #endif

    This example shows that I have one #define (i.e. LED_1_ON) which could be connected to two different pins (P1.4 or P3.6).

    During my application I only refer to LED_1_ON, so I don't need to care about the context I'm in.
    I.e.:

    void blink_led
    {
        LED_1_ON;  // output HIGH --> switch on LED
        __delay_cycles(35000); // make some delay
        LED_1_OFF;  // output LOW --> switch off LED
        __delay_cycles(35000); // make some delay
    }

    Rgds
    aBUGSworstnightmare

  • Thank you, I'll try.

  • Thank you it works fine.

**Attention** This is a public forum