News:

Let's find out together what makes a PIC Tick!

Main Menu

Preprocessor $if question

Started by trastikata, Oct 04, 2024, 05:30 PM

Previous topic - Next topic

trastikata

Hello all,

I am trying to verify if two $define declares have the same defined value, however it is not working at all.

Any suggestions how to implement something like this:

Dim bConst As Byte
$define Port1 = PORTB.1
$define Port2 = PORTB.2

    $if Port1 = Port2
        bConst = 5
    $else
        bConst = 2
    $endif 
End     

bConst = ... is just some code and the idea is if Port1 and Port2 are defined with the same Pin (Port1 = PORTB.1 and Port2 = PORTB.1), then there is one type of code being used, and if they are different pins (Port1 = PORTB.1 and Port2 = PORTB.2) then there's another code used.

top204

A $define does not support an equals character after it.

Also, PORTB.1 and PORTB.2 are not $defined, so it gives an error because it has no reference to them.

Remember, a preprocessor works with values or texts only.

The preprocessor will need to use:

    Dim bConst As Byte

$define Port1 _PORTB.1
$define Port2 _PORTB.2

$if Port1 = Port2
    bConst = 5
$else
    bConst = 2
$endif


Because '_PORTB' is $defined as a value in the device's .def file, so the preprocessor can use it.

trastikata

#2
Quote from: top204 on Oct 04, 2024, 06:52 PMBecause '_PORTB' is $defined as a value in the device's .def file, so the preprocessor can use it.

Thank you Les. The "equal" sign was my omission.

Using "_PORTB.n" indeed does evaluate the $define condition, but now the code listing can't use the Pin $define i.e.:

Dim bConst As Byte
$define Port1  _PORTB.1
$define Port2  _PORTB.2

    $if Port1 = Port2
        bConst = 5
        High Port1
    $else
        bConst = 2
        High Port2
    $endif 
End


However High Port2 does not produce the expected code, should I use two different $defines - one for evaluation with underscore in front and another without for the actual code?

F1_000012 equ $ ; in [TEST.BAS] bConst = 2
    movlw 2
    movwf bConst,0
F1_000013 equ $ ; in [TEST.BAS] high 3969.2
F1_000015 equ $ ; in [TEST.BAS] end

Yasin

I think the "$else" is the problem. The example below compiles without any errors.

Dim bConst As Byte
$define Port1 RB1
$define Port2 RB2
$if Port1 = Port2
    bConst = 5
    High Port1
$else$if Port1 <> Port2
    bConst = 2
    High Port2
$endif

trastikata

Quote from: Yasin on Oct 05, 2024, 08:47 PMI think the "$else" is the problem. The example below compiles without any errors.

Dim bConst As Byte
$define Port1 RB1
$define Port2 RB2
$if Port1 = Port2
    bConst = 5
    High Port1
$else$if Port1 <> Port2
    bConst = 2
    High Port2
$endif

Hello Yasin,

my example compiles too but if you check the resultant Assembler code, you'd see that my in my case High Port2 is not being recognized as High PORTB.2.

In your case the code compiles but if you check the Assembler window you'd see it is not producing any code at all.

top204

I added a set of $defines for pins in each device's .def file for use with the preprocessor, as well as the compiler itself, and these are:

Pin_A0, Pin_A1, Pin_A2, Pin_A3, Pin_A4, Pin_A5, Pin_A6, Pin_A7 etc...
Pin_B0, Pin_B1, Pin_B2, Pin_B3, Pin_B4, Pin_B5, Pin_B6, Pin_B7 etc...

The same for the other ports on a device.

These are also mentioned in the compiler manuals, but I will add more about them in the manuals.

So the code works with:

    Dim bConst As Byte

$define Port1 Pin_B1
$define Port2 Pin_B2

$if Port1 = Port2               
    bConst = 5                     
$else                             
    bConst = 2                   
$endif


The Pin_Xx values are accepted throughout the compilers and can even be used in Declares.

When the compiler sees a Pin_Xx constant in a command, it converts it to a Port.Pin internally, so it wastes no code space or time, unless they are used in a command or procedure that will also accept a variable as a Port.Pin, then it calls a library subroutine to convert the value to a Port.Pin Address and Pin Mask.

So if the program has the line of code:

PinHigh Pin_B1

It will produce the assembler code:

F1_000049 equ $ ; in [TEST_18F25K20.BAS] PinHigh 9
    bcf TRISB,1,0
    bsf PORTB,1,0

The IDE also highlights the Pin_Xx values so they are easier to see and scan in the code, and it is obvious what they represent, by their names.

trastikata

This is great addition to the compiler, thank you Les.