	org 	8000h
        jmp     8200h

	$MOD51GB

PWM_MOT EQU     R0
PWM_STR EQU     R1
STP_GO  EQU     P5.1
FOR_REV EQU     P5.0 

        org     8003h
        jmp     INT0_ISR

        org     8013h
        jmp     INT1_ISR

        org     8053h
        jmp     INT2_ISR


        org     8200h
main:
        ;initialize the external interrupts for InfraRed
        setb    IT0             ;set INT0 to trigger on falling edge
        setb    IT1             ;set INT1 to trigger on falling edge
        mov     A,#00           ;clr INT2 to trigger on falling edge
        mov     EXICON,A
        setb    P5.2
        setb    EX0             ;enable external interrupt 0
        setb    EX1             ;enable external interrupt 1
        mov     A,#04h          ;load Acc with value
        mov     IEA,A           ;to set the EX2 external interrupt 2

        ;STP_GO is cleared to stop motor
        ;FOR_REV is set to go in reverse

        ;initalize the motor and steering PWM waveforms
        mov     P1,#0FFH        ;must set all bits on port1 to 0
        mov     PWM_MOT,#0C0h   ;set motor for 1/8 speed
        mov     CCAP2h,PWM_MOT
        mov     PWM_STR,#03Bh   ;set steering servo to center
        mov     CCAP1h,PWM_STR
        clr     FOR_REV           ;set relays for FORWARD
        clr     STP_GO          ;block PWM signal to motor
        call    PCA_INIT


        setb    STP_GO
        setb    EA      ;enable interrupts

wait:   jmp     wait

	
INT0_ISR:
        jnb     STP_GO,stp_rev0         ;already stopped
        jb      FOR_REV,stp_rev0        ;or backing
        clr     STP_GO                  ;stop motor
        setb    FOR_REV                 ;reverse it
        mov     PWM_STR,#031h           ;turn wheels to right
        mov     CCAP1H,PWM_STR          ;
;        call    lngDelay                ;Give it 1/2 a second
        setb    STP_GO                  ;start motor
        call    PRINT
        db      'Backing\n',0,
stp_rev0:
        reti

INT1_ISR:
        jnb     STP_GO,stp_rev1 ;already stopped
        jb      FOR_REV,stp_rev1  ;or backing
        clr     STP_GO          ;stop motor
        setb    FOR_REV           ;reverse it
        mov     PWM_STR,#031h   ;turn wheels to right
        mov     CCAP1H,PWM_STR  ;
;        call    lngDelay
        setb    STP_GO          ;start motor
        call    PRINT
        db      'Backing\n',0,        
stp_rev1:
        reti

INT2_ISR:
        jnb     STP_GO,stp_rev2 ;already stopped
        jnb     FOR_REV,stp_rev2 ;or going forward
        clr     STP_GO          ;stop motor
        clr     FOR_REV           ;reverse it
        mov     PWM_STR,#03bh   ;center wheels
        mov     CCAP1H,PWM_STR  ;
;        call    lngDelay
        setb    STP_GO          ;start motor
        call    PRINT
        db      'Forward\n',0,
stp_rev2:
        reti

        ;initializes the programmable counter array for the motor
        ;and the steering servo
PCA_INIT:
        mov     CMOD,#006h      ;set PCA frequency to external
        mov     CCAPM1,#42h     ;put PCA module 1 into PWM mode
        mov     CCAPM2,#42h     ;put PCA module 0 into PWM mode
        setb    CR              ;start the PCA clock
        ret

lngDelay:
        $INCLUDE(lngDelay.inc)

cout:
        $INCLUDE(cout.inc)

PRINT:
        $INCLUDE(PRINT.inc)

SEND_BYTE:
        $INCLUDE(SEND_BYT.inc)



	end
