list p=16F84A ; list directive to define processor #include <p16F84A.inc> ; processor specific variable definitions __CONFIG _CP_OFF & _WDT_ON & _PWRTE_ON & _XT_OSC ;********************** Contact Assignment ***************************** ; Contact # Name Function ; 17 RA0 output TrigLeft ; 18 RA1 output TrigRight ; 01 RA2 input EchoLeft ; 02 RA3 input EchoRight ; 03 RA4 output GenRed ; 06 RB0 output RedLeft ; 07 RB1 output YellowLeft ; 08 RB2 output GreenLeft ; 09 RB3 output RedRight ; 10 RB4 output YellowRight ; 11 RB5 output GreenRight ; 12 RB6 output GenYellow ; 13 RB7 output GenGreen ; ;******************* Constants ************************** ; Red 00000001 Left channel ; Yellow 00000010 Left channel ; Green 00000100 Left channel ; ; Red 00001000 Right channel ; Yellow 00010000 Right channel ; Green 00100000 Right channel ;***** VARIABLE DEFINITIONS ***************************** CBLOCK 0x0C Reg_1 ; 0x0C use within Pause subroutine Reg_2 ; 0x0D use within Pause subroutine Reg_3 ; 0x0E use within Pause subroutine ENDC #define bank0 bcf STATUS,RP0 ; set bank0 #define bank1 bsf STATUS,RP0 ; set bank1 #define TrigLeft_1 bsf PORTA,0 ; set Trigleft #define TrigLeft_0 bcf PORTA,0 ; clean Trigleft #define TrigRight_1 bsf PORTA,1 ; set Trigright #define TrigRight_0 bcf PORTA,1 ; clean Trigright #define EchoLeft PORTA,2 #define EchoRight PORTA,3 #define RedLeft b'00000001 #define YellowLeft b'00000010 #define GreenLeft b'00000100 #define RedRight b'00001000 #define YellowRight b'00010000 #define GreenRight b'00100000 ;********************************************************************** ORG 0x000 ; processor reset vector goto main ; go to beginning of program ;---------------------------------------------------------------------- ;------------------ SUBROUTINE Pulse -------- ;delay = 10 machine cycles = 10 µs Pulse movlw .3 movwf Reg_1 wr0 decfsz Reg_1,F goto wr0 return ;------------------ SUBROUTINE Pause 1ms -------- ;delay = 1'000 machine cycles Pause movlw .75 movwf Reg_1 movlw .3 movwf Reg_2 wr1 decfsz Reg_1,F goto wr1 clrwdt decfsz Reg_2,F goto wr1 nop return ;-------------------- SUBROUTINE Pause 1sec --- ;delay = 1'000'000 machine cycles Pause1 movlw .254 movwf Reg_1 movlw .17 movwf Reg_2 movlw .6 movwf Reg_3 wr2 decfsz Reg_1,F goto wr2 clrwdt decfsz Reg_2,F goto wr2 decfsz Reg_3,F goto wr2 nop nop return ;-------------------- SUBROUTINE Pause 50ms -------- ;delay = 50'000 machine cycles Pause2 movlw .216 movwf Reg_1 movlw .65 movwf Reg_2 wr3 decfsz Reg_1,F goto wr3 clrwdt decfsz Reg_2,F goto wr3 return ;--------------------- SUBROUTINE General Red --------------- GenRed bsf PORTA,4 bcf PORTB,6 bcf PORTB,7 return ;--------------------- SUBROUTINE General Green ------------- GenGreen bsf PORTB,7 bcf PORTA,4 bcf PORTB,6 return ;--------------------- SUBROUTINE General Yellow ------------ GenYellow bsf PORTB,6 bcf PORTA,4 bcf PORTB,7 return ;--------------------- SUBROUTINE General All ------------ GenAll bsf PORTA,4 bsf PORTB,6 bsf PORTB,7 return ;--------------------------------------------------------- main bank1 ; set Bank 1 movlw 0x00 movwf TRISB ; PORTB - out movlw b'00001100 movwf TRISA ; RA2,RA3 - input; RA0,RA1,RA4 - output bank0 ; return to Bank 0 clrf PORTA clrf PORTB ; turn off all lights ;---------------------- TEST ----------------------------- call GenRed call Pause1 call GenYellow call Pause1 call GenGreen call Pause1 bcf PORTA,4 bcf PORTB,6 bcf PORTB,7 call Pause1 call GenAll call Pause1 bcf PORTA,4 bcf PORTB,6 bcf PORTB,7 call Pause1 call GenAll call Pause1 bcf PORTA,4 bcf PORTB,6 bcf PORTB,7 call Pause1 call GenAll call Pause1 bcf PORTA,4 bcf PORTB,6 bcf PORTB,7 call Pause1 goto SoundRight ;------------------LEFT---------------------- SoundLeft TrigLeft_1 call Pulse TrigLeft_0 btfss EchoLeft goto $-1 call Pause btfss EchoLeft goto SetRedLeft call Pause btfss EchoLeft goto SetYellowLeft goto SetGreenLeft SetRedLeft movlw RedLeft iorwf PORTB,1 bcf PORTB,1 bcf PORTB,2 goto SoundRight SetYellowLeft movlw YellowLeft iorwf PORTB,1 bcf PORTB,0 bcf PORTB,2 goto SoundRight SetGreenLeft movlw GreenLeft iorwf PORTB,1 bcf PORTB,0 bcf PORTB,1 clrwdt btfsc EchoLeft ; wait Echo end goto $-2 goto SoundRight ;----------------RIGHT--------------------- SoundRight call Pause2 TrigRight_1 call Pulse TrigRight_0 btfss EchoRight goto $-1 call Pause btfss EchoRight goto SetRedRight call Pause btfss EchoRight goto SetYellowRight goto SetGreenRight SetRedRight movlw RedRight iorwf PORTB,1 bcf PORTB,4 bcf PORTB,5 goto SetGenLight SetYellowRight movlw YellowRight iorwf PORTB,1 bcf PORTB,3 bcf PORTB,5 goto SetGenLight SetGreenRight movlw GreenRight iorwf PORTB,1 bcf PORTB,3 bcf PORTB,4 clrwdt btfsc EchoRight ; wait Echo end goto $-2 ;------------------------ Set General Lights ---------------- SetGenLight btfss PORTB,0 ; check RedLeft goto CheckRedRight call GenRed goto SoundLeft CheckRedRight btfss PORTB,3 goto CheckGreenLeft call GenRed goto SoundLeft CheckGreenLeft btfss PORTB,2 goto SetGenYellow btfss PORTB,5 goto SetGenYellow call GenGreen goto SoundLeft SetGenYellow call GenYellow goto SoundLeft END