***********************************************************
     * INTEX.ASM
     * This is a sample program to demonstrate the use of
     * interrupts in a Motorola microcontroller.
     *
     * This code was written for the 68HC705P9 MCU, which has
     * 5 sources of interrupts: IRQ', SWI, Input Capture, Output
     * Compare, and Timer Overflow.
     *
     * For this example, assume that the software interrupt is
     * not used (no SWI instructions) and the IRQ' interrupt is
     * not used (tied high). We will use only the input capture
     * and output compare interrupts, which share the timer
     * interrupt vector.
     *
     * This example is intended to illustrate the programming of
     * interrupts.
     * It uses the input capture function to record the time of
     * an external event on the TCAP pin, and toggle a Port A pin.
     * The output compare function toggles the TCMP pin at a
     * given interval, producing a square wave on the TCMP pin.
     *
     * If the TCMP pin is tied directly to the TCAP pin, then
     * the falling edge of the TCMP signal (caused by every other
     * output compare interrupt) triggers an input capture interrupt.
     * The input capture interrupt records the timer value and
     * toggles a pin on I/O port A. The result is a square wave on
     * pin PA0 of half the frequency of the TCMP square wave.
     ***********************************************************

     *----------------------------------------------------------
     * Assembler equates
     *----------------------------------------------------------
     PORTA           EQU     $0000           ;Port A Data Register
     DDRA            EQU     $0004           ;Port A Data Direction
     APIN            EQU     0               ;Toggle pin on Port A
     FAULT           EQU     1              ;Fault indicator pin
     TCR             EQU     $0012           ;Timer Control Register
     ICIE            EQU     7               ;ICIE bit position in TCR
     OCIE            EQU     6               ;OCIE bit position in TCR
     TOIE            EQU     5               ;TOIE bit position in TCR
     IEDG            EQU     1               ;IC input edge bit
     OLVL            EQU     0               ;OC output level bit
     TSR             EQU     $0013           ;Timer Status Register
     ICF             EQU     7               ;ICF bit position in TSR
     OCF             EQU     6               ;OCF bit position in TSR
     ICRH            EQU     $0014           ;High byte of IC register
     ICRL            EQU     $0015           ;Low byte of IC register
     OCRH            EQU     $0016           ;High byte of IC register
     OCRL            EQU     $0017           ;Low byte of IC register
     ATRH            EQU     $001A           ;Alternate timer register
     ATRL            EQU     $001B

     *----------------------------------------------------------
     * RAM variables
     *----------------------------------------------------------
                     ORG     $0080           ;Start of RAM
     InClock         RMB     2               ;Clock value for IC event
     OutDelay        RMB     1               ;Delay value for OC event

     *----------------------------------------------------------
     * Start of program code
     *----------------------------------------------------------
                     ORG     $0100           ;Start of user EPROM
     Startup         BSET    OCIE,TCR        ;Enable OC interrupts
                     BSET    ICIE,TCR        ;Enable IC interrupts
                     BCLR    TOIE,TCR        ;Disable TO interrupts

     * Setup Pin Toggle function
                     BSET    APIN,DDRA       ;Set toggle pin as output
                     BCLR     APIN,PORTA     ;Start pin low

     * Setup Input Capture function to trigger on falling edge
                     BCLR    IEDG,TCR        ;Set IEDG bit (go low)

     * Setup Output Compare function
                     BCLR    OLVL,TCR        ;Clear OLVL bit (go low)
                     LDA     #$FF            ;Setup delay variable
                     STA     OutDelay
                     LDX     ATRH            ;Load current timer MSB
                     LDA     ATRL            ;Load current timer LSB
                     ADD     OutDelay        ;Add delay
                     BCC     Continue        ;Check for carry
                     INCX                    ;Compensate for carry
     Continue        STA     OCRL            ;Record OC value
                     STX     OCRH

     * Enable interrupts globally
                     CLI                     ;Start interrupt service

     *----------------------------------------------------------
     * Main loop; wait for interrupts
     *----------------------------------------------------------
     Loop            BRA     Loop

     *----------------------------------------------------------
     * Timer interrupt service routine
     * Because either an input capture or output compare interrupt
     * will be vectored to this routine, the software must
     * determine the source of the interrupt.
     *----------------------------------------------------------
     TimerInt        BRSET   OCF,TSR,OCInt   ;Check the OC flag

     * If the OCF is clear,
     * an input capture happened, so record the time of the event
     ICInt           LDA     PORTA           ;Toggle the port pin
                     EOR     #$01
                     STA     PORTA
                     LDA     ICRH            ;Record high byte
                     STA     InClock
                     LDA     ICRL            ;Record low byte
                     STA     InClock+1
                     LDX     TSR             ;Ensure clear ICF bit
                     RTI                     ;End of interrupt

     * If we reach this point, an output compare event happened
     * Toggle the OLVL bit for the next compare, and add new
     * compare value
     OCInt           LDA     TCR             ;Load control register
                     EOR     #$01            ;Toggle the OLVL bit
                     STA     TCR             ;Setup new OLVL value
                     LDA     OCRL            ;Get time of compare
                     ADD     OutDelay        ;Add delay value
                     BCC     NoCarry
                     INC     OCRH            ;Compensate for carry
     NoCarry         LDX     TSR             ;Ensure clear OCF bit
                     STA     OCRL
                     RTI                     ;End of interrupt

     *----------------------------------------------------------
     * One routine for unused interrupt vectors; return 
     *----------------------------------------------------------
     UnusedInt       RTI                     ;Simply return


     *----------------------------------------------------------
     * Another routine for unused interrupt vectors; toggle I/O
     *----------------------------------------------------------
     UnusedFault        BSET    FAULT,PORTA   ;Toggle fault pin
                        BCLR    FAULT,PORTA  
                        RTI

     *----------------------------------------------------------
     * Vector Definitions
     * address all vectors, used and unused
     *----------------------------------------------------------
                     ORG     $1FF8           ;Timer vector
                     FDB     TimerInt        ;Timer interrupt routine
                     ORG     $1FFA           ;IRQ' vector
                     FDB     UnusedInt       ;IRQ' interrupt not used
                     ORG     $1FFC           ;SWI vector
                     FDB     UnusedInt       ;SWI not used
                     ORG     $1FFE           ;Reset vector
                     FDB     Startup         ;Start of program flow

     ******************************************************************************
     *                                                                            *
     * Motorola reserves the right to make changes without further notice to any  *
     * product herein to improve reliability, function, or design. Motorola does  *
     * not assume any liability arising out of the application or use of any      *
     * product, circuit, or software described herein; neither does it conveyany *
     * license under its patent rights nor the rights of others. Motorola         *
     * products are not designed, intended, or authorized for use as components   *
     * in systems intended for surgical implant into the body, or other           *
     * applications intended to support life, or for any other application in     *
     * which the failure of the Motorola product could create a situation where   *
     * personal injury or death may occur. Should Buyer purchase or use Motorola  *
     * products for any such intended or unauthorized application, Buyer shall    *
     * indemnify and hold Motorola and its officers, employees, subsidiaries,     *
     * affiliatees, and distributors harmless against all claims, costs, damages, *
     * and expenses, and reasonable attorney fees arising out of, directly or     *
     * indirectly, any claim of personal injury or death associated with such     *
     * unintended or unauthorized use, even if such claim alleges that Motorola   *
     * was negligent regarding the design or manufacture of the part. Motorola    *
     * and the Motorola Logo are registered trademarks of Motorola Inc.           *
     *                                                                            *
     ******************************************************************************