Page 124 - Programming Microcontrollers in C
P. 124

More Structures    109

                          following the colon. The first four fields are each 1 bit wide, and the
                          final field ALL is 4-bits wide. These bits can be turned on by:
                   FLAGS.OUT1 = FLAGS.OUT2 = 1;

                          or off
                   FLAGS.OUT1 = FLAGS.0UT2 = 0;

                          and they can be tested

                   if(FLAGS.PB1 == 0 && FLAGS.PB2 == 1)
                       ...

                              Some of the compilers have special bit constructs. These con­
                          structs are usually structs that have either 8- or 16-bits within the
                          field. These structs are useful as Boolean variables.
                              When setting up a microcontroller program, the programmer will
                          frequently want to have bit fields at specific locations in memory.
                          These bit fields can be used as I/O ports, control registers and even
                          arrays of bits to be used internally as flags. An approach to this prob­
                          lem is found in the bit array.

                   typedef struct
                   {
                       bit_0 :1;
                       bit_1 :1;
                       bit_2 :1;
                       bit_3 :1;
                       bit_4 :1;
                       bit_5 :1;
                       bit_6 :1;
                       bit_7 :1;
                   } BITS:

                          A macro definition is used to create a variable:
                   #define PORTA (*( BITS *) 0x1000)

                          With these definitions, instruction statements like
                   PORTA.bit_7 = 0;


                   if(PORTA.bit_3 == 1 && PORTA.bit_2 == 0)
                          etc. can be used in dealing with the bits within this memory location.
   119   120   121   122   123   124   125   126   127   128   129