'------------------------------------------------------------------------------- ' starter.bas ' this is an example for readining a SFR08 sonar ' all output will be on com2 ' There is also an LED on portB.0 that will light if the ' distance reading is lower then 6 inches '------------------------------------------------------------------------------- $crystal = 14745600 $regfile = "m128def.dat" $baud1 = 19200 '$sim 'config serial buffer Config Serialout1 = Buffered , Size = 40 Config Serialin1 = Buffered , Size = 40 'com 2 standard conection Config Com2 = Dummy , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0 'config I2C lines for mavric Config Sda = Portd.1 Config Scl = Portd.0 'this function is used to read the sonar value 'declare Functons Declare Function I2cread8_w(byval I2cnode As Byte , Byval Loc As Byte) As Word 'this sub starts the PING of all conected sonars 'Declare Subs Declare Sub I2cwrite8_b(byval I2cnode As Byte , Byval Loc As Byte , Byval Value As Byte) ' Startup int code I2cinit 'start the I2C buss Open "com2:" For Random As #2 'open come port Enable Interrupts 'enable interupts Config Portb = Output Portb = 0 'set all low 'varables Dim Distance As Word 'varable to hold sonar value Const Sonar1 = 226 'make address of sonar 'start main program Do 'small delay of 500mS Waitms 500 Call I2cwrite8_b(0 , 0 , 80) 'ping all sonars and return value in inches Waitms 70 'delay 70mS for all pings to hapen Distance = I2cread8_w(sonar1 , 2) 'read the distance from node 226 and loactions 2 & 3 Print #2 , "Distance = " + Str(distance) + Chr(10) + Chr(13) 'send distance out com port If Distance <= 6 Then 'check distance on sonar reading Portb.0 = 1 ' less then 6 inch then led ON Print #2 , "LED on" + Chr(10) + Chr(13) Else Portb.0 = 0 ' greater then 6 inch then led OFF Print #2 , "LED off" + Chr(10) + Chr(13) End If Print #2 , "" + Chr(10) + Chr(13) 'blank print to seperate readings Waitms 500 'wait 500mS and start again Loop 'end main program 'subs '------------------------------ I2C I2C I2C I2C I2C I2C I2C I2C I2C I2C I2C Sub I2cwrite8_b(byval I2cnode As Byte , Loc As Byte , Value As Byte) I2cstart I2cwbyte I2cnode I2cwbyte Loc I2cwbyte Value I2cstop Waitms 10 End Sub '----------------------------------------------------------------------------- '8x16 Function I2cread8_w(byval I2cnode As Byte , Loc As Byte) As Word Local Lsb As Byte Local Msb As Byte I2cstart I2cwbyte I2cnode I2cwbyte Loc I2cstart Incr I2cnode I2cwbyte I2cnode I2crbyte Msb , Ack I2crbyte Lsb , Nack I2cstop I2cread8_w = Makeint(lsb , Msb) End Function