SFR Bitnames

When you create a Swordfish program, you typically set the device type in your code using

device = 18F452

This will link in the 18F452.bas system module, which can be found in the includes sub-folder. If you take a look at this file (either by opening it through the FILE menu or clicking on the code explorer link) then you will see a number of Special Function Registers (SFR) listed. For example,

// special function registers...
PUBLIC SYSTEM
   TOSU AS BYTE ABSOLUTE $0FFF,
   TOSH AS BYTE ABSOLUTE $0FFE
   ...

What the file does not list is individual bitnames. If your refer to the device datasheet, you will typically see something like this

 Bit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1Bit 0
INTCONGIE/GIEHPEIE/GIELTMR0IEINT0IERBIETMR0IFINT0IFRBIF

However, because the bitnames are not listed in the device system module, trying to access a bitname will generate an error. For example,

TMR0IF = 0

will generate an "Identifier not declared" error. You access a bitname, you need to create an alias

dim TMR0IF as INTCON.2

now you can use the bitname in your code.

Using Bitname Structures

For those readers familiar with the Microchip "C" compiler, you can also create bitname structures. It's more convoluted than the previous example, but much more self documenting. In addition, you can create a module of bitnames structures to reuse over and over. Here's how it's done

Structure TINTCON
   _byte As Byte
   GIE As _byte.7
   GIEH As _byte.7
   PEIE As _byte.6
   GIEL As _byte.6
   TMR0IE As _byte.5
   INT0IE As _byte.4
   RBIE As _byte.3
   TMR0IF As _byte.2
   INT0IF As _byte.1
   RBIF As _byte.0
End Structure
Public Dim INTCON As TINTCON Absolute $0FF2

The structure is called "TINTCON" and has a single byte variable called _byte. The bitnames are then aliased to _byte. The total structure size is therefore only one byte. Next, we create a variable called "INTCON" which is of type "TINTCON". It is extremely important you give the variable an absolute address. That is, where the register resides in device RAM. You can get this figure easily by looking at the Swordfish device system file. Now we can address the variable in our code like this

INTCON.TMR0IE = 1 // bit access
INTCON.TMR0IF = 0 // bit access

Remember though that when defining a 'structured' register name, the compiler will treat INTCON as a structure. If you want to assign a byte value, or perform comparison operations, you need to access the _byte part of the structure. For example,

INTCON._byte = 0 // byte access
if INTCON._byte = 0 then
   ...
endif