Author Topic: MPLABX Identifiers in C  (Read 1105 times)

0 Members and 1 Guest are viewing this topic.

Offline PoeTopic starter

  • Regular Contributor
  • *
  • Posts: 248
MPLABX Identifiers in C
« on: August 07, 2024, 07:43:20 pm »
I have some experience with assembly on PICs.  Now trying to learn C on PICs with MPLABX.  Having trouble knowing what identifiers are valid.

The following code for a PIC16f1847 gives NO error:
// Configure the Option register
   OPTION_REGbits.nWPUEN = 0;
   OPTION_REGbits.T0CS = 0;
   OPTION_REGbits.PSA = 0;
   OPTION_REGbits.PS = 0;   


The following code, for a different PIC (PIC16f570), gives a compile error:
// Configure the Option register
   OPTION_REGbits.nRBWU = 0;
   OPTION_REGbits.nRBPU = 0;   
   OPTION_REGbits.T0CS = 0;   
   OPTION_REGbits.PSA = 0;   
   OPTION_REGbits.PS = 0;   


COMPILE ERROR: Main.c:46:9: error: expected identifier
        OPTION.nRBPU = 0;
                     ^
 


Both include "#include <xc.h>". 
Changing "OPTION_REGbits.nRBWU" to "OPTION_bits.nRBWU" or "OPTION.nRBWU" gives the same error. 
Changing it to just "nRBWU = 1;" gives an "expression not assignable" error.

The above code can be 'fixed' by changing it to:
// Configure the Option register
   OPTION = 0;


The "pic16f1847.h" file appears to be responsible... or what is used...to make this work... although I don't understand how.
} OPTION_REGbits_t;
extern volatile OPTION_REGbits_t OPTION_REGbits __at(0x095);
// bitfield macros
#define _OPTION_REG_PS0_POSN                                0x0
#define _OPTION_REG_PS0_POSITION                            0x0
#define _OPTION_REG_PS0_SIZE                                0x1
#define _OPTION_REG_PS0_LENGTH                              0x1
#define _OPTION_REG_PS0_MASK                                0x1
#define _OPTION_REG_PS1_POSN                                0x1
#define _OPTION_REG_PS1_POSITION                            0x1
#define _OPTION_REG_PS1_SIZE                                0x1
...etc


The "pic16f570.h" file does not appear to have code to support these special register identifier bits.
// Register: OPTION
#define OPTION OPTION
extern volatile __control unsigned char OPTION              __at(0x000);

/*
 * OPTION bits
 */
#define                                 PS                  0x7
#define                                 PSA                 0x8
#define                                 T0SE                0x10
#define                                 T0CS                0x20
#define                                 nRBPU               0x40
#define                                 nRBWU               0x80
#define                                 PS0                 0x1
#define                                 PS1                 0x2
#define                                 PS2                 0x4


Just looking for any resource like a tutorial or google search term to explain how to know what's valid.  Maybe some part of the MPLAB XC8 C Compiler User guide I'm missing?
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 6183
  • Country: es
Re: MPLABX Identifiers in C
« Reply #1 on: August 07, 2024, 07:58:48 pm »
You don't need any manual, read the pic manual, you're assuming the bits and registers are the same, but they're very different.

For the 1847 it's OPTION_REGbits.WPUEN.
For the 547 it's OPTIONbits.RBPU.

Or open picxxxx.h, search the register definition and see how the bits are called.
« Last Edit: August 07, 2024, 08:19:27 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 
The following users thanked this post: Poe

Offline PoeTopic starter

  • Regular Contributor
  • *
  • Posts: 248
Re: MPLABX Identifiers in C
« Reply #2 on: August 07, 2024, 09:23:25 pm »
Thank you for taking the time to help.   :) 

Do you mean instructions (bits) and registers are being confused?  That makes sense.  I forgot about "OPTION" being an instruction for some PICs.  When programming in assembly I would have caught that because the OPTION register doesn't have an address.  Guess that's why "OPTION = 0;" works while "OPTION_REGbits.PSA = 0;" and "OPTIONbits.PSA = 0;" do not. 

