PIC16 header file / syntax question

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

  1. Rixen

    Rixen

    Joined:
    Feb 16, 2016
    Messages:
    71
    Likes Received:
    12
    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,434
    Likes Received:
    1,269
    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:
    71
    Likes Received:
    12
    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

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.
Similar Threads
  1. solmaz
    Replies:
    0
    Views:
    2,112
    solmaz
    Nov 19, 2003
  2. Douglas Eagleson

    A Stupid Header File Question-ZDSII and the Z8

    Douglas Eagleson, May 25, 2007, in forum: Electronic Design
    Replies:
    1
    Views:
    347
    Douglas Eagleson
    May 26, 2007
  3. bruno gavand

    undocumented PIC16 opcodes

    bruno gavand, Jun 28, 2007, in forum: Electronic Design
    Replies:
    26
    Views:
    1,208
    Paul Keinanen
    Jul 4, 2007
  4. Dave

    Free C compiler for PIC16 and PIC18

    Dave, Oct 30, 2004, in forum: 8bit Microcontrollers
    Replies:
    2
    Views:
    2,556
    Alexander
    Dec 23, 2004
  5. Lord_grezington

    Proramming issue with pic16

    Lord_grezington, May 4, 2013, in forum: General Electronics Chat
    Replies:
    1
    Views:
    328
    CocaCola
    May 4, 2013
  6. max_torch

    PIC16 interface with bluetooth help

    max_torch, Aug 11, 2015, in forum: Microcontrollers and Programming
    Replies:
    0
    Views:
    291
    max_torch
    Aug 11, 2015
  7. Maisha

    Keypad header file for atmega

    Maisha, Apr 25, 2016, in forum: Microcontrollers and Programming
    Replies:
    3
    Views:
    457
    Sadlercomfort
    May 5, 2016
Loading...