• AVR Freaks

Hot!DEVICE_CONFIG_H

Author
PK DEY
New Member
  • Total Posts : 15
  • Reward points : 0
  • Joined: 2019/01/20 22:52:04
  • Location: 0
  • Status: offline
2019/02/13 08:08:58 (permalink)
0

DEVICE_CONFIG_H

PIC18F87K22
MCC v1.65.2
MPLAB XC8 Compiler 2.05


When I generate code using MCC it generate a header file called "device_config.h" and the following code is written down on that file. 

#ifndef DEVICE_CONFIG_H
#define DEVICE_CONFIG_H

#define _XTAL_FREQ 8000000

#endif

I don't understand the function of the following statements in the above code! 
#ifndef DEVICE_CONFIG_H
#define DEVICE_CONFIG_H
Could someone please explain the function of the above statement?
post edited by PK DEY - 2019/02/13 08:10:29
#1

8 Replies Related Threads

    1and0
    Access is Denied
    • Total Posts : 9040
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: DEVICE_CONFIG_H 2019/02/13 08:20:50 (permalink)
    #2
    PK DEY
    New Member
    • Total Posts : 15
    • Reward points : 0
    • Joined: 2019/01/20 22:52:04
    • Location: 0
    • Status: offline
    Re: DEVICE_CONFIG_H 2019/04/16 18:13:24 (permalink)
    0
    #define COUNT 100
    In the above statement "COUNT" constant is defined to a value 100.
    #define DEVICE_CONFIG_H
    BUT in the above statement "DEVICE_CONFIG_H" is defined to what value? Explain please?

    Thanks in Advance?
     
    #3
    pcbbc
    Super Member
    • Total Posts : 926
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: DEVICE_CONFIG_H 2019/04/16 21:21:31 (permalink)
    +2 (2)
    Esentially an empty string. But the point is it is defined.
    Prior to...
    #define DEVICE_CONFIG_H
    ...it was UN-defined.
    #4
    qhb
    Superb Member
    • Total Posts : 9990
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: DEVICE_CONFIG_H 2019/04/17 04:51:39 (permalink)
    +2 (2)
    ... and this matters because it is possible to detect if a label is defined at all using "#ifdef".
     

    Only 10 to go...
    #5
    PK DEY
    New Member
    • Total Posts : 15
    • Reward points : 0
    • Joined: 2019/01/20 22:52:04
    • Location: 0
    • Status: offline
    Re: DEVICE_CONFIG_H 2019/04/25 19:56:06 (permalink)
    0
    #include "DEFINE.H"
    #ifndef TEST
    #define TEST 1
    #endif

    According to the above code:
    DEFINE.H is the header file that is supposed to contain the definition of TEST macro. The directive #ifndef TEST searches for the definition of TEST in the header file and if not defined then all the lines between #ifndef and the corresponding #endif directive are left active in the program. That is the preprocessor directive #define TEST 1 is processed
    But in the following code in where the directive #define DEVICE_CONFIG_H search for the definition of "DEVICE_CONFIG_H"? Also DEVICE_CONFIG_H is defined to what?

    #ifndef DEVICE_CONFIG_H
    #define DEVICE_CONFIG_H

    #define _XTAL_FREQ 8000000

    #endif

    #6
    1and0
    Access is Denied
    • Total Posts : 9040
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: DEVICE_CONFIG_H 2019/04/25 20:13:27 (permalink)
    +1 (1)
    PK DEY
    But in the following code in where the directive #define DEVICE_CONFIG_H search for the definition of "DEVICE_CONFIG_H"? Also DEVICE_CONFIG_H is defined to what?
    #ifndef DEVICE_CONFIG_H
    #define DEVICE_CONFIG_H
    #define _XTAL_FREQ 8000000
    #endif


    Did you read about include guard in the link that I provided in Post #2?
     
    It is used to avoid double inclusion of content between #ifndef and #endif.
     
    When the C preprocessor processes this
    #ifndef DEVICE_CONFIG_H
    #define DEVICE_CONFIG_H
    #define _XTAL_FREQ 8000000
    #endif

    for the first time, DEVICE_CONFIG_H is not defined so the #ifndef condition is true, then it defines DEVICE_CONFIG_H along with _XTAL_FREQ. If this code (usually in a header file) is #included again, these #define are skipped since now DEVICE_CONFIG_H is already been defined; i.e. the #ifndef condition is now false. This is done to avoid definitions of symbols that are already defined, when this header file is included in different files.
     
    As other said, it is defined not with a value but the fact that it is defined, which can be tested with #ifdef and #ifndef.
    post edited by 1and0 - 2019/04/25 20:29:11
    #7
    qhb
    Superb Member
    • Total Posts : 9990
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: DEVICE_CONFIG_H 2019/04/26 02:14:27 (permalink)
    +1 (1)
    PK DEY
    ...
    But in the following code in where the directive #define DEVICE_CONFIG_H search for the definition of "DEVICE_CONFIG_H"? Also DEVICE_CONFIG_H is defined to what?

    I already explained how this works in post#5.
    It gets very annoying having to explain the same thing multiple times!
     

    Only 10 to go...
    #8
    1and0
    Access is Denied
    • Total Posts : 9040
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: DEVICE_CONFIG_H 2019/04/26 03:00:31 (permalink)
    +1 (1)
    qhb
    I already explained how this works in post#5.
    It gets very annoying having to explain the same thing multiple times!

    Agreed!  For those with a thick skull, in plain English, literally, #ifdef is "if defined" and #ifndef is "if not defined". mr green: mr green
     
    #9
    Jump to:
    © 2019 APG vNext Commercial Version 4.5