8.2 Defining And Using Bits

The interrupt routine shown in this chapter modifies a flag that is used by main-line code to set the state of the LED. The flag's state is set by the ISR. As this flag only needs to hold a true or false value, it was defined as a bit object to save on storage space and make checking its contents more efficient.

Bit objects for PIC18 devices are created as per Mid-range devices (discussed in section Defining And Using Bits), using the bit psect flag with the psect that holds them. The only difference to the following code extract compared to the that for the Mid-range device is that the psect was placed in the Access bank.
PSECT bitbssCOMMON,bit,class=COMRAM,space=1
LEDState:
    DS          1             ;a single bit used to hold the required LED state

Bit symbols are used in PIC18 code in the same way as for Mid-range devices. The file register address of a bit symbol can be obtained by dividing the bit address by 8, and the bit position can be obtained by taking the bitwise AND with 7. And as with Mid-range devices, once you include <xc.inc> into your program, bit symbols are defined for bits within special function registers, such as TMR0IF and GIEH, which were used in the example code.

You may notice in the example, repeated below, that the IPEN bit in the INTCON0 register was not set using an instruction like bsf IPEN, but rather, the operand to this instruction was specified as the INTCON0 file register address and a special symbol used to locate the IPEN bit within this register.
PSECT code
start:
    bsf         BANKMASK(INTCON0),INTCON0_IPEN_POSN,c	    ;set IPEN bit

In this particular case, attempting to use the IPEN bit directly would result in an undefined symbol error. This is because the 18F47K42 device has more than one bit within its SFRs called IPEN. The other is the IPEN bit in the ADCON1 register. In such instances, the name IPEN is not unique and no bit symbol will be available in the provided headers. You must access the bit using the SFR name, but the headers do provide macros that represent the bit position, such as the INTCON0_IPEN_POSN, used above. The same headers also supply macros which indicate the number of bits in the SFR, e.g., _INTCON0_IPEN_SIZE and a mask you can use for bitwise logic operations, e.g., _INTCON0_IPEN_MASK. These macros are available for every SFR bit, and you can use them in preference to the bit name if you prefer.