so jetzt raucht mir der Kopf aber der Groschen ist endlich gefallen.
wie die kommunikation abläuft habe ich mit einem Byte kapiert.
Was muss ich aber verändern um den Array zu übertragen? Ich kapiers net... Ich muss doch irgendwo reinschreiben das es insgesamt 4 Bytes Datenbytes sind. Aber wie?
Diesen Code aus WiKi:
Master Receiver:
Code:
$regfile = "M8def.dat"
$crystal = 12000000
$baud = 9600
Declare Function Twi_read_byte(byval Slave As Byte) As Byte
Config Lcd = 20 * 4
Config Lcdpin = Pin , E = Portd.3 , Rs = Portd.2 , Db7 = Portd.7 , Db6 = Portd.6 , Db5 = Portd.5 , Db4 = Portd.4
Dim Twi_control As Byte '
Dim Twi_status As Byte
Dim Twi_data As Byte
Dim B As Byte
Dim Ch1 As Long
Dim Ch1counter(4) As Byte At Ch1 Overlay
Dim Error As Byte
Twcr = &B00000100
Twsr = 0
Twbr = 72
Ch1 = 0
Ch1counter = 0
Cls
Locate 1 , 1
Lcd "TWI Master Receiver"
Do
Ch1counter = Twi_read_byte(&H40)
Locate 2 , 1
Lcd "CH1:" ; Ch1
Locate 4 , 1
Lcd " Error : " ; Err
Waitms 25
Loop
End
'--------------------------------------------------------------------------------
Function Twi_read_byte(slave As Byte) As Byte
Error = 0
Twi_read_byte = 0
Twcr = &B10100100
Gosub Twi_wait_int
If Twi_status = &H08 Or Twi_status = &H10 Then
Twdr = Slave Or &H01
Twcr = &B10000100
Gosub Twi_wait_int
If Twi_status = &H40 Then
Twcr = &B10000100
Gosub Twi_wait_int
If Twi_status = &H58 Or Twi_status = &H50 Then
Twi_read_byte = Twdr
Error = 0
Else
Error = Twi_status
End If
Else
Error = Twi_status
End If
Twcr = &B10010100
Else
Twcr = &B10000100
Error = Twi_status
End If
End Function
Twi_wait_int:
Do
Twi_control = Twcr And &H80
Loop Until Twi_control = &H80
Twi_status = Twsr And &HF8
Return
Und der Slave Transmitter:
Code:
$regfile = "m8def.dat"
$crystal = 7372800
' $baud = 9600
Config Pinc.0 = Output
'------------- I2C Variablen ----------------------
Dim Twi_control As Byte
Dim Twi_status As Byte
Dim Twi_data As Byte
Dim Ch1 As Long
Dim Ch1counter(4)As Byte at Ch1 Overlay
Declare Sub Twi_init_slave
Ch1 = 0
Ch1counter = 0
Twi_data = 0
Call Twi_init_slave ' TWI aktivieren
Do
Portc.0 = 1
Incr Ch1
Twi_control = Twcr And &H80
If Twi_control = &H80 Then
Twi_status = Twsr And &HF8
If Twi_status = &HA8 Or Twi_status = &HB8 Then
Twdr = Ch1counter(4)
Portc.0 = 0
End If
Twcr = &B11000100
End If
Loop
End
'------- Unterprogramme--------------
'Mega8 als I2C Slave setzen
Sub Twi_init_slave
Twsr = 0
Twdr = &HFF
Twar = &H40
Twcr = &B01000100
End Sub
Lesezeichen