MPLABX Identifiers in C - Page 1
Forum section: electronics / general

author: Poe
author_rank: Frequent Contributor
timestamp: August 07, 2024, 07:43:20 pm
content: 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 registerOPTION_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 registerOPTION_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 identifierOPTION.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 registerOPTION = 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...etcThe "pic16f570.h" file does not appear to have code to support these special register identifier bits.// Register: OPTION#define OPTION OPTIONextern 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 0x4Just 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?

author: DavidAlfa
author_rank: Super Contributor
timestamp: August 07, 2024, 07:58:48 pm
content: 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.

author: Poe
author_rank: Frequent Contributor
timestamp: August 07, 2024, 09:23:25 pm
content: 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.nWPUENSo 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.

author: Ian.M
author_rank: Super Contributor
timestamp: August 07, 2024, 09:55:53 pm
content: 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 arewrite-onlyaccessed 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: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.

author: DavidAlfa
author_rank: Super Contributor
timestamp: August 07, 2024, 10:19:20 pm
content: Quote from: Poe on August 07, 2024, 09:23:25 pmI know, but the generally it follows the datasheet name.If you see a register namedANSELAin the datasheet, it'll be called that way in xc8, and the bitfields will beANSELAbits.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 checkMicrochip Online docs, speciallyMPLABXandXC8user guides!Also checkProgram Files/Microchip/xc8/vx.xx/docs/.The standard way is to check the header files, as stated in theDevice-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: WriteOPTIONand do Ctrl+Space, anything starting with that will appear (OPTION_REG,OPTION_REGbits).Example 2: WriteOPTION_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 caseRBPUbit is callednRBPU(n for negated).

author: MarkF
author_rank: Super Contributor
timestamp: August 07, 2024, 11:56:15 pm
content: 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 BitsYou 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.

author: westfw
author_rank: Super Contributor
timestamp: August 08, 2024, 07:04:11 am
content: 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)

author: NorthGuy
author_rank: Super Contributor
timestamp: August 19, 2024, 02:15:02 pm
content: Microchip are big at renaming things. The same register or field may be named different in different PICs.