8
\$\begingroup\$

Today I started to use MPLAB X to program my PICs, and found a code template, with multiple files and spots to fill in code. Before I have had a single file with my code in it. In this template, where I am supposed to put my Configuration bits, there is following text:

/* TODO Fill in your config bits here.  Remove #if 0 to embed config words.   */

#if 0

/* General syntax for configuration word 1 - Check your device .h file
for an up to date listing of available macros.*/
__CONFIG(FOSC_INTOSC & WDTE_OFF & PWRTE_OFF & MCLRE_OFF & BOREN_OFF);

/* If the device has multiple configuration words, the second macro defines
the second configuration word.  Again check your device .h file
for an up to date listing of available macros. */
__CONFIG(WRT_OFF & PLLEN_OFF & STVREN_OFF & BORV_19 & LVP_OFF);

#endif

I dont really understand what the "Remove #if 0 to embed config words." means... This is propably something really simple. I tried googling, and searching the compilers manual, but didnt get any good results. From what I can understand, the code in between #if 0 and #endif never gets compiled. What does this embed mean? Does it have something to do with setting the config-bits in code vs. afterwards?

\$\endgroup\$

3 Answers 3

17
\$\begingroup\$

To expand a bit on #if 0:
It's essentially a hack to allow for multi-line comments. The preprocessor, which runs before the compiler does, will remove everything between #if 0 and the matching #endif.

One reason it's used instead of /* */ style comments is that you can enable the entire block simply by changing it to "#if 1".

\$\endgroup\$
1
  • 9
    \$\begingroup\$ In particular, /* */ doesn't nest, so if you surround a large block of code with /* */ that already has a /* */ comment inside of it, the whole thing won't comment out correctly. A cleaner approach though, for this library, would have been to use #if defined EMBED_CONFIG_WORDS instead of #if 0 and then (possibly in a separate config-file or something) have a line like //#define EMBED_CONFIG_WORDS with a comment saying "uncomment this line to enable embedding of config-words." \$\endgroup\$ Commented Mar 3, 2012 at 16:57
5
\$\begingroup\$

From what I can understand, the code in between #if 0 and #endif never gets compiled.

Yes.

What does this embed mean? Does it have something to do with setting the config-bits in code vs. afterwards?

Config bits are a special programmed register in the PIC that determines operation of oscillator, brown-out detection, + other things. From one of the reference manuals: (the __CONFIG macro in C maps to the corresponding directive in assembly)

MPASM’s CONFIG Directive

Microchip’s assembler, MPASM, has a nice feature that allows you to specify, in the source code file, the selected states of the configuration bits for this program. This ensures that when pro- gramming a device for an application the required configuration is also programmed. This mini- mizes the risk of programming the wrong device configuration, and wondering why it no longer works in the application.

\$\endgroup\$
3
\$\begingroup\$

Your exactly right, the code in between the #if 0/#endif is never compiled. It's another way of commenting out a block of code.

Removing the #if 0/#endif will include the __CONFIG macros which will enable those bits in the PIC's configuration bits.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.