#INCLUDE <P16F877A.INC>
__CONFIG _XT_OSC & _WDT_OFF & _PWRTE_OFF & _BODEN_OFF & _LVP_OFF & _CPD_OFF & _WRT_OFF & _CP_OFF
#DEFINE IN1_L PORTB, 0
#DEFINE IN2_L PORTB, 1
#DEFINE IN3_R PORTB, 2
#DEFINE IN4_R PORTB, 3
#DEFINE MID 0X021
#DEFINE RIGHT 0X022
#DEFINE LEFT 0X023
CBLOCK 0x20
A1,COUNTER
ENDC
ORG 0X000
GOTO MAIN
ORG 0X004
BCF INTCON,T0IF
MOVLW -D'125'
MOVWF TMR0
DECFSZ COUNTER,F
RETFIE
MOVLW D'125'
MOVWF COUNTER
CALL TEST
RETFIE
MAIN
CALL SETUP
MOVLW D'125'
MOVWF COUNTER
MOVLW -D'125'
MOVWF TMR0
BSF INTCON, T0IE
BSF INTCON, GIE
GOTO $
A2D_CHANEL_0
MOVLW B'01000001'
MOVWF ADCON0
CALL A2D_DELAY
BSF ADCON0,GO
BTFSC ADCON0,GO
GOTO $-1
MOVF ADRESH,W
MOVWF MID
RETURN
A2D_CHANEL_1
MOVLW B'01001001'
MOVWF ADCON0
CALL A2D_DELAY
BSF ADCON0,GO
BTFSC ADCON0,GO
GOTO $-1
MOVF ADRESH,W
MOVWF LEFT
RETURN
A2D_CHANEL_3
MOVLW B'01011001'
MOVWF ADCON0
CALL A2D_DELAY
BSF ADCON0,GO
BTFSC ADCON0,GO
GOTO $-1
MOVF ADRESH,W
MOVWF RIGHT
RETURN
TEST
CALL A2D_CHANEL_0
CALL A2D_CHANEL_1
CALL A2D_CHANEL_3
MOVF MID,W
SUBWF RIGHT,1
BTFSS STATUS,C
GOTO MOTOR_RIGHT
SUBWF LEFT,1
BTFSS STATUS,C
GOTO MOTOR_LEFT
GOTO MOTOR_FORWARD
RETURN
A2D_DELAY
MOVLW D'5'
MOVWF A1
DECFSZ A1, F
GOTO $-1
RETURN
MOTOR_L_CCW
BSF IN1_L
BCF IN2_L
RETURN
MOTOR_L_CW
BCF IN1_L
BSF IN2_L
RETURN
MOTOR_L_STOP
BCF IN1_L
BCF IN2_L
RETURN
MOTOR_R_CCW
BSF IN3_R
BCF IN4_R
RETURN
MOTOR_R_CW
BCF IN3_R
BSF IN4_R
RETURN
MOTOR_R_STOP
BCF IN3_R
BCF IN4_R
RETURN
MOTOR_FORWARD
CALL MOTOR_L_CW
CALL MOTOR_R_CW
RETURN
MOTOR_RIGHT
CALL MOTOR_R_STOP
CALL MOTOR_L_CCW
RETURN
MOTOR_LEFT
CALL MOTOR_L_STOP
CALL MOTOR_R_CCW
RETURN
MOTOR_STOP
CALL MOTOR_L_STOP
CALL MOTOR_R_STOP
RETURN
SETUP
CLRF PORTA
CLRF PORTB
CLRF PORTC
CLRF PORTD
CLRF PORTE
BSF STATUS, RP0
MOVLW B'00000100'
MOVWF ADCON1
MOVLW B'00000111'
MOVWF CMCON
MOVLW B'00001011'
MOVWF TRISA
MOVLW B'00000000'
MOVWF TRISB
MOVLW B'00000000'
MOVWF TRISC
MOVLW B'00000000'
MOVWF TRISD
MOVLW B'00000000'
MOVWF TRISE
MOVLW B'11000011'
MOVWF OPTION_REG
BCF STATUS, RP0
RETURN
END