$regfile = "m8def.dat" $crystal = 8000000 $baud = 9600 Config Scl = Portc.5 Config Sda = Portc.4 Config Lcdpin = Pin , Db4 = Portb.4 , Db5 = Portb.5 , Db6 = Portb.0 , Db7 = _ Portb.1 , E = Portb.3 , Rs = Portb.2 Dim Lsb As Byte Dim Msb As Byte Dim Measurement As Word Dim Srf08_address As Byte Dim Srf08_range As Word Dim Srf08_adjust As Word Srf08_address = 224 Declare Sub Srf08_ping(byval I2caddr As Byte) Declare Sub Srf08_results(byval I2caddr As Byte) Declare Sub Srf08_results1(byval I2caddr As Byte) Declare Sub Srf08_results2(byval I2caddr As Byte) Declare Sub Srf08_set_range(byval I2caddr As Byte , Byval Range As Byte) Declare Sub Srf08_set_gain(byval I2caddr As Byte , Byval Gain As Byte) Waitms 500 Call Srf08_set_range(srf08_address , 255) Call Srf08_set_gain(srf08_address , 1) Srf08_adjust = 0 Cls Do Call Srf08_ping(0) Waitms 65 Call Srf08_results(srf08_address) Locate 1 , 1 Lcd "R=" ; Measurement ; "cm " Loop ' ---------- Sub ---------------------- Sub Srf08_ping(byval I2caddr As Byte) I2cstart I2cwbyte I2caddr I2cwbyte 0 I2cwbyte 81 I2cstop End Sub Sub Srf08_set_range(byval I2caddr As Byte , Byval Range As Byte) I2cstart I2cwbyte I2caddr I2cwbyte 2 I2cwbyte Range I2cstop End Sub $regfile = "m8def.dat" $crystal = 8000000 $baud = 9600 Config Scl = Portc.5 Config Sda = Portc.4 Config Lcdpin = Pin , Db4 = Portb.4 , Db5 = Portb.5 , Db6 = Portb.0 , Db7 = _ Portb.1 , E = Portb.3 , Rs = Portb.2 Dim Lsb As Byte Dim Msb As Byte Dim Measurement As Word Dim Srf08_address As Byte Dim Srf08_range As Word Dim Srf08_adjust As Word Srf08_address = 224 Declare Sub Srf08_ping(byval I2caddr As Byte) Declare Sub Srf08_results(byval I2caddr As Byte) Declare Sub Srf08_results1(byval I2caddr As Byte) Declare Sub Srf08_results2(byval I2caddr As Byte) Declare Sub Srf08_set_range(byval I2caddr As Byte , Byval Range As Byte) Declare Sub Srf08_set_gain(byval I2caddr As Byte , Byval Gain As Byte) Waitms 500 Call Srf08_set_range(srf08_address , 255) Call Srf08_set_gain(srf08_address , 1) Srf08_adjust = 0 Cls Do Call Srf08_ping(0) Waitms 65 Call Srf08_results(srf08_address) Locate 1 , 1 Lcd "R=" ; Measurement ; "cm " 'Waitms 100 Loop ' ---------- Sub ---------------------- Sub Srf08_ping(byval I2caddr As Byte) I2cstart I2cwbyte I2caddr I2cwbyte 0 I2cwbyte 81 I2cstop End Sub Sub Srf08_set_range(byval I2caddr As Byte , Byval Range As Byte) I2cstart I2cwbyte I2caddr I2cwbyte 2 I2cwbyte Range I2cstop End Sub Sub Srf08_set_gain(byval I2caddr As Byte , Byval Gain As Byte) I2cstart I2cwbyte I2caddr I2cwbyte 1 I2cwbyte Gain I2cstop End Sub Sub Srf08_results(byval I2caddr As Byte) I2cstart I2cwbyte I2caddr I2cwbyte 2 I2cstart Incr I2caddr I2cwbyte I2caddr I2crbyte Lsb , Ack I2crbyte Msb , Nack I2cstop Measurement = 256 * Msb Measurement = Measurement + Lsb End Sub End