News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

PrePocessor question

Started by diebobo, Sep 14, 2024, 10:45 AM

Previous topic - Next topic

diebobo

I am using the code below to direct universal commands to a different I2C bus..  But i would like to know if it's possible to "know" for the preprocessor if a variable is already declared, and if not create that variable ?

$ifndef EEPROM_I2C
    $error "$define EEPROM_I2C not issued in the main program"
$endif

$if EEPROM_I2C = 1
    Dim EEPROM_I2C_Occupied As I2C_Bus_1_Occupied
    $define EEPROM_BusIn    HBusIn1
    $define EEPROM_BusOut   HBusOut1
$endif

$if EEPROM_I2C = 2
    Dim EEPROM_I2C_Occupied As I2C_Bus_2_Occupied
    $define EEPROM_BusIn    HBusIn2
    $define EEPROM_BusOut   HBusOut2
$endif

$if EEPROM_I2C = 3
    Dim EEPROM_I2C_Occupied As I2C_Bus_3_Occupied
    $define EEPROM_BusIn    HBusIn3
    $define EEPROM_BusOut   HBusOut3
$endif

trastikata

You can declare variables as Shared within pre-processor defines and if it happens that a variable is already declared, the newly declared variable, which will happen to be repeated, will share the same address.

diebobo

Shared.. Shared.. I read that at first and did not ring a bell realy loud, help file -> shared.... Yes ! That's it... Thanks Trastikata

top204

#3
The preprocessor is a seperate program that runs before the compiler, and has no knowledge of variables created or used within the code listing.

The Shared directive is something I added for myself many years ago for internal use only, when I was first writing and testing the 8-bit compiler.

However, it was so useful, I re-wrote its mechanism to be more controlled, and made it a user directive, and it comes in very handy within procedures and preprocessor meta-macros. See page 25 of the latest Positron8 compiler's manual, and page 29 of the latest Positron16 compiler's manual.