-
SRF08 - I²C Fehler...
Hallo!
Ich teste gerade das SRF08-Modul für Suny.
Ich habe es am I²C angeschlossen; mit einem AtMega8 @ 16MHz.
Folgender Code, die I²C-Funktionen sind 1:1 aus dem AVR-Buch...
Auch die Überprüfung der Fehlercodes...
Code:
$crystal = 16000000
$regfile = "m8def.dat"
$baud = 9600
Config Portb = Output
Config Portd = Output
Twsr = &B00000000 'Bits1+0=00: TWI-Vorteilung 1
Twbr = &H0C 'IC2-Bitrate 400000Hz bei 16MHz
'BitRate=AvrClock/(16+2*TWBR*4^TwiPrescale)
Declare Function Twistart() As Byte
Declare Function TwiSend(ByVal DataByte As Byte) As Byte
Declare Function TwiReceive(ByRef DataByte As Byte) As Byte
Declare Sub TwiStop()
Dim I As Byte
Dim J As Byte
Enable Interrupts
Wait 1
Print "STARTING......."
If Twistart() <> &H08 Then
Print "ERROR!" ; " Occured by sending START CONDITION!"
End If
If Twisend(&He0) <> &H18 Then
Print "ERROR!" ; " Occured by sending ADDRESS!"
End If
If Twisend(0) <> &H18 Then
Print "ERROR!" ; " Occured by sending REGISTER-ADDRESS!"
End If
If Twisend(&H51) <> &H18 Then
Print "ERROR!" ; " Occured by sending ADDRESS!"
End If
Twistop
Print "SENT MEASURING-ORDER."
Waitms 250
End
Function TwiStart() As Byte
Twcr = &B10100100 'START-Signal erzeugen
Do : Loop Until Twcr.twint = 1 'Warten bis fertig
Twistart = Twsr And &B11111000 'Status-Code zurückgeben
End Function
'----------------------------------------------------------
Function TwiSend(ByVal DataByte As Byte) As Byte
Twdr = Databyte 'Byte ins Datenregister
Twcr = &B10000100 'TWI-Operation starten
Do : Loop Until Twcr.twint = 1 'Warten bis fertig
Twisend = Twsr And &B11111000 'Status-Code zurückgeben
End Function
'----------------------------------------------------------
Function TwiReceive(ByRef DataByte As Byte) As Byte
Twcr = &B10000100 'TWI-Operation starten
Do : Loop Until Twcr.twint = 1 'Warten bis fertig
Databyte = Twdr
Twireceive = Twsr And &B11111000 'Status-Code zurückgeben
End Function
'----------------------------------------------------------
Sub TwiStop()
Twcr = &B10010100 'STOP-Signal erzeugen
End Sub
Das sieht auch ganz gut aus; aber der erste Fehler kommt nach dem senden der Registeradresse, also 0.
Dann beim Senden des Befehls.
Das sind die Ausgaben des Terminalprogramms:
Code:
STARTING.......
ERROR! Occured by sending REGISTER-ADDRESS!
ERROR! Occured by sending ADDRESS!
SENT MEASURING-ORDER.
Ich weis jetzt nicht, aber die LED des SRF flackert ganz kurz auf, wenn ich das Programm laufen lasse...
...und im DS steht, dass das dann auftritt, wenn man eine Messung befiehlt...
Was läuft da falsch? das senden der Ragisterardesse, oder halte ich das Prtokoll nicht ein?!
Ist der erste Versuch...
Danke schonmal!
Ganz liebe Grüße,
Tobi
-
Haha Mit diesem Code geht's... :D :
Und warum gehts mit meinem Hardwarecode nicht?
Code:
$crystal = 16000000
$regfile = "m8def.dat"
$baud = 9600
Config Portb = Output
Config Portd = Output
Config Pinc.5 = Output
Config Pinc.4 = Output
Config Scl = Portc.5
Config Sda = Portc.4
'Config Servos = 7 , Servo1 = Portd.2 , Servo2 = Portd.3 , Servo3 = Portd.4 , Servo4 = Portd.5 , Servo5 = Portd.6 , Servo6 = Portd.7 , Servo7 = Portb.0 , Reload = 10 'Servos initialisieren...
Twsr = &B00000000 'Bits1+0=00: TWI-Vorteilung 1
Twbr = &H0C 'IC2-Bitrate 400000Hz bei 16MHz
'BitRate=AvrClock/(16+2*TWBR*4^TwiPrescale)
Declare Function Twistart() As Byte
Declare Function Twisend(byval Databyte As Byte) As Byte
Declare Function Twireceive(byref Databyte As Byte) As Byte
Declare Sub Twistop()
Dim Msb As Byte
Dim Lsb As Byte
Dim Entfernung As Word
Enable Interrupts
Wait 1
'Servo(1) = 200
'Servo(2) = 0
Do
Print "STARTING......."
I2cstart 'Messung auslösen
I2cwbyte 0 '0 Adresse für alle Bausteine (Broadcastadresse)
I2cwbyte 0 'Register 0 - Befehlsregister
I2cwbyte 81 'Befehl 81 - Messung in cm
I2cstop
Waitms 300 'delay 65mS auf Ergebnis warten
I2cstart
I2cwbyte 224 'E0 Addresse vom 1. US
I2cwbyte 2 'Register 2 (+3) = 1. Entfernung
I2cstart
I2cwbyte 225 'Leseadresse (E1)
I2crbyte Msb , Ack 'Register 2 lesen
I2crbyte Lsb , Nack 'Register 3 lesen
I2cstop
Entfernung = Makeint(lsb , Msb) 'Word als Ergebnis von 2 Bytes bauen
Print "Entfernung: " + Str(entfernung) ; "cm."
'Print "MSB: " + Str(msb)
'Print "LSB: " + Str(lsb)
Loop
Ganz liebe Grüße,
Tobi