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

I! .5 Gadfly Synchronization                                        327

              LDAB #$80             ; Put one in leftmost bit of accumulator B
              STAB   TSCR           ; Put it into the control register TSCR
              LDD    #$201          ; Generate 2 in Accumulator A, 1 in Accumulator B
              STAA   $8B            ; Write 2 in rightmost bit of control register TCTLE
              STAB   $8E            ; Write one in rightmost bit of control register TFLG1

            In place of a delay loop, the gadfly loop itself is used whenever input or output is
        done: do ; while{(TFLG1 & 1) == 0); TFLG1 = 1;. The loop waits until an edge
        sets the flag bit, and the next statement clears the flag bit. This is optimally compiled to
        assembly language as

        L:    BRCLR $8E,#1,L        ; Wait for one in TFLG1 bit 0
              LDAB #1               ; Put one in rightmost bit of accumulator B
              STAB   $8E            ; Write it into control register TFLG1

            We illustrate the use of gadfly synchronization for counting pulses from a Geiger
        counter. Each time a pulse occurs, PORTT bit 0 sees a rising edge. To count the pulses,
        execute the C procedure:
        void main() { int i = 0;
            TSCR = 0x80; TFLG1 = 1;TCTLE = 1;
            do {while({TFLG1 & 1) == 0); TFLG1 = 1; i++; }while(1);
         }

         An optimized assembly language program segment for the body of the C procedure is
                LDAB     #$80       ; Put one in leftmost bit of accumulator B
                STAB     $86        ; Write it into control register TSCR
                LDD      #$102      ; Generate one in Accumulator A, 2 in Accumulator B
                STAA     $ 8E       ; Write one in rightmost bit of control register TFLG1
                STAB     $ 8B       ; Write 2 in rightmost bit of control register TCTLE
                CLRA                ; Clear msbyte of local variable i
                CLRB                ; Clear next byte of i
        LOOP: BRCLR      $ 8E, #1, LOOP ; Wait for one in TFLG1 bit 0
                MOVE    #1, $ 8E    ; Write 1 into control register TFLG1 to clear bit 0
               ADDD     #1          ; Increment count
                BRA      LOOP       ; Loop forever
         You can stop this program after it has counted the pulses and examine accumulator D.
   345   346   347   348   349   350   351   352   353   354   355