also falls du mal zeit hast kannst ja mal "klappenabfrage" angugn. 2 x-beliebige bytes, die in das array geschrieben werden und dann wird die TxRoutine aufgerufen.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 ' 'pb4,pb5,pd5,pd6,pd7,pb3,pd3,pb1 klappen '******************************************************************************* '=============================================================================== '***| 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 Befehl As Byte Dim Anzahl_bytes As Byte Dim Neuesb_flagge As Byte Dim Buffer(8) As Byte Dim N As Byte 'zähler/index variable 'sound '==== Config Portb.2 = Output 'sound Dim Dauer As Word 'für peep 'Klappenabfrage Dim Bytex As Byte 'rechenvariable Dim Klappen As Byte Dim Soll_motor_r As Byte Dim Soll_motor_l As Byte Config Portb.4 = Input Config Portb.5 = Input Config Portd.5 = Input Config Portd.6 = Input Config Portd.7 = Input Config Portb.3 = Input Config Portd.3 = Input Config Portb.1 = Input 'pullups Config Portb.4 = 1 Config Portb.5 = 1 Config Portd.5 = 1 Config Portd.6 = 1 Config Portd.7 = 1 Config Portb.3 = 1 Config Portd.3 = 1 Config Portb.1 = 1 Enable Interrupts 'Init '===== Portb.2 = 0 For N = 1 To 8 Buffer(n) = 0 Next N Befehl = 0 Anzahl_bytes = 0 Soll_motor_r = 100 Soll_motor_l = 100 Call Twi_init_slave '------------------------------------------------------------------------------- '***| Hauptprogramm |*********************************************************** '------------------------------------------------------------------------------- 'auf Befehle vom master warten Do Neuesb_flagge = 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 Befehl = Twdr ' neue Daten merken Neuesb_flagge = 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 Neuesb_flagge <> 0 Then 'Register zuordnen -> Befehl Select Case Befehl Case 1 Gosub Klappen_abfrage Case 10 Gosub Beep Case 255 Gosub Softreset End Select End If Loop End '------------------------------------------------------------------------------- '***|KLappenabfrage|***************************************************************** '------------------------------------------------------------------------------- Klappen_abfrage: Incr Soll_motor_r Incr Soll_motor_l Buffer(1) = Soll_motor_r Buffer(2) = Soll_motor_l Anzahl_bytes = 1 Gosub I2c_tx Return '------------------------------------------------------------------------------- '***| beep |***************************************************************** '------------------------------------------------------------------------------- Beep: Anzahl_bytes = 1 '1 Byte holen Gosub I2c_rx Dauer = Buffer(1) '1. byte aus buffer Dauer = Dauer * 20 Portb.2 = 1 Waitms Dauer 'piiiiieep Portb.2 = 0 Return '------------------------------------------------------------------------------- '***| softreset |***************************************************************** '------------------------------------------------------------------------------- Softreset: Goto &H1C00 Return '------------------------------------------------------------------------------- '***| I2C-BUS Daten Empfangsroutinen |****************************************** '------------------------------------------------------------------------------- I2c_rx: For N = 1 To Anzahl_bytes 'erstes Byte empfangen Neuesb_flagge = 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 Buffer(n) = Twdr Neuesb_flagge = 2 End If Twcr = &B11000100 End If If Neuesb_flagge = 2 Then Exit Do Loop Next N Return '------------------------------------------------------------------------------- '***| I2C-BUS Daten Senderoutinen |****************************************** '------------------------------------------------------------------------------- I2c_tx: For N = 1 To Anzahl_bytes 'erstes Byte empfangen Neuesb_flagge = 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 = Buffer(n) Neuesb_flagge = 2 End If Twcr = &B11000100 End If If Neuesb_flagge = 2 Then Exit Do Loop Next N ' Slaveadresse setzen I2C-RX-Adr:Hex22 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
Lesezeichen