Hallo,
schau dir mal unter stehenden code an. diesen verwende ich bei mir und der läuft ganz gut.
Code:
'*******************************************************************************
' um mit dem master zu senden folgenden code benutzen
'
' I2cstart
' i2cwbyte &H22 'slave-id
' I2cwbyte 1 'für motor 1
' I2cwbyte PWM1
' I2cwbyte PWM1b 'falls die pwm variable größer als ein byte ist
' I2cstop
'
'*******************************************************************************
'===============================================================================
'***| Mikrocontroller Config |**************************************************
'===============================================================================
'Microcontroller
'================
$regfile = "m8def.dat"
$crystal = 16000000
$hwstack = 32
$swstack = 10
$framesize = 40
'TWI
'====
Declare Sub Twi_init_slave
Dim Twi_control As Byte
Dim Twi_status As Byte
Dim Command As Byte
Dim Index As Byte
Dim Newbyte As Byte
Dim I2cbyte(10) As Byte
Dim N As Byte
Enable Interrupts
'Init
'=====
For N = 1 To 10
I2cbyte(n) = 0
Next N
Command = 0
Index = 0
Call Twi_init_slave
'-------------------------------------------------------------------------------
'***| Hauptprogramm |***********************************************************
'-------------------------------------------------------------------------------
Do
Newbyte = 0
'schauen ob TWINT gesetzt ist
Twi_control = Twcr And &H80 ' Bit7 von Controlregister
If Twi_control = &H80 Then
Twi_status = Twsr And &HF8 ' Status
'wurde ein Byte geschickt?
If Twi_status = &H80 Or Twi_status = &H88 Then
Command = Twdr ' neue Daten merken
Newbyte = 1 ' merken das ein neues Byte da ist
End If
'TWINT muss immer gelöscht werden, damit es auf dem Bus weiter geht
Twcr = &B11000100 ' TWINT löschen, erzeugt ACK
End If
'wenn ein neues Byte gekommen ist verarbeiten
If Newbyte <> 0 Then
'Register zuordnen -> Befehl
Select Case Command
Case 1
Gosub Motor1
Case 2
Gosub Motor2
Case 3
Gosub Motor3
End Select
End If
Loop
End
'-------------------------------------------------------------------------------
'***| Motor 1 |*****************************************************************
'-------------------------------------------------------------------------------
Motor1:
Index = 1
'1 Byte holen / gibt an wieviele bytes geholt werden sollen hier im beispiel max. 10 "i2cbyte(1 bis 10)"
Gosub I2c_rx
'PWM1 = i2cbyte(1)
Return
'-------------------------------------------------------------------------------
'***| Motor 2 |*****************************************************************
'-------------------------------------------------------------------------------
Motor2:
Index = 1 '1 Byte holen
Gosub I2c_rx
'PWM2 = i2cbyte(1)
Return
'-------------------------------------------------------------------------------
'***| Motor 3 |*****************************************************************
'-------------------------------------------------------------------------------
Motor3:
Index = 1 '1 Byte holen
Gosub I2c_rx
'PWM3 = i2cbyte(1)
Return
'-------------------------------------------------------------------------------
'***| I2C-BUS Daten Empfangsroutinen |******************************************
'-------------------------------------------------------------------------------
I2c_rx:
For N = 1 To Index
'erstes Byte empfangen
Newbyte = 0
Do
Twi_control = Twcr And &H80
If Twi_control = &H80 Then
Twi_status = Twsr And &HF8
If Twi_status = &H80 Or Twi_status = &H88 Then
I2cbyte(n) = Twdr
Newbyte = 2
End If
Twcr = &B11000100
End If
If Newbyte = 2 Then Exit Do
Loop
Next N
Return
'-------------------------------------------------------------------------------
'***| I2C-BUS Daten Senderoutinen |******************************************
'-------------------------------------------------------------------------------
I2c_tx:
For N = 1 To Index
'erstes Byte empfangen
Newbyte = 0
Do
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 = I2cbyte(n)
Newbyte = 2
End If
Twcr = &B11000100
End If
If Newbyte = 2 Then Exit Do
Loop
Next N
Return
'-------------------------------------------------------------------------------
'***| TWI: Slavekonfiguration |*************************************************
'-------------------------------------------------------------------------------
Sub Twi_init_slave
Twsr = 0 ' status und Prescaler auf 0
Twdr = &HFF ' default
Twar = &H22 ' Slaveadresse setzen I2C-RX-Adr:Hex22
Twcr = &B01000100 ' TWI aktivieren, ACK einschalten
End Sub
Gruß,
Bammel
Lesezeichen