irgendwie stimmt dein code nicht, nimm diesen mal und bring deine case rein und deine atmegaanpassungen.
Code:
Declare Function Srf02_firmware(byval Slaveid As Byte) As Byte
Declare Function Srf02_entfernung(byval Slaveid As Byte) As Integer
$regfile = "m32def.dat"
$framesize = 42
$swstack = 42
$hwstack = 42
$crystal = 8000000 'Quarzfrequenz
$baud = 19200
Config Scl = Portc.0 'Ports fuer IIC-Bus
Config Sda = Portc.1
Const Srf02_slaveid = &HE0 'Standard I2C Adresse von SRF02
Dim Entfernung As Integer
Dim V As Byte
Wait 1
I2cinit
Print "SRF02 Ultraschall-Firmware Version:" ; Srf02_firmware(srf02_slaveid)
Do
Entfernung = Srf02_entfernung(srf02_slaveid)
Print "Entfernung:" ; Entfernung ; "cm"
Wait 1
Loop
End
'------------- 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
Lesezeichen