Microchip Technology XC8 Standard Compiler (Workstation) SW006021-1 SW006021-1 ユーザーズマニュアル
製品コード
SW006021-1
MPLAB
®
XC8 C Compiler User’s Guide
DS52053B-page 140
2012 Microchip Technology Inc.
5.3.8
Bit Instructions
Wherever possible, the MPLAB XC8 C Compiler will attempt to use bit instructions,
even on non-bit integer values. For example, when using a bitwise operator and a mask
to alter a bit within an integral type, the compiler will check the mask value to determine
if a bit instruction can achieve the same functionality.
even on non-bit integer values. For example, when using a bitwise operator and a mask
to alter a bit within an integral type, the compiler will check the mask value to determine
if a bit instruction can achieve the same functionality.
unsigned int foo;
foo |= 0x40;
will produce the instruction:
BSF _foo,6
To set or clear individual bits within integral type, the following macros could be used:
#define bitset(var, bitno) ((var) |= 1UL << (bitno))
#define bitclr(var, bitno) ((var) &= ~(1UL << (bitno)))
To perform the same operation on foo as above, the bitset macro could be
employed as follows:
employed as follows:
bitset(foo,6);
5.3.9
Baseline PIC MCU Special Instructions
The Baseline devices have some registers which are not in the normal SFR space and
cannot be accessed using an ordinary file instruction. These are the OPTION and TRIS
registers.
cannot be accessed using an ordinary file instruction. These are the OPTION and TRIS
registers.
Both registers are write-only and cannot be used in expression that read their value.
They can only be accessed using special instructions which the compiler will use
automatically.
They can only be accessed using special instructions which the compiler will use
automatically.
The definition of the variables that map to these registers make use of the control
qualifier. This qualifier informs the compiler that the registers are outside of the normal
address space and that a different access method is required. You should not use this
qualifiers for any other registers.
qualifier. This qualifier informs the compiler that the registers are outside of the normal
address space and that a different access method is required. You should not use this
qualifiers for any other registers.
When you write to either of these SFR variables, the compiler will use the appropriate
instruction to load the value. So, for example, to load the TRIS register, the following
code:
instruction to load the value. So, for example, to load the TRIS register, the following
code:
TRIS = 0xFF;
may be encoded by the compiler as:
MOVLW 0ffh
TRIS
Those PIC devices which have more than one output port may have definitions for
objects: TRISA, TRISB and TRISC, depending on the exact number of ports available.
This objects are used in the same manner as described above.
objects: TRISA, TRISB and TRISC, depending on the exact number of ports available.
This objects are used in the same manner as described above.