'*********************************************************** '** ** '** Rutinas I2C para que el Basic BX-24 ** '** cambie la direccion del modulo SRF08 ** '** ** '** 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 SRF08_NEW_ADDRESS As Byte = &He0 ' Poner la dirección del modulo aqui 'Direcciones disponibles: e0, e2, e4, e6, e8, ea, ec, ee ' f0, f2, f4, f6, f8, fa, fc, fe Const SCL As Byte = 14 ' I2C clock - elegir los pines deseados para SCL and SDA Const SDA As Byte = 13 ' I2C data Const GB As Byte = 0 ' I2C Dirección de atencion general Const CmdReg As Byte = 0 ' Registro de comandos del SRF08 Const LdrReg As Byte = 1 ' Registro del sensor de luz en el SRF08 Const RangeReg As Byte = 2 ' Registro de la primera mediada en el SRF08 Const RangeCmd As Byte = 81 ' Registro de comando - 80 Pulgadas, 81 cm, 82 uSegundos Dim I2cAck As Boolean ' Indicador de comprobacion Sub Main() Dim Ldr As Byte Dim Range As New UnsignedInteger Call PutPin(SCL, bxOutputHigh) Call PutPin(SDA, bxOutputHigh) Call Delay(1.0) ' Retardo para que termine el reset del srf08 Call I2cByteWrite(GB, CmdReg, &Ha0) ' 1º comando de la secuencia Call I2cByteWrite(GB, CmdReg, &Haa) ' 2º comando de la secuencia Call I2cByteWrite(GB, CmdReg, &Ha5) ' 3º comando de la secuencia Call I2cByteWrite(GB, CmdReg, SRF08_NEW_ADDRESS) ' Cambia la nueva direccion de I2C ' Una vez cambiada la direccion comienza un bucle que realiza medidas continuas Do Call I2cByteWrite(SRF08_NEW_ADDRESS, CmdReg, RangeCmd) ' Comienza a medir en centimetros Call Delay(0.07) ' Espera 70mS a que termine la medida Ldr = I2cByteRead(SRF08_NEW_ADDRESS, LdrReg) ' Lee el sensor de luz Range = I2cWordRead(SRF08_NEW_ADDRESS, RangeReg) ' Lee la distancia medida debug.Print "Luz = "; CStr(Ldr); ", Distancia = "; CStr(Range) 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