Access to IO Devices
In the PIC microcontroller family, IO devices appear as memory locations.
The PIC microcontroller, with only a few notable exceptions, has no registers
or devices which are wider than 8 bits, so all devices must be accessed as
either bytes or the individual bits of which they are made.
We can access data in bytes using either the char or unsigned char data
type, and the MPLAB C18 compiler provides means to manipulate individual
bits through the use of bitfield support and header files which define the
registers in terms of their individual, named, bits. For example, we may, at
some point, which to clear a flag in a register. For the sake of the example,
let us take the INTCON register (Interrupt Control register) and a particular
flag, the INT0IF (Timer 0 Interrupt Flag, which indicates that a Timer 0
overflow has occured). We want to clear this flag.
INTCON is defined as a variable of type unsigned char in the header file
extern volatile near unsigned char INTCON;
(The header file also specifies the address of the INTCON so that we can
just treat it as a variable. Note that this uses constructs which are not ANSI
C compatible.) Without using bitfields, we would clear the INT0IF flag by
the following line of C code.
INTCON = (INTCON & 0xFD);
Note that this is a bit ugly and also a bit problematic for maintenance,
containing, as it does, a hard-coded constant. We can reduce the problem
indicated by the hard-coded constant by placing the constant in a header file
(the problem is still there, but now it’s
somebody else’s problem
, if we
aren’t put in charge of header file maintenance).
A more severe problem is that the compiler may have difficulty imple-
menting this in an efficient way. To see why this is so, consider how we’d do
this in assembly language, using the single line:
(The labels INTCON and INT0IF are defined in the header file “p18f452.inc”
which you can include using the line
near the beginning of your program, if you recall.