* Filename      : BUMP.ASM
* Programmer    : Michael Hattermann
* Date          : February 21, 2002
* Version       : 1.0
* Description   : This file contains bump sensor functions.
*                 The following functions are available:
*
*                   INITBUMP - Initializes bump sensors
*                   BUMPED - Determines if a bump has occured
*                   BUMPREACT - Determines reaction if a bump has occurred
*
*
#define __DEBUGBUMP_     1

#include "hc12.asm"

*
************************************************************
* A/D Debug Code
************************************************************
*
#ifdef __DEBUGBUMP_
            ORG     USERPROG_PVECT
            JMP     TEST

            ORG     $A000

TEST        LDAA    #$00            ; turn off COP watchdog timer
            STAA    COPCTL

            LDS     #$0bff          ; init the stack pointer

            JSR     INITATD         ; init A/D system
            JSR     INITSCI         ; init SCI system

TEST1       LDX     #100            ; wait 1/10 sec
            JSR     WAIT            ;
            JSR     BUMPED          ; check bumpers
            TBEQ    A,TEST1         ; if bumper not pressed, keep checking
            LDX     #BUMPSTR        ; print bump string
            JSR     OUTSTR          ;
            JSR     OUTNUM          ; print analog value
            LDX     #NEWLINE        ; print end of line
            JSR     OUTSTR          ;
            BRA     TEST1           ;

BUMPSTR     DC.B    'BUMPER VALUE = '
            DC.B    EOS
NEWLINE     DC.B    CR,LF,EOS

#include "wait.asm"
#include "sci.asm"
#include "atd.asm"

#endif

* Reaction table for values for center IR
************************************************************
BUMPTBL     DC.B        NOACTION        ; $00-$07
            DC.B        NOACTION        ; $08-$0F
            DC.B        NOACTION        ; $10-$17
            DC.B        NOACTION        ; $18-$1F
            DC.B        NOACTION        ; $20-$27
            DC.B        NOACTION        ; $28-$2F
            DC.B        NOACTION        ; $30-$37
            DC.B        NOACTION        ; $38-$3F
            DC.B        NOACTION        ; $40-$47
            DC.B        NOACTION        ; $48-$4F
            DC.B        NOACTION        ; $50-$57
            DC.B        NOACTION        ; $58-$5F
            DC.B        NOACTION        ; $60-$67
            DC.B        NOACTION        ; $68-$6F
            DC.B        NOACTION        ; $70-$77
            DC.B        NOACTION        ; $78-$7F
            DC.B        NOACTION        ; $80-$87
            DC.B        NOACTION        ; $88-$8F
            DC.B        NOACTION        ; $90-$97
            DC.B        NOACTION        ; $98-$9F
            DC.B        NOACTION        ; $A0-$A7
            DC.B        NOACTION        ; $A8-$AF
            DC.B        NOACTION        ; $B0-$B7
            DC.B        NOACTION        ; $B8-$BF
            DC.B        NOACTION        ; $C0-$C7
            DC.B        NOACTION        ; $C8-$CF
            DC.B        NOACTION        ; $D0-$D7
            DC.B        NOACTION        ; $D8-$DF
            DC.B        NOACTION        ; $E0-$E7
            DC.B        NOACTION        ; $E8-$EF
            DC.B        NOACTION        ; $F0-$F7
            DC.B        NOACTION        ; $F8-$FF

*
*******************************************************************************
*                       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    #BUMPER             ; get data from bumper
            JSR     ANALOG              ; get the analog value of the port
            RTS                         ; return to caller
*
*******************************************************************************
*                       SUBROUTINE -  BUMPREACT
* 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        : Reaction in reg A.
* Destroys      : Reg A.
* Calls         : None.
*******************************************************************************
*
BUMPREACT   PSHX                        ; save register X
            LDX     #BUMPTBL            ; load address of bump reaction table
            JSR     BUMPED              ; get bumper value
            LSRA                        ; convert bumper value
            LSRA                        ;   into an index for
            LSRA                        ;   reaction table
            LDAA    A,X                 ; get reaction from table
            PULX                        ; restore register X
            RTS                         ; return to caller
*
*******************************************************************************
