Hi,
um also einen Atemga8 zum Portexpander als TWI Slave zu machen, könnte ich doch die Beispielrotine aus der Bascom Hilfe nutzen:
Code:
$regfile="meinemega8konfiguration"' specify the used micro
$crystal= 8000000 ' used crystal frequency
$baud= 19200 ' use baud rate
$hwstack= 32 ' default use 32 for the hardware stack
$swstack= 10 ' default use 10 for the SW stack
$framesize= 40 ' default use 40 for the frame space
' Not all AVR chips have TWI (hardware I2C)
' IMPORTANT : this example ONLY works when you have the TWI slave library
' which is a commercial add on library, not part of Bascom
Print"MCS Electronics TWI-slave demo"
Config Twislave =&H70 , Btr = 1 , Bitrate = 100000
'as you might need other interrupts as well, you need to enable them manual
EnableInterrupts
'this is just an empty loop but you could perform other tasks there
Do
nop
Loop
End
'A master can send or receive bytes.
'A master protocol can also send some bytes, then receive some bytes
'The master and slave must match.
'the following labels are called from the library
Twi_stop_rstart_received:
Print"Master sent stop or repeated start"
Return
Twi_addressed_goread:
Print"We were addressed and master will send data"
Return
Twi_addressed_gowrite:
Print"We were addressed and master will read data"
Return
'this label is called when the master sends data and the slave has received the byte
'the variable TWI holds the received value
Twi_gotdata:
Print"received : "; Twi
Return
'this label is called when the master receives data and needs a byte
'the variable twi_btr is a byte variable that holds the index of the needed byte
'so when sending multiple bytes from an array, twi_btr can be used for the index
Twi_master_needs_byte:
Print"Master needs byte : "; Twi_btr
Twi = 65 ' twi must be filled with a value
Return
'when the mast has all bytes received this label will be called
Twi_master_need_nomore_byte:
Print"Master does not need anymore bytes"
Return
Damit wäre dann mein Mega8 als Slave konfiguriert wenn ich das richtgi verstehe. Und in der Variable TWI ist mein Empfengener Wert.
Die Bascom-Hilfe schreibt:
' IMPORTANT : this example ONLY works when you have the TWI slave library
' which is a commercial add on library, not part of Bascom
Weiß jemand was es damit auf sich hat?
Meinem Mega32 als Sender und Master würde dann so folgenden COde bekommen (ebenfalls aus der Hilfe):
Code:
Config Sda = Portb.5
Config Scl = Portb.7
Dim X AsByte, A AsByte, Bytes AsByte
X = 5 'assign variable to 5
Dim Ax(10)asByte
Const Slave =&H40 'slave address of a PCF 8574 I/O IC
I2csend Slave , X 'send the value or
For A = 1 To 10
Ax(a)= A 'Fill dataspace
Next
Bytes = 10
I2csend Slave , Ax(1), Bytes
End
Habe ich das richtig verstanden, dass das schon meine Antwort auf die Portexpanderung wäre? Meinem Salve würde ich natürlich dann noch in ner Schleife sagen was er mit den empfangenen Daten machen soll also z.B.
Code:
if twi = 000001 then
porta.1 = 1
oder ähnlich...
was mein Ihr?
Viele Grüße
Thomas
Lesezeichen