Maker Pro
Maker Pro

PIC16 header file / syntax question

Rixen

Feb 16, 2016
98
Joined
Feb 16, 2016
Messages
98
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;


syazX62.png


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.

ptvVg3R.png



Any advice is appreciated, I can insert more of the header file if its needed.
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,681
Joined
Nov 17, 2011
Messages
13,681
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
 

Rixen

Feb 16, 2016
98
Joined
Feb 16, 2016
Messages
98
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

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.
 
Top