;////////////////////////////////////////////////////////////////////////////
;//
;//	Ultrasonic rangefinder Software srf04
;//
;//	Written by Gerald Coe     -       May 2000
;//	Tidied up for publication - 12th July 2000
;//
;//	Private and Educational use only is permitted
;//	Commercial use of this software is prohibited.
;//
;////////////////////////////////////////////////////////////////////////////


	processor	12f508
	__config	0feeh		; Internal Osc, WDT Enabled, not code protected
	include	"p12f508.inc"

#define	RAMSTART 07h

	radix	dec

#define trig	GPIO,0		; trigger input from host
#define	pulse	GPIO,1		; timing pulse output to host
#define	echo	GPIO,2		; echo signals from comparitor
#define  nc	GPIO,3		; Unused - do not connect.
#define	tx2	GPIO,4		; Tx phase 2
#define	tx1	GPIO,5		; Tx phase 1

#define _C	STATUS,C

;/////////////////////////////////////////////////////////////////////////////
	org	RAMSTART

loop		res	1	; loop counter
dlyctr		res	1	; delay counter
tone_cnt	res	1	; count echo cycles
period		res	1	; received burst cycle period from tmr0

;/////////////////////////////////////////////////////////////////////////////

    ;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    org     1ffh      	                                              ;++
    movlw   70h                                                       ;++++
   	movwf  OSCCAL		; use microchip's calibration value   ;++++++    aki esta mi problema no se si asi
    org 0x000           ;start address 0                              ;++++++++  esta bien configurado
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	movlw	89h
	option			;assign 1:2 prescaler to watchdog

    CLRF	GPIO   ;  nuevo agregado
	movlw	0dh
	tris	GPIO		;GPIO 1, 4 & 5 are outputs

	movwf	0

	bcf	pulse


main:	clrwdt
	btfss	trig		; wait for trigger signal from user to go high
	goto	main		; from previous measurement.

m2:	clrwdt
	btfsc	trig		; wait for trigger signal from user
	goto	m2

	call	burst		; send the ultra-sonic burst
	bsf	pulse		; start the output timing pulse
 
m1:	btfsc	echo
	goto	m1		; wait for low
	bcf	pulse		; end the output timing pulse

	goto	main

burst:	clrf	loop
	movlw	8		; number of cycles in burst
	movwf	loop

burst1:	movlw	0x10		; 1st half cycle
	movwf	GPIO

	movlw	3		; (3 * 3inst * 1uS) -1uS = 8uS 
	movwf	dlyctr		; 8uS + (4*1uS) = 12uS
burst2:	decfsz	dlyctr,f
	goto	burst2

	movlw	0x20
	movwf	GPIO
	movlw	2		; (2 * 3inst * 1uS) -1uS = 5uS 
	movwf	dlyctr		; 5uS + (8*1uS) = 13uS
burst3:	decfsz	dlyctr,f
	goto	burst3
	nop
	decfsz	loop,f
	goto	burst1

	movlw	0x00		; set both drives low
	movwf	GPIO

	retlw	0x000
	
tone:	clrf	TMR0

t1:	btfsc	echo
	goto	t1		; wait for low

	movfw	TMR0
	clrf	TMR0
	movwf	period		; store timer0 value

	movlw	21		; if(period>22 && period<30) 
	subwf	period,w
	btfss	_C
	goto	t2
	movlw	30
	subwf	period,f
	btfsc	_C
	goto	t2

	decfsz	tone_cnt,f	; 25uS period OK, so 
	goto	t1		; if not yet 3 of them, keep looking
	retlw	0		; else - success - return
	
t2:	movlw	3		; failed to detect 25uS period, so reset tone detect
	movwf	tone_cnt	; to 3 and keep looking
	goto	t1

;////////////////////////////////////////////////////////////////////////////

	end

;////////////////////////////////////////////////////////////////////////////