Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- list p=18F1220 ; processor type
- radix hex ; default radix for data
- ; Disable Watchdog timer, Low V. Prog, and RA6 as a clock
- config WDT=OFF, LVP=OFF, OSC = INTIO2
- #include p18f1220.inc
- #define lastL 0x80 ; Last L Sensor Value
- #define loopCount 0x81 ; Timer Loop Count
- #define countL 0x82 ; Count the cycles we have had echoL on
- #define countOD 0x83 ; Count for outer delay loop
- #define countID 0x84 ; Count for inner delay loop
- ;these are shortcuts, string replacements
- #define _TrigL PORTA,RA1
- #define _TrigR PORTA,RA4
- #define _EchoL PORTA,RA0
- org 0x000 ; Executes after reset, equivalent to org
- GOTO StartL
- org 0x008 ; Executes after high priority interrupt
- GOTO HPRIO
- org 0x020 ; Start of the code
- HPRIO:
- BTFSC PIR1, TMR2IF ; high priority loop
- BRA iLoop
- RETFIE ; return from interrupt
- iLoop:
- INCF loopCount
- MOVLW .120
- CPFSLT loopCount
- BRA doTrigger ; trigger every 30,000 uSec.
- MOVLW .1
- CPFSGT loopCount
- BRA stopTrigger
- ; we didn't trigger so update
- BRA updateSensor
- doTrigger:
- CLRF loopCount
- BRA doTriggerL
- doTriggerL:
- MOVFF countL,lastL
- ; we should check to see if echo is high and kill trigger if that's the case.
- BTFSC _EchoL
- BRA killL
- continueL:
- BSF _TrigL ; Set Left trigger on
- CLRF countL ; clear count of eccho
- BRA loopDone
- killL: ; Sensors is known to hang whne when no object is found within its
- ; Measurement range - Noise is known to reset the sensor.
- ; So here, we are using the left sensor to reset right sensor.
- ; Sensors work best with 4.5-5.5 v supply voltage.
- BSF _TrigR ; start trigger or right sensor
- MOVLW .1 ; 1 millisecond
- CALL Delay
- BCF _TrigR ; Clear right trigger on
- MOVLW .1 ; 1 millisecond
- CALL Delay
- ; If Echo is not cleared then try to reset it again
- BTFSS _EchoL
- BRA continueL
- BRA killL
- stopTrigger:
- BCF _TrigL ; Set Left trigger off
- BRA loopDone
- updateSensor:
- ;increment count for each cycle echo is on
- btfsc _EchoL
- incf countL
- bra loopDone
- loopDone:
- bcf PIR1, TMR2IF ; Clear Timer 2 interrupt Flag
- bra HPRIO ; Go to start and service any pending Interrupt
- StartL:
- ; Initialize all I/O ports per EDbot Specifications
- MOVLW 0x7F
- MOVWF ADCON1 ; Set all Port A Pins as digital
- CLRF PORTA ; Initialize PORTA
- CLRF PORTB ; Initialize PORTB
- MOVLW 0x0D
- MOVWF TRISA ; Set Port A direction
- MOVLW 0xC7
- MOVWF TRISB ; Set Port B direction
- MOVLW 0x60
- IORWF OSCCON ; Set to 4mhz
- ; Clear Sensor related counter
- CLRF lastL
- CLRF loopCount
- BSF INTCON, PEIE ; enable peripheral interrupts
- ; Enable Timer2 Interrupat as high priority
- BSF PIE1, TMR2IE
- BSF IPR1, TMR2IP
- CLRF TMR2
- CLRF T2CON ; Timer 2 is set to 8-bit with no scaling
- MOVLW 0xFA ; Timer 2 is set to interrupt in 250 uSec.
- MOVWF PR2
- BSF T2CON,TMR2ON ; enable TMR2
- BSF INTCON, GIE ; enable interrupts globally
- BSF PORTB, 3 ;enable right motor
- BSF PORTB, 4 ;enable left motor
- Mloop:
- BCF PORTB,RB5 ; turn off LED
- MOVLW .6 ; 10 inch stopping distance
- CPFSGT lastL ; skip if LastL > wreg
- CALL Motors
- BRA Mloop
- ;Function to delay for Wreg miliseconds
- Motors:
- BSF PORTB,5
- BCF PORTA,6
- BCF PORTA,7
- MOVLW .10
- CALL Delay
- BSF PORTA,6
- CALL Delay
- BSF PORTA,7
- RETURN
- Delay:
- MOVWF countOD
- DelayOL: ; delay Outer loop
- CLRF countID
- DelayIL: ; Delay Inner Loop
- NOP
- INCF countID
- BNZ DelayIL
- DECF countOD
- BNZ DelayOL
- RETURN ; end delay function
- end ; end of code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement