Hallo
Da bei mir der Slave controller im Interrupt empfangem muss, habe ich mir die MCS I2CSlave Library zugelegt.
Leider habe es noch nicht zum laufen gebracht. Der Master sendet periodisch so wie der code zeigt,
habe mit den Oszilloskope gemessen, aber sobald ich die Slave Platine auch and den I2C Bus anschliesse
sinken die Impulse auf der SDA Leitung auf 0,6Vss, SCL bleibt bei 5Vss, unterbreche ich die SDA Leitung
steigt am Master die SDA Impulse wieder auf 5Vss. An beide Paltinen sind Atmega2561 und an beide controller
an den SCL und SDA pins jeweils 5,6K pull up widerstände. Habe es auch schon beide Widerstände auf eine der
Platinen entfernt, doch auch ohne Erflog. Weiss jemand was ich falsch mache?
Gruss
Sato
Hier der code für den Master un Slave
Master:
Code:
'-------------------------------------------------------------------------------
' (c) 2004 MCS Electronics
' This demo shows an example of the TWI
' Not all AVR chips have TWI (hardware I2C)
'-------------------------------------------------------------------------------
$hwstack = 200
$swstack = 200
$framesize = 200
'-------------------------------------------------------------------------------
$regfile = "m2561def.dat"
$crystal = 16000000
Open "Com2:" For Binary As #1
$baud1 = 9600
'
$lib "i2c_twi.lbx" ' we do not use software emulated I2C but the TWI
Config Scl = Portd.0 ' we need to provide the SCL pin name
Config Sda = Portd.1 ' we need to provide the SDA pin name
I2cinit ' we need to set the pins in the proper state
Config Twi = 100000 ' wanted clock frequency
Const Slave = &H82 'slave address
'
Dim Dados(4) As Byte , Bytes As Byte
Dados(1) = 80
Dados(2) = 02
Dados(3) = 76
Dados(4) = 90
'
Print #1 , "TWI master"
Bytes = 4
Do
I2csend Slave , Dados(1) , Bytes ' send the value
Print #1 , "ER4 " ; " " ; Err ' show error status
Waitms 100
Loop
End
Slave:
Code:
'-------------------------------------------------------------------------------
' (c) 2004 MCS Electronics
' This demo shows an example of the TWI in SLAVE mode
' 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
'Use this sample in combination with i2cscan.bas and/or twi-master.bas
'-------------------------------------------------------------------------------
$hwstack = 200
$swstack = 200
$framesize = 200
'-------------------------------------------------------------------------------
$regfile = "m2561def.dat"
$crystal = 16000000
Open "Com2:" For Binary As #1
$baud1 = 9600
Print #1 , "MCS Electronics TWI-slave demo"
Config Twislave = &H82 , Btr = 4 , Bitrate = 100000
'In I2C the address has 7 bits. The LS bit is used to indicate read or write
'When the bit is 0, it means a write and a 1 means a read
'When you address a slave with the master in bascom, the LS bit will be set/reset automatic.
'The TWAR register in the AVR is 8 bit with the slave address also in the most left 7 bits
'This means that when you setup the slave address as &H70, TWAR will be set to &H0111_0000
'And in the master you address the slave with address &H70 too.
'The AVR TWI can also recognize the general call address 0. You need to either set bit 0 for example
'by using &H71 as a slave address, or by using GENCALL=1
'
'as you might need other interrupts as well, you need to enable them all manual
Enable Interrupts
'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 #1 , "Master sent stop or repeated start"
Return
Twi_addressed_goread:
Print #1 , "We were addressed and master will send data"
Return
Twi_addressed_gowrite:
Print #1 , "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 #1 , "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 #1 , "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 #1 , "Master does not need anymore bytes"
Return
Lesezeichen