;range.asm ; Range detector program. Uses the SRF-04 ultrasonic range detector ; Runs at 10 MHz ;Jumper Settings ; JU2 5VDC internal/external select REG ; JU1 Variable DC internal/external select REG ; JU3 Processor power select jumper RUN ; JU4 EEPROM X8/X16 organization select N/A ; JU6 Port A LED output and analog input RA0 N/A ; RA1 N/A ; RA2 N/A ; RA3 N/A ; RA4 N/A ; JU5 RS-232 receive/transmit select N/A ;Requirements ; If you are using the EPIC programmer and the In-Circuit programming ; cable, you must remove this cable from the PIC-MDS to ensure proper ; program execution. Maclib 'P16F8x.INC' ;Library file for PIC16F84 and PM assembler ;Comment this line out for other assemblers Device PIC16F84,HS_OSC,WDT_OFF,PROTECT_OFF,PWRT_ON ID 'RANG' ;Hardware Equates ORG 0Ch ;Start of File register area #define trigger PORTB.1 ; trigger to SRF04 - RB1 #define echo PORTB.2 ; echo from SRF04 - RB2 ;Equates required by BIN2DEC.LIB and DEC2BIN.LIB: Hundreds DS 1 ; Hundreds digit Tens DS 1 ; Tens digit Ones DS 1 ; Ones digit ;Equates required by RS232TX.LIB: Counter DS 1 ; Serial bit delay counter BitCounter DS 1 ; RS232 bit counter register Transmit DS 1 ; Transmit byte storage register ;Other equates Counter1 DS 1 ; general purpose counter Counter2 DS 1 ; general purpose counter Counter3 DS 1 ; general purpose counter loop DS 1 ; loop counter range DS 1 ; range returned by SRF04 ORG 00h ; Start program at Reset Vector CALL Transmit_Port ; Configure PortA.4 as serial output GOTO Initialize ; Continue with initialize routine ORG 05h ; Continue after interrupt vector Include 'BIN2DEC.LIB' ; and Binary to Decimal conversion Include 'RS232TX.LIB' ; RS232 serial transmission library Initialize BSF RP0 ; Select register page 1 MOVLW 00000100b ; RB1 is echo (read) the rest are write MOVWF TRISB ; Move W to TRISB to make bit 1 input BCF RP0 ; Move back to register page 0 CLRF PORTB Main ;This program loop triggers the range detector and reads ;back the results. BIN2DEC.LIB is used ;to convert the binary key number to three decimal digits. ;Each decimal digit is converted to ASCII by adding 30h and ;is sent to the LCD display. ;============================================================= ; read range detector ;============================================================= ; Trigger the pulse BSF trigger ; Set trigger to high NOP ; Wait 11 uS NOP ; 2 NOP ; 3 NOP ; 4 NOP ; 5 NOP ; 6 NOP ; 7 NOP ; 8 NOP ; 9 NOP ; 10 NOP ; 11 BCF trigger ; Turn off the trigger ; Get set to start counting MOVLW 8 ; start range at min echo time MOVWF range MOVLW 247 ; Start at 255 - range MOVWF Counter ; Put it in the Counter ; Wait until the echo pin to go high echoHiWait BTFSS echo GOTO echoHiWait ; Now wait for the pin to go low. This loop is 6 usec long, so the ; Delay is the speed of sound (74usec) - 6 usec = 68 usec echoLoop BTFSS echo ; See if the echo is received GOTO echoDone ; Got the echo INCF range,1 ; Increment range count CALL Delay_68us ; Delay 1 inch of time DECFSZ Counter,1 ; Decrement inch counter GOTO echoLoop ; repeat loop echoDone ; The echo is received, or at max range ;============================================================== ; Write the range to the LCD/Serial port ;============================================================= CALL RangeSerial ; Write range to serial port CALL Delay_10ms ; Wait for range detector to reset ; Wait 300ms (roughly 1/3 second CALL Delay_100ms ; CALL Delay_100ms ; CALL Delay_100ms ; GOTO Main ; Do it again! Delay_5ms ;Delay loop used to initialize LCD CLRF Counter1 ;Wipe first counter MOVLW 1Ah ;Load W with Timer Constant MOVWF Counter2 ;for Counter2 :Loop DECFSZ Counter1,1 ;Decrement first counter GOTO :Loop ;256 times DECFSZ Counter2,1 ;Decrement, second counter GOTO :Loop ;from constant, above RETURN Delay_10ms CALL Delay_5ms CALL Delay_5ms RETURN Delay_20ms CALL Delay_5ms CALL Delay_5ms CALL Delay_5ms CALL Delay_5ms RETURN Delay_100ms CALL Delay_20ms CALL Delay_20ms CALL Delay_20ms CALL Delay_20ms CALL Delay_20ms RETURN Delay_100us MOVLW 61h ; 97d MOVWF Counter1 Loop100us DECFSZ Counter1,1 GOTO Loop100us RETURN ; ; Delay for 68 microseconds including call and return time ; Delay_68us MOVLW 62 ; speed of sound - prog loop time MOVWF Counter1 ; Start counter at 62 Loop68us DECFSZ Counter1,1 ; Decrement and skip GOTO Loop68us ; do it again RETURN ; All done, return now ;////////////////////////////////////////////////////////////////// ; Write the range to the serial port ; RangeSerial MOVLW 'R' ;Load W with ASCII code for 'K' and CALL Transmit_Data ;send out of serial port MOVLW 'a' ;Load W with ASCII code for 'K' and CALL Transmit_Data ;send out of serial port MOVLW 'n' ;Load W with ASCII code for 'K' and CALL Transmit_Data ;send out of serial port MOVLW 'g' ;Load W with ASCII code for 'K' and CALL Transmit_Data ;send out of serial port MOVLW 'e' ;Load W with ASCII code for 'K' and CALL Transmit_Data ;send out of serial port MOVLW ':' ;Load W with ASCII code for 'K' and CALL Transmit_Data ;send out of serial port MOVLW ' ' ;Load W with ASCII code for 'K' and CALL Transmit_Data ;send out of serial port Transmit_Num ;Transmit the range ASCII digits. MOVF range, 0 ;Move range into W CALL BIN_DEC ;and convert to decimal MOVLW 30h ;Load constant to convert to ASCII ADDWF Hundreds,1 ;and add to hundreds digit ADDWF Tens,1 ;add to tens digit ADDWF Ones,1 ;and add to ones digit MOVF Hundreds,0 ;Load W with ASCII Tens digit and CALL Transmit_Data ;send out of serial port MOVF Tens,0 ;Load W with ASCII Tens digit and CALL Transmit_Data ;send out of serial port MOVF Ones,0 ;Load W with ASCII Ones digit and CALL Transmit_Data ;send out of serial port TransmitEOL MOVLW 13 ;Carriage return CALL Transmit_Data ;send out of serial port MOVLW 10 ;Linefeed CALL Transmit_Data ;send out of serial port RETURN