Page 301 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 301

278                                 Chapter 9 Implementation of C Procedures


            struct spiDevice {
                unsigned int spie:l,spe:l,swom:l,mstr:l f cpol:l r cpha:l,ssoeil,
                   lsbf:l;
            } *spiPtr = (struct spiDevice *)OxdO;
            #define spi (*spiPtr)

            raain() { spi.spe = 1; do ; while(spi.spe); }
                                     a. A C Program

              0000095B 3B               PSHD
              0000095C FE0800           LDX     spiPtr
              0000095F 6E80             STX 0,SP
              00000961 OC0040           BSET 0,X,#64
              00000964 EE80             LDX 0,SP
              00000966 OE0040FA         BRSET 0,X,#64,*-6        ;abs = 0964
              0000096A 30               PULX
              0000096B 3D               RTS
                         b. Assembly Language developed from Part (a)

                        Figure 9.13. A Program Setting and Testing a Bit

            The do while statement in Figure 9.13a tests the condition after the loop is
        executed at least once, but it tests the result of the loop's activities. This is very useful
        in I/O software because it lets you get data within the statement and test the result in the
        conditional expression, which is not executed until the statement is executed at least
        once. See Figure 9.13. The program sets a bit of a struct and tests it repeatedly until
        it is cleared (by hardware). It is compiled into assembly language shown in Figure 9.13b.
        The struct definition shown below merely defines accesses using the pointer spiPtr.
        The elements can be accessed using "arrow" notation. For instance the bit spe can be
        set using spiPtr->spe = 1; However, by declaring #define spi (*spiPtr), the
        expression spi. spe = 1; can be used instead. This is encoded into assembly language
        using the BSET instruction:

             BSET 0, X, #6 4       ; set bit 6 of location pointed to by X (the spi port)

        The statement do ; while (spi. spe); is implemented with

             BRSET 0, X, #6 4, * - 6 ; wait while bit 6 of location OxDO is 1

        Generally, if the bitfield is more than one bit, data to be inserted will have to be shifted
        to the correct bit position, and masked parts of it are ORed with masked parts of bits in
        other fields, to be written into the memory. This code looks like the code for statement
        lui = (lui « 3) + (lui « 1) + Isc -'0' ; that we studied at the end of
        Section 9.2. Data read from such a bitfield will have to be shifted and masked in like
        manner.
   296   297   298   299   300   301   302   303   304   305   306