hallo

um das thema mal hier abzuschliessen:
ich hab den srf02 und das rn motorcontrol über I2C mit dem rn control verbunden.
mit diesem ersten einfachen code läuft das ganze:

Declare Function Srf02_firmware(byval Slaveid As Byte) As Byte
Declare Function Srf02_entfernung(byval Slaveid As Byte) As Integer


$regfile = "m32def.dat"
$framesize = 60
$swstack = 60
$hwstack = 60

$crystal = 16000000 'Quarzfrequenz
'''''''''''''''''''''lcd''''''''''''''''''

Config Lcd = 20 * 4 'wir verwenden ein 4 x 20 Zeichen Display
Cursor On

' Im I/O Mode wird jeder Prozessor Pin einzeln angegeben
Config Lcdpin = Pin , Db4 = Portb.0 , Db5 = Portb.1 , Db6 = Portb.2 , Db7 = Portb.3 , E = Portb.5 , Rs = Portb.4

Config Scl = Portc.0 'Ports fuer IIC-Bus
Config Sda = Portc.1





Dim Entfernung As Integer
Dim V As Byte

Cls
Locate 2 , 3
Lcd "moin chef"
Waitms 900
Cls
Locate 1 , 1
Lcd "RN-CONTROL V1.4 "
Cls
Waitms 800
Locate 2 , 6
Lcd "Fahrprog1"
Wait 1


I2cinit
Const Srf02_slaveid = &HE2

Cls
Locate 1 , 1
Lcd "SRF02 Ultraschall-Firmware Version:" ; Srf02_firmware(srf02_slaveid)
Wait 1

V = 1
Do

I2cinit
I2cwbyte &HE2 'Standard I2C Adresse von SRF02
Entfernung = Srf02_entfernung(srf02_slaveid)
Cls
Locate 2 , 1
Lcd "Entfernung:" ; Entfernung ; "cm"
Waitms 65
If Entfernung < 27 Then Gosub Hind

Cls
Locate 2 , 5
Lcd "vorwaerts..."


I2cstart
I2cwbyte &H58
I2cwbyte 35 'Kennung
I2cwbyte 5 'Befehl
I2cwbyte 3 'Motorwahl
I2cwbyte 1 'Richtung
I2cstop
Waitms 500
I2cstart 'Motor 1 starten (Geschwindigkeit=45
I2cwbyte &H58
I2cwbyte 35 'Kennung
I2cwbyte 2 'Befehl
I2cwbyte 1 'Motorwahl
I2cwbyte 195 'Geschwindigkeit
I2cstop
Waitms 250

I2cstart 'Motor 2 starten (Geschwindigkeit=45
I2cwbyte &H58
I2cwbyte 35 'Kennung
I2cwbyte 2 'Befehl
I2cwbyte 2 'Motorwahl
I2cwbyte 175 'Geschwindigkeit
I2cstop







Loop


End

'''''''''''''''''''''
Hind:


I2cstart
I2cwbyte &H58
I2cwbyte 35 'Kennung
I2cwbyte 2 'Befehl
I2cwbyte 3 'Motorwahl
I2cwbyte 0 'Geschwindigkeit
I2cstop

Waitms 500
I2cstart 'Motor Rechts drehen
I2cwbyte &H58
I2cwbyte 35 'Kennung
I2cwbyte 5 'Befehl
I2cwbyte 3 'Motorwahl
I2cwbyte 0 'Richtung
I2cstop
Waitms 100
I2cstart
I2cwbyte &H58
I2cwbyte 35 'Kennung
I2cwbyte 2 'Befehl
I2cwbyte 3 'Motorwahl
I2cwbyte 175 'Geschwindigkeit
I2cstop
Wait 2
I2cstart
I2cwbyte &H58
I2cwbyte 35 'Kennung
I2cwbyte 2 'Befehl
I2cwbyte 3 'Motorwahl
I2cwbyte 0 'Geschwindigkeit
I2cstop
'motor2 starten andersrum
I2cstart 'Motor Rechts drehen
I2cwbyte &H58
I2cwbyte 35 'Kennung
I2cwbyte 5 'Befehl
I2cwbyte 1 'Motorwahl
I2cwbyte 0 'Richtung
I2cstop

I2cstart
I2cwbyte &H58
I2cwbyte 35 'Kennung
I2cwbyte 2 'Befehl
I2cwbyte 1 'Motorwahl
I2cwbyte 185 'Geschwindigkeit
I2cstop
Waitms 250
I2cstart 'Beide Motoren Rechts drehen
I2cwbyte &H58
I2cwbyte 35 'Kennung
I2cwbyte 5 'Befehl
I2cwbyte 2 'Motorwahl
I2cwbyte 1 'Richtung
I2cstop
Waitms 250
I2cstart
I2cwbyte &H58
I2cwbyte 35 'Kennung
I2cwbyte 2 'Befehl
I2cwbyte 2 'Motorwahl
I2cwbyte 195 'Geschwindigkeit
I2cstop
Wait 4
I2cstart
I2cwbyte &H58
I2cwbyte 35 'Kennung
I2cwbyte 2 'Befehl
I2cwbyte 3 'Motorwahl
I2cwbyte 0 'Geschwindigkeit
I2cstop
Wait 1




Return
'------------- Hilfsfunktionen für SRF02 ----------


Function Srf02_firmware(byval Slaveid As Byte) As Byte
Local Firmware As Byte
Local Slaveid_read As Byte

Slaveid_read = Slaveid + 1

I2cstart
I2cwbyte Slaveid
I2cwbyte 0 'Leseregister festlegen
I2cstop

I2cstart
I2cwbyte Slaveid_read
I2crbyte Firmware , Nack
I2cstop

Srf02_firmware = Firmware
End Function



Function Srf02_entfernung(byval Slaveid As Byte) As Integer
Local Lob As Byte
Local Hib As Byte
Local Firmware As Byte
Local Temp As Byte
Local Slaveid_read As Byte

Slaveid_read = Slaveid + 1

'Messvorgang in starten
I2cstart
I2cwbyte Slaveid
I2cwbyte 0
I2cwbyte 81 'in Zentimetern messen
I2cstop


Waitms 65 'minimale Wartezeit für Messung bei SRF02

I2cstart
I2cwbyte Slaveid
I2cwbyte 2 'Leseregister festlegen
I2cstop


I2cstart
I2cwbyte Slaveid_read
I2crbyte Hib , Ack
I2crbyte Lob , Nack
I2cstop


Srf02_entfernung = Makeint(lob , Hib)
End Function

mfg jörg