was mich mal intressieren würde, woher hast du die demo programme? ich habe diese nirgens in den samples gefunden...
ich habe folgende demos benützt:
für den 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
'-------------------------------------------------------------------------------
$regfile = "M128def.dat" ' the chip we use
$crystal = 8000000 ' crystal oscillator value
$baud = 19200 ' baud rate
Print "MCS Electronics TWI-slave demo"
#if _build >= 11175
Config Twislave = &H70 , Btr = 1 , Bitrate = 100000
' the code below #else is executed by the compiler when you use version 11175 or higher
#else
'as this is available in 1.11.7.5 some manual work mus tbe done
$lib "i2c_twi-slave.lbx" ' include
$external _twi_slave 'include code from the lib
Const _twisdaport = Portd
Const _twisclport = Portd
Const _twisdaddr = Ddrd
Const _twisclddr = Ddrd
Const _twisda = 1
Const _twiscl = 0
Const Twi_cbtr = 1 'how many bytes we will receive
Dim Twi As Byte , Twi_btr As Byte , Twi_btw As Byte
rcall _twi_init
Twbr = 12 'bit rate register
Twsr = 0
Twar = &B01110000 ' own slave address
Twcr = &B01000101 'general call not enabled
On Twi _twi_slave_isr Nosave
Enable Twi
#endif
'as you might need other interrupts as well, you need to enable them 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 "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
wenn der master nun daten sendet springt der slave in folgende sub und in der variabel twi steht das byte das der master gesendet hat dieses muss dann natürlich in dieser sub in eine adere variabel gespeichert werden um es weiterzuverarbeiten...
Twi_gotdata:
Print "received : " ; Twi
Return
und wenn der master daten anfordert springt er in folgende sub, und das byte das der master empfangen soll muss in twi gesoeichert werden...
Twi_master_needs_byte:
Print "Master needs byte : " ; Twi_btr
Twi = 65 ' twi must be filled with a value
Return
und wenn deine version von Bascom höher ist als "if _build >= 11175" kannst du in der ersten if..then... abfragung die else anweisung löschen...
und noch den mega32 einsetzen, crystal und die baudrate anpassen...
für den master folgendes beispiel:
Code:
'-------------------------------------------------------------------------------
' (c) 2004 MCS Electronics
' This demo shows an example of the TWI
' Not all AVR chips have TWI (hardware I2C)
'-------------------------------------------------------------------------------
'The chip will work in TWI/I2C master mode
'Connected is a PCF8574A 8-bits port extender
$regfile = "M8def.dat" ' the used chip
$crystal = 4000000 ' frequency used
$baud = 19200 ' baud rate
$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
'On the Mega8, On the PCF8574A
'scl=PC5 , pin 28 pin 14
'sda=PC4 , pin 27 pin 15
I2cinit ' we need to set the pins in the proper state
Config Twi = 100000 ' wanted clock frequency
'will set TWBR and TWSR
'Twbr = 12 'bit rate register
'Twsr = 0 'pre scaler bits
Dim B As Byte , X As Byte
Print "TWI master"
Do
Incr B ' increase value
I2csend &H70 , B ' send the value
Print "Error : " ; Err ' show error status
I2creceive &H70 , X ' get a byte
Print X ; " " ; Err ' show error
Waitms 500 'wait a bit
Loop
End
hier must du noch den mega8 in mega32 umändern und die ports der I2C leitungen ändern...
mit I2csend &H70 , B wird die variabel B über I2C an den slave mit der hex adresse 70 gesendet
mit I2creceive &H70 , X wird ein byte vom slave mit der adresse 70 angefordert und in x gespeichert....
ich hoffe es funktioniert jetzt damit...
gruss bluesmash
Lesezeichen