Page 232 - Programming Microcontrollers in C
P. 232

Header File    217

                   .
                   .
                   PORTA.MOTOR = ON; /* turn the motor on */
                   .
                   .
                   .
                   if(PORTA.PUSH_BUTTON==ON)
                   {
                          do push button things
                   }

                              With this compiler, an int is a 16-bit value. Therefore, the
                          registers that are two bytes are cast onto the type int. Usually these
                          registers are accessed as ints only and there is no need to have the
                          individual bit access afforded by the use of the Register type.
                          The timer counter register (TCNT) is one such register that is accessed
                          as an int only. There are also a few one byte, or 8-bit, registers that
                          are accessed as bytes only. No bit accesses within these registers are
                          needed. In these cases, the register is cast onto the type char. The
                          several ADRx registers are examples of this type. The ADRx registers
                          contain the result of an analog-to-digital conversion that is usually
                          handled as an 8-bit unit only.
                              In most instances, register locations should be unsigned. The ADRx
                          registers each contain the result of analog-to-digital conversions. These
                          results are all unsigned. Therefore, these registers should be cast as
                          unsignedchar. Also note that all of the timer count and input capture
                          or output compare registers are also declared as unsigned.
                              In the listing above, you will note that there are two parts to the
                          declaration of each register. The first identifies all of the bits in the
                          register through a structure typedef. Then a macro definition of the
                          port name causes each instance of the port name in the program to be
                          replaced by the dereferenced value of the register address cast onto a
                          pointer of the correct type. The port name is the name of the port
                          found in the data manual, and the bit names given the bits in the structure
                          are the names found in the data manual. Therefore, if you wish to set
                          the bit named HNDS found in the register PIOC, you need to use

                   PIOC.HDNS=ON;
                              There are two register locations that work differently from the
                          rest. These are the two flag registers whose bits are set by the
   227   228   229   230   231   232   233   234   235   236   237