EV3Basic - Sensor Multiplexer ansteuern

NXC, C/C++, Lejos, pbLua, RobotC...

Moderator: Moderatoren

Benutzeravatar
c0pperdragon
Schreibt viel
Schreibt viel
Beiträge: 231
Registriert: 9. Feb 2015 00:29

EV3Basic - Sensor Multiplexer ansteuern

Beitragvon c0pperdragon » 10. Sep 2016 11:48

Weil ich hin und wieder Anfragen kriege, wie man diesen Sensor-Multiplexer
http://www.mindsensors.com/ev3-and-nxt/ ... ev3-or-nxt
von EV3Basic aus ansteuern kann, möchte ich hier einen Beispielcode posten, den ein User erstellt hat:

Code: Alles auswählen

'  ** I2C Test **
'  By David Musgrave
'  http://WinthropDC.com
'  Last Modified: 09-Sep-2016

Finished = "False"
Clicks = ""
Value = 0
 
I2CPort = 4
I2CBus = 80
I2CChannel = 2
I2CAddr = I2CBus + 1 * (I2CChannel - 1)
 
Sensor.SetMode (I2CPort, 0)
LCD.Clear()
LCD.Write( 0*8, 0*10, "I2C Sensor Test")
LCD.Write( 0*8, 2*10, "Port: " + I2CPort)
LCD.Write(10*8, 2*10, "Address: " + I2CAddr)
 
I2CReg = 82 ' 0x52 Sensor Mode
I2CWriteByte = 1 + 1
I2CWriteData = Vector.Init(I2CWriteByte, I2CReg)
I2CWriteData[1] = 0 ' Sensor Mode
I2CReadByte = 1
I2CReadData = Vector.Init(I2CReadByte, 0)
 
I2CReadData = Sensor.CommunicateI2C(I2CPort, I2CAddr, I2CWriteByte, I2CReadByte, I2CWriteData)
 
While Finished = "False"
'  I2CReg = 116 ' 0x74 Ready Flag
'  I2CWriteByte = 1 + 0
'  I2CWriteData = Vector.Init(I2CWriteByte, I2CReg)
'  I2CReadByte = 1
'  I2CReadData = Vector.Init(I2CReadByte, 0)
'  While I2CReadData[0] = 0
'    I2CReadData = Sensor.CommunicateI2C(I2CPort, I2CAddr, I2CWriteByte, I2CReadByte, I2CWriteData)
'  EndWhile
 
  I2CReg = 84 ' 0x54 Sensor Data
  I2CWriteByte = 1 + 0
  I2CWriteData = Vector.Init(I2CWriteByte, I2CReg)
  I2CReadByte = 2
  I2CReadData = Vector.Init(I2CReadByte, 0)
 
  I2CReadData = Sensor.CommunicateI2C(I2CPort, I2CAddr, I2CWriteByte, I2CReadByte, I2CWriteData)
 
  For x = 1 To I2CReadByte
    LCD.Write(0*8, (x+3)*10, Text.GetSubText("  "+(I2CReg+x-1),Text.GetLength((I2CReg+x-1)),3) + ":")
    LCD.Write(6*8, (x+3)*10, Text.GetSubText("  "+I2CReadData[x-1],Text.GetLength(I2CReadData[x-1]),3))
    LCD.Write(12*8, (x+3)*10, Text.GetCharacter(I2CReadData[x-1]))
  EndFor
  Value = (I2CReadData[1]*256 + I2CReadData[0])
  LCD.Write(0*8, (I2CReadByte+5)*10, "Data: "+ Text.GetSubText("    "+(Value),Text.GetLength((Value)),5))
 
  Clicks = Buttons.GetClicks()
  If Text.IsSubText(Clicks, "E") Then
    Finished = "True"
  EndIf
EndWhile


(Note: I could not get the Ready Flag part to work which is why it is commented out.)

Zurück zu „textbasierte Programmiersoftware“

Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder und 25 Gäste

Lego Mindstorms EV3, NXT und RCX Forum : Haftungsauschluss