Dim D As New oDio4 'Initializes a new object called D as a nibble I/O Dim E As New oDio1 'Initializes a new object called E as a single bit I/O Dim RS As New oDio1 'Initializes a new object called RS as a single bit I/O Dim LCD as New oDataStrobe 'Initializes a new object called LCD as a Data Strobe Dim Srf08 As New oI2C 'Create the SRF08 object Dim Ver as new oByte Dim Light as new oByte Dim Range1 as new oWord Dim Range2 as new oWord Dim S As New oBuffer(5) Dim Sw1 As New oDio1 Dim Sw2 As New oDio1 Sub main() IOSetup ' Sets up the I/O lines LCDSetup ' Initialize the LCD Display SRF08Setup ' Initialize the SRF08's ClearLCD LocateRow1 LCD.String = "SRF08/ooPIC Example" Do ReadSonar(112) ' 112 is decimal of 0xE0 shifted right by 1 LocateRow2 LCD.String = Str$(Light) + " " LCD.String = Str$(Range1) + " " LCD.String = Str$(Range2) ReadSonar(113) ' 113 is decimal of 0xE2 shifted right by 1 LocateRow3 LCD.String = Str$(Light) + " " LCD.String = Str$(Range1) + " " LCD.String = Str$(Range2) Loop End Sub Sub ReadSonar(Addr As oByte) SRF08.Node = Addr ' I2C Address of SRF08 sonar SRF08.Location = 0 ' Command Register SRF08.Width = cv8bit ' 1 byte wide transfer SRF08 = 81 ' Ranging Command - Result in cm ' Use these three lines Do ' Wait for ranging to complete Ver = SRF08 ' This will wait forever if your sonar Loop While Ver=255 ' becomes disconnected, so you may prefer ' or this one ' OOPic.Delay = 7 ' to simply wait 70mS instead SRF08.Location = 1 ' Light Sensor Register Light = SRF08 ' Get Light Sensor value SRF08.Width = cv16bit ' 2 byte wide transfer SRF08.Location = 2 ' 1st Range Register Range1 = SRF08 ' Get Range to 1st object SRF08.Location = 4 ' 2nd Range Register Range2 = SRF08 ' Get Range to 2nd object End Sub 'ReadSonar Sub LocateRow1() 'Routine to locate the cursor at row 1, column 1 RS = 0 'Sets Register Select to Control Register LCD.Value = 128: 'Sets the address of the cursor RS = 1 'Sets Register Select to Data Register End Sub Sub LocateRow2() 'Routine to locate the cursor at row 2, column 1 RS = 0 'Sets Register Select to Control Register LCD.Value = 192: 'Sets the address of the cursor RS = 1 'Sets Register Select to Data Register End Sub Sub LocateRow3() 'Routine to locate the cursor at row 2, column 1 RS = 0 'Sets Register Select to Control Register LCD.Value = 148: 'Sets the address of the cursor RS = 1 'Sets Register Select to Data Register End Sub Sub LocateRow4() 'Routine to locate the cursor at row 2, column 1 RS = 0 'Sets Register Select to Control Register LCD.Value = 212: 'Sets the address of the cursor RS = 1 'Sets Register Select to Data Register End Sub Sub ClearLCD() RS = 0 'Sets Register Select to Control Register LCD.Value = 1: 'Sets the address of the cursor RS = 1 'Sets Register Select to Data Register End Sub Sub IOSetup() 'This routine sets the IO lines that are connected to the LCD display 'and configures the Virtual Circuit to do the Data Strobe. D.Iogroup = 3 'I/O group used for nibble out to LCD D.Nibble = 0 'Lower nibble - I/O 24-27 D.Direction = 0 'Direction of data flow in or out E.IOLine = 23 'Which I/O line to use for Display enable and strobe E.Direction = 0 'Direction of data flow in or out E = 0 'Initialize E to 0 RS.IOLine = 22 'Which I/O line to use for Register Select RS.Direction = 0 'Direction of data flow in or out RS = 0 'Initialize RS to 0 LCD.Output.Link(D) 'Connect the datastrobe's data to the LCD's data I/O line LCD.Strobe.Link(E) 'Connect the datastrobe's datastrobe to the LCD's E I/O line LCD.Operate = cvTrue 'Turn the Datastrobe object on. Sw1.IOLine = 20 Sw1.Direction = cvInput Sw2.IOLine = 21 Sw2.Direction = cvInput End Sub Sub LCDSetup() 'This routine performs LCD reset and initialization and 'configures LCD display to operate in 4 bit nibble mode 'Note: LCD Module starts out in 8 bit mode' 'This sequence of codes were found in the Optrix handbook. RS = 0 'Sets Register Select to Control Register LCD.Mode = cv8Bit: 'Select 8-bit mode LCD.Value = 3: ' Power on sequence LCD.Value = 3: ' Power on sequence LCD.Value = 3: ' Power on sequence LCD.Value = 2: ' Sets data length to 4 bits LCD.Mode = cv4Bit: 'Select 4-bit mode LCD.Value = 40: ' Set # of display lines & font LCD.Value = 8: ' Set display off LCD.Value = 12: ' sets display on LCD.Value = 6: ' Set entry mode RS = 1 'Sets Register Select to Data Register End Sub Sub Srf08SetUp() SRF08.Node = 112 ' Decimal of Hex address 0xE0 shifted right by 1 SRF08.Mode = cv10bit ' I2C mode is 10-Bit Addressing. SRF08.NoInc = 1 ' Don't increment SRF08.Width = cv8bit ' 1 byte wide transfer SRF08.Location = 2 ' Range Register SRF08 = 140 ' Limit 1st sonar Range to 6m, 140 x 43mm = 6m SRF08.Node = 113 ' Decimal of Hex address 0xE2 shifted right by 1 SRF08 = 140 ' Limit 2nd sonar Range to 6m, 140 x 43mm = 6m End Sub