• AVR Freaks

Hot!Trying to Understand How MCC includes Device Config Registers

Author
racingsubby
Starting Member
  • Total Posts : 54
  • Reward points : 0
  • Joined: 2010/06/22 10:34:11
  • Location: 0
  • Status: offline
2019/08/15 20:41:06 (permalink)
0

Trying to Understand How MCC includes Device Config Registers

Hi.  I'm a rookie at C code.  Everything I've done usually only includes 1 source file, the main.c.  My understanding is that to access functions in other C files, you include the header file the declares those functions at the top of your main.c file for the pre-processor to see them.  Then you can call those functions.
So I used MCC to setup some basic digital I/O on Port C.  I've then tried to understand how it all pieces together.  I see the function call for the system initialize in main.c  Within the system initialize function, it calls pin_manager initialize and oscillator initialize to setup the pins and oscillator registers.  I get all that.
What I'm struggling with is how do the CONFIG registers, located in the device_confic.c file, get included in the build.  There's no function call or anything.  The device.h file only has the definition of the XTAL_FREQ.  So how do these registers get included into the program?
Let me know if this is clear enough.
Thanks for any feedback!grin: grin
#1

12 Replies Related Threads

    ric
    Super Member
    • Total Posts : 23545
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Trying to Understand How MCC includes Device Config Registers 2019/08/15 20:46:08 (permalink)
    0
    CONFIG registers are set with #pragma lines, which are instructions directly to the compiler.
    They are NOT executable statements, so they are never "called".
    Just having those "#pragma config" lines in one of your C files is all you require.
    n.b. They should only appear in one C file, so most definitely do NOT ever put them into a header file that is included into multiple C files.
     
    Ric (aka "subby_is_towcar" ;) )
     
    post edited by ric - 2019/08/15 20:50:04

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    du00000001
    Just Some Member
    • Total Posts : 2998
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: Trying to Understand How MCC includes Device Config Registers 2019/08/16 00:54:01 (permalink)
    0
    The config registers hold bits that influence the chip's configuration (behavior). They are stored at specific addresses and read during reset or power-up. (Doesn't make much difference - see the dataheet(s) for the exact "mechanism".)

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #3
    racingsubby
    Starting Member
    • Total Posts : 54
    • Reward points : 0
    • Joined: 2010/06/22 10:34:11
    • Location: 0
    • Status: offline
    Re: Trying to Understand How MCC includes Device Config Registers 2019/08/16 11:19:19 (permalink)
    0
    Ric!
    Thank you much for the reply.  I haven't used the #pragma statements much so did not know this.  To make sure I understand, if I make a seperate C file with these in it and no functions at all, all I need to do then is create a blank header file for it and include that in my main.c?  Does anything need to be in the header file?
    Also, with the way this works, I'm assuming this is the same as putting the CONFIG registers at the top of my main.c instead of the #include header statement.  Am I understanding this correctly?
     
    Thanks!
    Tom
    #4
    mbrowning
    Just a Member
    • Total Posts : 1504
    • Reward points : 0
    • Joined: 2005/03/16 14:32:56
    • Location: Melbourne, FL
    • Status: online
    Re: Trying to Understand How MCC includes Device Config Registers 2019/08/16 11:56:50 (permalink)
    0
    No. The config lines must be in a C file that is part of the project. No h file needed.

    I accomplish this by putting my config lines in an h file (that has just the config lines) and #include at the beginning of my c file where main() is. Config lines Should be before any includes or defines

    Oh well - there's always next year
    #5
    ric
    Super Member
    • Total Posts : 23545
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Trying to Understand How MCC includes Device Config Registers 2019/08/16 15:47:01 (permalink)
    0
    racingsubby
    ...
    if I make a seperate C file with these in it and no functions at all, all I need to do then is create a blank header file for it and include that in my main.c?

    You don't need the header file at all, just add that C file to the project.

    Does anything need to be in the header file?

    No, because you don't need it.

    Also, with the way this works, I'm assuming this is the same as putting the CONFIG registers at the top of my main.c instead of the #include header statement.  Am I understanding this correctly?

    Not quite, as already explained.
    You can do as mbrowning said, put them in a header file, and then include THAT into one of your C files.
    I don't encourage this, because out of havit, people are liable to go and include that header into ALL their C files, which is wrong.
     
    If you want to remove them from your main.c, then as already described, the cleanest way is just to put them into a separate C file, and add that to your project. NOTHING MORE!
     
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #6
    racingsubby
    Starting Member
    • Total Posts : 54
    • Reward points : 0
    • Joined: 2010/06/22 10:34:11
    • Location: 0
    • Status: offline
    Re: Trying to Understand How MCC includes Device Config Registers 2019/08/17 17:38:46 (permalink)
    0
    Ric,
    Great explanation!  One follow up question.  So I just need to have them in a seperate C file with no associated header file.  I got it.  Is that a general condition for C code when using the #pragma statements because, as you noted earlier, this is statements directly called by the compiler.  Or is this specific to the CONFIG registers and the XC compiler?
    Again thanks to you and mbrowing for the help!
    Tom
    #7
    ric
    Super Member
    • Total Posts : 23545
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Trying to Understand How MCC includes Device Config Registers 2019/08/18 00:12:49 (permalink)
    0
    racingsubby
    ...
    Is that a general condition for C code when using the #pragma statements because, as you noted earlier, this is statements directly called by the compiler.  Or is this specific to the CONFIG registers and the XC compiler?

    "#pragma" is a very general purpose construct that the C compiler can get to do whatever they want.
    More commonly they are used to control how the compiler treats source code following the #pragma in the current file.
    The "#pragma config" construct is specific to Microchip compilers, and it does what we have been discussing.
    i.e. it tells the compiler what values are to be stored in the CONFIG words in the hex file.
     
    Again, don't talk about these lines being "called" by the compiler. That leads to misunderstanding to how they work.
    They are never "called", they are just instructions passed to the compiler at compile time, similar to command line switches.
     
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #8
    1and0
    Access is Denied
    • Total Posts : 9607
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Trying to Understand How MCC includes Device Config Registers 2019/08/18 03:03:23 (permalink)
    0
    The "#pragma config" directive is neither a statement nor an instruction that is "called" or executed by the compiler. It simply tells the compiler what value to set the configuration registers, which set up the device operation, such as the oscillator mode, watchdog timer, code protection, etc.
     
    You can have them either in a .c file and add it to your project; or in a .h file and #include it in one and only one .c file, typically at the very beginning of where main() is. Or, you can just have them at the very beginning of the .c file where main() is.
    #9
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3233
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: Trying to Understand How MCC includes Device Config Registers 2019/08/18 05:11:07 (permalink)
    4 (1)
    Compilers only collect data so they can be put anywhere.
    They are put in a separate file to make code easy to read.
     
     
     
    post edited by Gort2015 - 2019/08/18 05:12:13

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #10
    racingsubby
    Starting Member
    • Total Posts : 54
    • Reward points : 0
    • Joined: 2010/06/22 10:34:11
    • Location: 0
    • Status: offline
    Re: Trying to Understand How MCC includes Device Config Registers 2019/08/19 10:12:49 (permalink)
    0
    Thanks all for the clarifications.  Just wanna make sure I got a clear understanding.  NOTE: I'll never use the word 'call' again when referring to these! :)
    So I can:
    a) Put them in a seperate C file with my project but no header file need to 'call' them.
    b) Put them in a header file and call that header file once and only once in my main C file.
    c) Just put them at the very top of my main source file.
     
    Check!  Thank you much! 
    Tomgrin: grin
    #11
    mbrowning
    Just a Member
    • Total Posts : 1504
    • Reward points : 0
    • Joined: 2005/03/16 14:32:56
    • Location: Melbourne, FL
    • Status: online
    Re: Trying to Understand How MCC includes Device Config Registers 2019/08/19 10:40:55 (permalink)
    0
    You used the word “call” immediately after promising not to :)
    You don’t “call” a header - you “include” it with #include and the preprocessor substitutes the text of the header for the #include line before passing on to the compiler

    Oh well - there's always next year
    #12
    ric
    Super Member
    • Total Posts : 23545
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Trying to Understand How MCC includes Device Config Registers 2019/08/19 13:20:47 (permalink)
    0
    Once you understand what "#include" does, you will see that method "b)" and method "c)" are effectively the same.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #13
    Jump to:
    © 2019 APG vNext Commercial Version 4.5