PIC16 header file / syntax question

Discussion in 'Microcontrollers and Programming' started by Rixen, Oct 12, 2017.

  1. Rixen

    Rixen

    Joined:
    Feb 16, 2016
    Messages:
    77
    Likes Received:
    13
    Hi all,

    Recently in uni we are messing with the MSP430 Launchpad from TI, when setting registers there,
    we look up the registers in the included header file, setting the individual bits was quite intuitive.

    Say I wanted to set Bits TASSEL1 and MC0 of the TA0CTL register I could just write
    TA0CTL |= MC0 + TASSEL1;


    [​IMG]

    I want to try do the same with a PIC micro, but it's header file is different, can anyone show what the syntax is for setting individual and multiple bits in here..?

    The way I found was like this, if i wanted to set a couple bits in this OSCCON register, this seems to work.
    OSCCONbits.SCS1 = 1;
    OSCCONbits.IRCF1 = 1;
    OSCCONbits.IRCF2 = 1;
    OSCCONbits.IRCF3 = 1;
    OSCCONbits.SPLLEN = 1;

    But surely there is a way similar to the MSP430 one.

    Also, what does the _t at the end mean? OSCCONbits_t. SCS1 = 1; seems to not work.

    [​IMG]


    Any advice is appreciated, I can insert more of the header file if its needed.
     
    Rixen, Oct 12, 2017
    #1
    1. Advertisements

  2. Rixen

    Harald Kapp Moderator Moderator

    Joined:
    Nov 17, 2011
    Messages:
    7,606
    Likes Received:
    1,336
    Location:
    Germany-Europe-Earth-Sol System-Milky Way-Laniakea
    OSCCONbits_t: The '_t' is commonly used in C syntax to mark a name as a type (as identified by typedef...). When you look through typical header files you will often see e.g. uint8_t etc. used in a similar way. This is afaik not mandatory, but common usage.

    To access the OSCCON register using a meaningful name, the line
    extern volatile OSCCONbits_t OSCCONbits @ 0x099;
    is used. It defined a variable with the name OSCCONbits as being of the type OSCCONbits_t at address 0x099. Therefore you access the register by OSCCONbits.SCS0 (as an example), the '.' notation being used to address the single named bit within this register.


    The keyword 'volatile' is used to indicate to the compiler that this is a variable that may change at any time, e.g. by external signals. The compiler then cannot remove accesses to this 'variable' even if during its static code analysis at compile time the 'variable' seemingly is not changed. If you omit the keyword 'volatile', the compiler may remove the variable from code sections where it finds no change in the variable, whereas the code relies on this variable to be changed e.g. by an external input signal.
    Here's some more on this topic: https://barrgroup.com/Embedded-Systems/How-To/C-Volatile-Keyword
     
    Harald Kapp, Oct 12, 2017
    #2
    Rixen likes this.
    1. Advertisements

  3. Rixen

    Rixen

    Joined:
    Feb 16, 2016
    Messages:
    77
    Likes Received:
    13
    Hi Harald,

    Thank you so much for the reply, I got exactly what I was after :)

    Interesting article on volatile, the explanation we were given was the same as that one, except they show us no examples, use applications, or really anything of the sort.
     
    Rixen, Oct 12, 2017
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.