'---------------Title--------------

' File......sonar1.pbp
' Started....5/4/06
' Microcontroller used:  Microchip Technology 16F88
'                        microchip.com
' PicBasic Pro Code: micro-Engineering Labs, Inc.
'                    melabs.com 

'--------Program Desciption--------

' Using the Devantech SRF04 ultrasonic range finder,
' the distance from the range finder to an object
' is displayed on an LCD screen in inches. 

'------------Schematic-------------

' See schematic at:
' http://cornerstonerobotics.org/schematics/pic_programming_sonar1.pdf

'----------Related Lesson----------

' sonar1.pbp is used in the lesson Ultra Sonic Sensor at:
' http://cornerstonerobotics.org/curriculum/lessons_year2/erii24_ultra_sonic_sensor.pdf

'-----New PicBasic Pro Commands---- 

' The PicBasic Pro Compiler Manual is on line at:
' http://www.microengineeringlabs.com/resources/index.htm#Manuals

' PULSIN Pin,State,Var
' Pulse width is measured on Pin.
' If State = 0, width of low pulse is measured
' and assigned to variable Var.
' Is State = 1, width of high pulse is measured
' and assigned to variable Var.
' Look around page 120 in the PicBasic Pro Compiler Manual
 
'---------Revision History---------

' 11/25/08 Convert from PIC16F84A to PIC16F88,
' add PIC16F88 oscillator and ANSEL = 0 initializations.

'----------PIC Connections---------

'		16F88 Pin			   Wiring
'		---------			---------- 
'  		  RA0				LCD pin 11(DB4)
'         RA1               LCD pin 12(DB5)
'         RA2               LCD pin 13(DB6)
'         RA3               LCD pin 14(DB7)
'         RA4               LCD Register Select(RS)
'         RB0               SRF04 Emitter
'         RB1               SRF04 Echo
'         RB3               LCD Enable(E)
' See schematic for the other usual PIC connections

'----------LCD Connections---------

'	     LCD Pin	   	      Wiring
'		---------		 	---------- 
'          1               Ground(Vss)
'          2               + 5v(Vdd)
'          3               Center of 20K Pot(Contrast)
'          4               RA4(Register Select,RS)
'          5               Ground(Read/Write,R/W)
'          6               RB3(Enable)
'          7               No Connection(DB0) 
'          8               No Connection(DB1)     
'          9               No Connection(DB2)     
'         10               No Connection(DB3)     
'         11               RA0(DB4)    
'         12               RA1(DB5)
'         13               RA2(DB6)
'         14               RA3(DB7)
 
'---------Constants/Defines--------

    conv_to_inches  con 15       ' Assigns the value 15 to the
                                 ' constant conv_to_inches  

'------------Variables-------------

    emit    var PORTB.0          ' Pin RB0 assigned the name emit
    
    echo    var PORTB.1          ' Pin RB1 assigned the name echo
    
    dist_raw var word            ' Defines dist_raw as a WORD
                                 ' variable  
    
    dist_inch var word           ' Defines dist_inch as a WORD
                                 ' variable  
    
'-----------Initialization---------

    TRISB = %00000010            ' Sets PORTB.1 (echo) as input,
                                 ' all other PORTB pins as outputs

    ANSEL = 0                    ' Configure all pins to digital
                                 ' operation since not using ADC
                                 ' (Analog to Digital Converter)
                                
    OSCCON = $60	             ' Sets the internal oscillator in the
                                 ' 16F88 to 4 MHz   	    	

'-------------Main Code------------ 
        
start:                           ' start label
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
    pulsout emit,1               ' Pulse Width:
                                 ' Sends a pulse out on pin RB0 (emit)
                                 ' for 10 usec.  The period,(1) is 
                                 ' multiplied by the increment for 
                                 ' a 4 MHz oscillator(10 usec)
                                 ' to get a pulse out time of 10 usec.
                                
    pulsin echo,1,dist_raw       ' Measures the pulse width on pin RB1
                                 ' (echo) and assigns the reading to the 
                                 ' variable dist_raw.               
    
    dist_inch = (dist_raw/conv_to_inches) ' Converts raw sonar reading   
                                 ' to inches.
                                                                     
    lcdout $FE,1,"Dist.in inch." ' Clears LCD screen, displays
                                 ' "Dist. in inch."
    
    lcdout $FE,$14,#dist_inch    ' Moves cursor over one space,
                                 ' displays value of the variable
                                 ' dist_inch    
    
    pause 10                     ' Pause 10 milliseconds
    
    goto start
       
    end

