Page 231 - Programming Microcontrollers in C
P. 231
216 Chapter 5 Programming Large 8-Bit Systems
{
signed char bit 0 :1;
. . .
unsigned char bit 7 :1;
} Register;
We can now declare:
typedef union
{
char byte;
Register bits;
} Mix_Register;
and thus
#define PORTA(*volatile
Mix_Register*)(Register_Set +0)
will allow the programmer to use
PORTA.byte = 0x2E
to set the value of all bits in the port with one instruction and in the
same program to use
PORTA.bits.bit3 = 1;
to set, reset, or test the individual bits inside of the port. In this book,
we will use the bit fields only as shown in header file.
Since PORTA is of the type Register, it is possible to deal
with the individual bits within this location by normal C constructs
such as
PORTA.bit3 = 1;
if(PORTA.bit6 == 0)
...
Of course, it is much better to define practical names to the various
bits within the port to achieve even clearer code:
#define ON TRUE
#define MOTOR bit3
#define PUSH_BUTTON bit6
.