* Filename      : WAIT.ASM
* Programmer    : Michael Hattermann
* Date          : March 29, 2002
* Version       : 1.0
* Description   : This file contains the wait and bumper
*                 functions. They must be together because
*                 the bumpers are checked while waiting. The
*                 following functions are available:
*
*                   WAIT - waits for specified # ms
*                   BUMPED - Determines if a bump has occured
*
*#define __DEBUGBUMP_    1
#define __PRINTBUMP_    1

#include "hc12.asm"
*
************************************************************
* Bump/Wait Equates
************************************************************
*
NOBUMPMAX       EQU     $09             ; max value for no bumper pressed
FRONTREAR       EQU     $47             ; division between front and back

*
************************************************************
* Bump/Wait Debug Code
************************************************************
*
#ifdef __DEBUGBUMP_
            ORG     USERPROG_PVECT
            JMP     TEST

            ORG     $0900
BUMPVALUE   DC.B    $00             ; bumper value

            ORG     $B000

TEST        LDAA    #$00            ; turn off COP watchdog timer
            STAA    COPCTL

            LDS     #$0bff          ; init the stack pointer

            LDX     #1000           ;
            JSR     WAIT            ;

            JSR     INITATD         ; init A/D system
            JSR     INITSCI         ; init SCI system
            MOVB    #$80,TSCR       ; enable the timer

TEST1       LDX     #500            ; wait 1/2 sec
            JSR     WAIT            ;
            LDAA    BUMPVALUE       ; get bumper value
            TBEQ    A,TEST1         ; if bumper not pressed, keep checking

            BRA     TEST1           ;

TEST2       LDX     #1              ; wait 1ms to time wait routine
            LDY     TCNT            ; get start value of timer
            JSR     WAIT            ; wait
            LDX     TCNT            ; get end value of timer
            JSR     OUTADDR         ; print start value
            LDAA    #$20            ; print blank space
            JSR     OUTCHAR         ;
            TFR     Y,X             ; get end value
            JSR     OUTADDR         ; print end value
            LDX     #NEWLINE        ; print end of line
            JSR     OUTSTR          ;

            BRA     TEST1           ;

NEWLINE     DC.B    CR,LF,EOS

#include "sci.asm"
#include "atd.asm"

#endif

#ifdef __PRINTBUMP_
BUMPSTR     DC.B    'BUMPER VALUE = '
            DC.B    EOS
#endif

*
*******************************************************************************
*                       SUBROUTINE -  WAIT
* Description: Waits for the designated amount of time (in ms).  If a bumper is
*               pressed while waiting, it will quit waiting and returns to the
*               function that called it. Function returns 0 if waited full time,
*               returns bumper value otherwise
* Input         : # of ms to wait in reg X.
* Output        : Bumper value in BUMPVALUE.
* Destroys      : None.
* Calls         : None.
*******************************************************************************
*
WAIT        TBEQ    X,WAITX         ; if no time to wait, get out
            PSHX                    ; save reg X
            PSHD                    ; save reg D
            MOVB    #$00,BUMPVALUE  ; clear old bumper value

WAIT1       LDAB    #11             ; load loop counter

WAIT2       LDAA    #BUMPER         ; check to see if we have
            JSR     ANALOG          ;   been bumped
            CMPA    #NOBUMPMAX      ; were we bumped?
            BHI     WAITBX          ; yes, get out
            NOP                     ; do nothing
            NOP                     ;
            NOP                     ;
            NOP                     ;
            NOP                     ;
            NOP                     ;
            NOP                     ;
            NOP                     ;
            NOP                     ;
            DBNE    B,WAIT2         ; repeat until counter=0

            DBNE    X,WAIT1         ; if we need to wait more, go wait

WAITX       PULD                    ; restore reg D
            PULX                    ; restore reg X
            RTS                     ; return to caller
WAITBX      STAA    BUMPVALUE       ; save bumper value

#ifdef __PRINTBUMP_
            PSHX                    ; save reg X
            LDX     #BUMPSTR        ; print bump string
            JSR     OUTSTR          ;
            JSR     OUTNUM          ; print analog value
            LDX     #NEWLINE        ; print end of line
            JSR     OUTSTR          ;
            PULX                    ; restore reg X
#endif

            BRA     WAITX           ; get out

*******************************************************************************
*                       SUBROUTINE -  BUMPED
* Description: Determines if a bump sensor has been pressed.  If it has, the
*               function will return the value read from the bumper A/D port.
*               If no bumper is pressed, a $00 will be returned.
* Input         : None.
* Output        : Bumper value in reg A.
* Destroys      : Reg A.
* Calls         : None.
*******************************************************************************
*
BUMPED      LDAA    BUMPVALUE           ; get the last bump value
            RTS                         ; return to caller
*
*******************************************************************************
*                       SUBROUTINE -  WAIT
* Description: Waits for the designated amount of time (in ms).
* Input         : # of ms to wait in reg X.
* Output        : None.
* Destroys      : None.
* Calls         : None.
*******************************************************************************
*
*WAIT        TBEQ    X,WAITX         ; if no time to wait, get out
*            PSHX                    ; save reg X
*            PSHD                    ; save reg D
*
*WAIT1       LDD    #1323            ; load loop counter
*
*WAIT2
*            DBNE    D,WAIT2         ; repeat until counter=0
*
*            DBNE    X,WAIT1         ; if we need to wait more, go wait
*
*WAITX       PULD                    ; restore reg D
*            PULX                    ; restore reg X
*            RTS                     ; return to caller
*
*******************************************************************************
