	org 	8000h
        jmp     8200h

	$MOD51GB

PWM_MOT EQU     R0
PWM_STR EQU     R1
STP_GO  EQU     P5.1
FOR_REV EQU     P5.0 


        org     8200h
main:
        ;enable A/D operation
        mov     ACON,#010h   

        ;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
IRScan:   call    lngDelay

L_Rear: clr     C             ; Clear carry bit
        mov     A,AD1         ; get the left rear IR
        subb    A,#100        ; subtract threshold from value
        jc      L_Front       ; jump if value is less than threshold

        jnb     STP_GO,stp_rev2  ;already stopped
        jnb     FOR_REV,stp_rev2 ;or going forward
        clr     STP_GO           ;stop motor
        clr     FOR_REV          ;place in forward
        mov     PWM_STR,#03bh    ;center wheels
        mov     CCAP1H,PWM_STR   ;
        call    lngDelay
        mov     PWM_MOT,#0C0h           ;set motor for 1/8 speed
        mov     CCAP2h,PWM_MOT
        setb    STP_GO           ;start motor
        call    PRINT
        db      'Forward\n',0,
stp_rev2:
        jmp     IRScan


L_Front:
        clr     C             ; Clear carry bit
        mov     A,AD2         ; get the left front IR
        subb    A,#110        ; subtract threshold from value
        jc      R_Front       ; jump if value is less than threshold

        jmp     forward

R_Front:
        clr     C             ; Clear carry bit
        mov     A,AD3         ; get the left front IR
        subb    A,#110        ; subtract threshold from value
        jc      IRScan        ; jump if value is less than threshold

forward:
        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
        mov     PWM_MOT,#099h           ;set motor for 1/8 speed
        mov     CCAP2h,PWM_MOT
        setb    STP_GO                  ;start motor
        call    PRINT
        db      'Backing\n',0,
stp_rev0:
        jmp     IRScan

wait:   jmp     wait

	

        ;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
