*IF ELSE ELSEIF ENDIF/de

From MCS Wiki AVR
Jump to: navigation, search

Contents

= (**COPIED FROM ENGLISH PAGE**) === Action

Conditional compilation directives intended for conditional compilation.

 

 

Syntax

#IF condition

 

#ELSEIF condition

 

#ELSE

 

#ENDIF

 

 

Remarks

Conditional compilation is supported by the compiler.

What is conditional compilation?

Conditional compilation will only compile parts of your code that meet the criteria of the condition.

 

By default all your code is compiled.

 

Conditional compilation needs a constant to test.

So before a condition can be tested you need to define a constant.

 

CONST test = 1

#IF TEST

    Print "This will be compiled"

#ELSE

    Print "And this not"

#ENDIF

 

Notice.jpg
 Note that there is no THEN and that #ENDIF is not #END IF (no space)

 

You can nest the conditions and the use of #ELSE and #ELSEIF is optional.

 

There are a few internal constants that you can use. These are generated by the compiler:

_CHIP = 0

_RAMSIZE = 128

_ERAMSIZE = 128

_SIM = 0

_XTAL = 4000000

_BUILD = 11162

 

_CHIP is an integer that specifies the chip, in this case the 2313

_RAMSIZE is the size of the SRAM

_ERAMSIZE is the size of the EEPROM

_SIM is set to 1 when the $SIM directive is used

_XTAL contains the value of the specified crystal

_BUILD is the build number of the compiler.

 

The build number can be used to write support for statements that are not available in a certain version :

#IF _BUILD >= 11162

 s = Log(1.1)

#ELSE

Print "Sorry, implemented in 1.11.6.2"

#ENDIF

 

Conditional compilation allows you to create different versions of your program but that you keep one source file.

For example you could make a multi lingual program like this :

 

CONST LANGUAGE=1

 

'program goes here

 

#IF LANGUAGE=1

DATA "Hello"

#ENDIF

#IF LANGUAGE=2

DATA "Guten tag"

#ENDIF

 

By changing the just one constant you then have for example English or German data lines.

 

 

Conditional compilation does not work with the $REGFILE directive. If you put the $REGFILE inside a condition or not, the compiler will use the first $REGFILE it encounters. This will be changed in a future version.

 

A special check was added to 1.11.8.1 to test for existence of constants or variables.

#IF varexist("S")

      'the variable S was dimensioned so we can use it here

#ELSE

   'when it was not dimmed and we do need it, we can do it here

  DIM S as BYTE

#ENDIF

 

 

See Also

CONST


Languages   English Deutsch  
Personal tools
Namespaces
Variants
Actions
Navigation
In other languages
Language