Angeregt durch das Thema 2 Atmegas über serielle ganz einfach habe ich mal eine erste einfache Variante mit TWI gebastelt.
Die "Kauf-LIB" für den Slave braucht man aber dafür, sie kostet nicht die Welt und lohnt sich, wie man sieht.
Der TWI Master
Alles anzeigen
und der TWI Slave
Alles anzeigen
Und hier noch das Video
Die "Kauf-LIB" für den Slave braucht man aber dafür, sie kostet nicht die Welt und lohnt sich, wie man sieht.
Der TWI Master
BASCOM-Quellcode
- 'I2C Master
- 'Test von I2C/TWI mit ATMega8
- $regfile = "m8def.dat"
- $crystal = 8000000
- $hwstack = 64
- $swstack = 64
- $framesize = 64
- $lib "i2c_twi.lbx" ' we do not use software emulated I2C but the TWI
- Config Scl = Portc.5 ' we need to provide the SCL pin name
- Config Sda = Portc.4 ' we need to provide the SDA pin name
- Config Twi = 100000
- Dim Befehl As Byte , Wert As Byte
- I2cinit ' we need to set the pins in the proper state
- 'Twbr = 12 'bit rate register
- 'Twsr = 0 'pre scaler bits
- Do
- 'Begrüßung
- Befehl = 0
- Wert = 0
- Gosub Sende_twi_daten
- Wait 2
- 'Befehl 1 mit Wert 0
- Befehl = 1
- Wert = 0
- Gosub Sende_twi_daten
- Wait 2
- Befehl = 1
- Wert = 1
- Gosub Sende_twi_daten
- Wait 2
- Befehl = 1
- Wert = 2
- Gosub Sende_twi_daten
- Wait 2
- 'Display löschen
- Befehl = 2
- Wert = 0
- Gosub Sende_twi_daten
- Wait 5
- Loop
- End
- Sende_twi_daten:
- I2cstart
- I2cwbyte &H70 ' slave address write
- I2cwbyte Befehl '&B10101010 ' write command
- I2cwbyte Wert
- I2cstop
- Return
- 'Ende
und der TWI Slave
BASCOM-Quellcode
- 'I2C Slave
- 'Test von I2C/TWI mit ATMega8
- $regfile = "m8def.dat"
- $crystal = 8000000
- $hwstack = 64
- $swstack = 64
- $framesize = 64
- $lib "i2c_twi-slave.lbx" 'Für Hardware TWI
- Config Twislave = &H70 , Btr = 2 , Bitrate = 100000
- Config Lcdpin = Pin , Db4 = Portd.4 , Db5 = Portd.5 , Db6 = Portd.6 , Db7 = Portd.7 , E = Portd.2 , Rs = Portd.3
- Config Lcd = 16 * 2
- Enable Interrupts
- Dim Befehl As Byte , Wert As Byte
- Cursor Off
- Cls
- Do
- Loop
- End
- Twi_stop_rstart_received:
- Return
- Twi_addressed_goread:
- Return
- Twi_addressed_gowrite:
- Return
- Twi_gotdata:
- Select Case Twi_btw
- Case 1 : Befehl = Twi
- Case 2 : Wert = Twi
- End Select
- Select Case Befehl
- Case 0 :
- 'Begrüßung
- Locate 1 , 1
- Lcd " I2C Slave Test "
- Locate 2 , 1
- Lcd " Bascomforum.de "
- Case 1 :
- 'Befehl 1 mit drei Werten
- Locate 1 , 1
- Lcd "Befehl " ; Befehl ; " "
- Select Case Wert
- Case 0 :
- Locate 2 , 1
- Lcd "Wert " ; Wert ; " "
- Case 1 :
- Locate 2 , 1
- Lcd "Wert " ; Wert ; " "
- Case 2 :
- Locate 2 , 1
- Lcd "Wert " ; Wert ; " "
- End Select
- Case 2 : Cls
- End Select
- Return
- Twi_master_needs_byte:
- Return
- Twi_master_need_nomore_byte:
- Return
- 'Ende
Und hier noch das Video
Eine Lösung habe ich nicht, aber mir gefällt Ihr Problem.