The datasheet doesn't say anything about adding "bits.x" to the end of a register name in order to access the bits.  e.g.  OPTION_REGbits.nWPUEN
So that's why I was trying to use the "pic16f570.h" file to determine which mnemonics were valid.

If anyone has a better place to learn about this, I'd love to know.
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 13026
Re: MPLABX Identifiers in C
« Reply #3 on: August 07, 2024, 09:55:53 pm »
PIC16F570 is a Baseline device (albeit an enhanced one with a four level stack and interrupts).  Unlike Midrange and Enhanced Midrange devices which  have their OPTION and port TRIS registers memory mapped, the Baseline OPTION and port TRIS registers are write-only accessed by special instructions that load them.  This makes it impossible for the compiler to offer bitfield access to them.  You need to build the desired register value as a single byte, by using bitwise logic operators and the bitmasks defined in the header, then assign it in a single write:
Code: [Select]
OPTION = complicated_C_expression_evaluated_at_build_time;
Alternatively write the value as a binary constant (prefix 0b) referring to the register description in the datasheet.
« Last Edit: August 07, 2024, 09:58:53 pm by Ian.M »
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 6183
  • Country: es
Re: MPLABX Identifiers in C
« Reply #4 on: August 07, 2024, 10:19:20 pm »
The datasheet doesn't say anything about adding "bits.x" to the end of a register name in order to access the bits.
I know, but the generally it follows the datasheet name.
If you see a register named ANSELA in the datasheet, it'll be called that way in xc8, and the bitfields will be ANSELAbits.

This was exactly the issue you were having, not checking the registers and bit names in the datasheet, you would have found the differences in OPTION/OPTION_REG and RBPU/WPUEN.

You won't find a compiler guide for every PIC out there!

Maybe you should check Microchip Online docs, specially MPLABX and XC8 user guides!

Also check Program Files/Microchip/xc8/vx.xx/docs/.

The standard way is to check the header files, as stated in the Device-specific features:
How Do I Access SFRs?
How Do I Find The Names Used To Represent SFRs And Bits?.

You can quickly have a peek by writing i.e. OPTION_REGbits, then right click, Navigate / Go to declaration/Definition.

And remember to make use of code autocompletion, makes life so much easier.
Example 1: Write OPTION and do Ctrl+Space, anything starting with that will appear (OPTION_REG, OPTION_REGbits).
Example 2: Write OPTION_REGbits. (Don't forget the dot), again Ctrl+Space, a list of the reg bits will appear, select the one you're searching for.

In this case RBPU bit is called nRBPU (n for negated).

« Last Edit: August 07, 2024, 10:51:38 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline MarkF

  • Super Contributor
  • ***
  • Posts: 2630
  • Country: us
Re: MPLABX Identifiers in C
« Reply #5 on: August 07, 2024, 11:56:15 pm »
I just use MPLABX's 'Configuration Bits' tool to setup the bits.
It can be found on the dropdown menu:

  Window -> PIC Memory Views -> Configuration Bits

You just make the configuration selections you want in the tab. 
After which, you press the 'Generate Source Code to Output' button at the bottom of the tab.
Then just copy-&-paste from the 'output' tab to your source code and you're done.
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4281
  • Country: us
Re: MPLABX Identifiers in C
« Reply #6 on: August 08, 2024, 07:04:11 am »
New chips have "more modern" definitions for register and bits  (structures, types, standard naming syntax, etc.)You pretty much have to look at the .h file to figure out what you've got.)
(It doesn't get really hairy until they switch styles between versions, like they did with SAMD)
 
The following users thanked this post: Ian.M

Offline NorthGuy

  • Super Contributor
  • ***
  • Posts: 3237
  • Country: ca
Re: MPLABX Identifiers in C
« Reply #7 on: August 19, 2024, 02:15:02 pm »
Microchip are big at renaming things. The same register or field may be named different in different PICs.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf