'*********************************************************** '** ** '** Programa de ejemplo para conectar ** '** dos modulos SRF08 al basicX24 ** '** ** '** Copyright 2002 - Devantech Ltd ** '** Prohibido el uso comercial de este software ** '** Solo para uso privado y educacional ** '** ** '** Escrito por Gerald Coe - Febrero 2002 ** '** Traducción española por www.superrobotica.com ** '** ** '*********************************************************** Const SCL As Byte = 14 ' I2C clock - Escoger los pines del I2C aqui. Const SDA As Byte = 13 ' I2C data Const CmdReg As Byte = 0 ' Registro de comandos del SRF08 Const LdrReg As Byte = 1 ' Registro del sensor de luz del SRF08 Const RangeReg As Byte = 2 ' Registro de las mediciones del SRF08 Const RangeCmd As Byte = 81 ' Registro de comandos - 80 Pulgadas, 81 centimetros, 82 uSegundos ' Los modulos deben de tener las siguientes direcciones Const Sonar1 As Byte = &He0 ' 1º SRF08 en la direccion 0Xe0 Const Sonar2 As Byte = &He2 ' 2º SRF08 en la direccion 0Xe2 Dim I2cAck As Boolean ' Indicador de Ok Sub Main() Dim Ldr1 As Byte Dim Range1 As New UnsignedInteger Dim Ldr2 As Byte Dim Range2 As New UnsignedInteger Call PutPin(SCL, bxOutputHigh) Call PutPin(SDA, bxOutputHigh) Do ' 1º modulo SRF08 Call I2cByteWrite(Sonar1, CmdReg, RangeCmd) ' Comando de hacer la medicion en Cm Call Delay(0.07) ' Espera 70 ms para que termine Ldr1 = I2cByteRead(Sonar1, LdrReg) ' Lee el sensor de luz Range1 = I2cWordRead(Sonar1, RangeReg) ' Lee la distancia medida ' 2º modulo SRF08 Call I2cByteWrite(Sonar2, CmdReg, RangeCmd) ' Comando de hacer la medicion en Cm Call Delay(0.07) ' Espera 70 ms para que termine Ldr2 = I2cByteRead(Sonar2, LdrReg) ' Lee el sensor de luz Range2 = I2cWordRead(Sonar2, RangeReg) ' Lee la distancia medida debug.Print "Sensor Luz 1= "; CStr(Ldr1); ", Distancia 1 = "; CStr(Range1); _ " Sensor Luz 2= "; CStr(Ldr2); ", Distancia 2 = "; CStr(Range2) Loop End Sub '-------------------------------------------------------------------------------------------- ' Rutinas de comunicacion I2C '-------------------------------------------------------------------------------------------- ' Escribe I2cData en I2cReg delI2cAddr Sub I2cByteWrite(ByVal I2cAddr As Byte, ByVal I2cReg As Byte, ByVal I2cData As Byte) Call I2cStart() Call I2cOutByte(I2cAddr) ' Envia la direccion Call I2cOutByte(I2cReg) ' Envia numero de registro Call I2cOutByte(I2cData) ' Envia el dato Call I2cStop() End Sub Function I2CByteRead(ByVal I2cAddr As Byte, ByVal I2cReg As Byte) As Byte Call I2cStart() Call I2cOutByte(I2cAddr) ' Envia la direccion Call I2cOutByte(I2cReg) ' Envia numero de registro Call I2cStart() ' Envia start I2cAddr = I2cAddr+1 Call I2cOutByte(I2cAddr) ' Envia la direccion I2cAck = False ' Envia Nak I2cByteRead = I2cInByte() ' lee el byte con Nak Call I2cStop() End Function Function I2CWordRead(ByVal I2cAddr As Byte, ByVal I2cReg As Byte) As UnsignedInteger Set I2CWordRead = New UnsignedInteger Call I2cStart() Call I2cOutByte(I2cAddr) ' Envia la direccion Call I2cOutByte(I2cReg) ' Envia numero de registro Call I2cStart() ' Envia start I2cAddr = I2cAddr+1 Call I2cOutByte(I2cAddr) ' Envia la direccion I2cAck = True ' Envia Ack I2cWordRead = CuInt(I2cInByte()*256) I2cAck = False ' Envia Nak I2cWordRead = I2cWordRead + CuInt(I2cInByte()) Call I2cStop() End Function Sub I2cOutByte(I2cData As Byte) Call ShiftOut(SDA, SCL, 8, I2cData) ' Saca el dato Call PutPin(SDA, bxInputTristate) ' Call PutPin(SCL, bxOutputHigh) ' Call PutPin(SCL, bxOutputLow) End Sub Function I2cInByte() As Byte I2cInByte = ShiftIn(SDA, SCL, 8) If I2cAck=True Then Call PutPin(SDA, bxOutputLow) Else Call PutPin(SDA, bxOutputHigh) End If Call PutPin(SCL, bxOutputHigh) Call PutPin(SCL, bxOutputLow) End Function Sub I2cStart() ' Comando Start de I2C Call PutPin(SDA, bxOutputHigh) Call PutPin(SCL, bxOutputHigh) Call PutPin(SDA, bxOutputLow) Call PutPin(SCL, bxOutputLow) End Sub Sub I2cStop() ' Comando Stop de I2C Call PutPin(SDA, bxOutputLow) Call PutPin(SCL, bxOutputHigh) Call PutPin(SDA, bxOutputHigh) End Sub