PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : M8 TWI-slave in M32 umschreiben.



Zeroeightfifteen
08.01.2006, 15:02
Hallo
Wie kann ich die M8 TWI-Slave so umschreiben, dass es auch auf einem Mega32 läuft?
Das Datenblatt vom Atmega32 hab ich mir auch schon etwas durchgelesen. Doch da blick ich noch nicht so durch. Mir wäre es mal lieber wenn ich mal einen Quellcode habe der auch funktioniert.



'-------------------------------------------------------------------------------
' (c) 2004 MCS Electronics
' This demo shows an example of the TWI in SLAVE mode
' Not all AVR chips have TWI (hardware I2C)
' This demo is a Mega8 I2C A/D converter slave chip
' NOTICE that this demo will only work with the TWI slave library which is avaialble as an add on
'-------------------------------------------------------------------------------
$regfile = "M32def.dat" ' the chip we use
$crystal = 16000000 ' crystal oscillator value
$baud = 9600 ' baud rate

Print "MCS Electronics M8 TWI-slave demo"
Print "Use with M8-TWI master demo"

Config Adc = Single , Prescaler = Auto
'Now give power to the chip
Start Adc

Dim W As Word
Config Portb = Output

'Dim Status As Byte
'Print Hex(status)



Twsr = 0 ' status und Prescaler auf 0
Twdr = &HFF ' default
Twcr = &B00000100 ' erstmal nur TWI aktivieren
Twar = &H40 ' Slaveadresse setzen
Twcr = &B01000100 ' dann ACK einschalten


'The variables Twi , Twi_btr and Twi_btw are created by the compiler. These are all bytes
'The TWI interrupt is enabled but you need to enabled the global interrupt


Enable Interrupts

'this is just an empty loop but you could perform other tasks there
Do
'Print Getadc(0)
'Waitms 500
nop
Loop
End


'The following labels are called from the library. You need to insert code in these subroutines

'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 when master send stop or start
Twi_stop_rstart_received:
Print "Master sent stop or repeated start"
Return

'master sent our slave address and will not send data
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 ; " byte no : " ;
Select Case Twi_btw
Case 1 : Portb = Twi ' first byte
Case 2: 'you go figure
End Select
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
Select Case Twi_btr
Case 1: ' first byte
W = Getadc(0)
Twi = Low(w)
Case 2 ' send second byte
Twi = High(w)
End Select
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

Was nicht passt sind diese Zeilen:

Twi_gotdata:
Print "received : " ; Twi ; " byte no : " ;
Select Case Twi_btw
Case 1 : Portb = Twi ' first byte
Case 2: 'you go figure
End Select
Return

Twi_master_needs_byte:
Print "Master needs byte : " ; Twi_btr
Select Case Twi_btr
Case 1: ' first byte
W = Getadc(0)
Twi = Low(w)
Case 2 ' send second byte
Twi = High(w)
End Select
Return


warum ist denn das mit dem Mega32 nicht kompatibel?Was hat der Mega8 was der Mega32 nicht hat?

Edit: Ich habe es jetzt soweit umgeschrieben.
Dim Twi As Byte , Twi_btr As Byte , Twi_btw As Byte
Diese Zeile hab ich noch eingefügt.

Doch kann ich vom master immer noch nichts empfangen. Was muss ich da noch verändern?

Dies ist das Masterprogramm:

'-------------------------------------------------------------------------------
' (c) 2004 MCS Electronics
' This demo shows an example of the M8 TWI
' Not all AVR chips have TWI (hardware I2C)
'-------------------------------------------------------------------------------

'The chip will work in TWI/I2C master mode
'Connected is another Mega8 in TWI-slave mode


$regfile = "M32def.dat" ' the used chip
$crystal = 16000000 ' frequency used
$baud = 9600 ' baud rate

$lib "i2c_twi.lbx" ' we do not use software emulated I2C but the TWI

Config Scl = Portc.0 ' we need to provide the SCL pin name
Config Sda = Portc.1 ' we need to provide the SDA pin name

'On the Mega8, On the slave Mega8
'scl=PC5 , pin 28 scl=PC5 , pin 28
'sda=PC4 , pin 27 sda=PC4 , pin 27

'the M8 slave uses a simple protocol
'WRITE -> Start-address-B1-B2-STOP
'READ -> start-address-B1-B2-STOP
'start -> I2CSTART
'address-the slave address
'B1 and B2 are 2 bytes that when written, write to B1
' when read , return A/D converter value


Dim B1 As Byte , B2 As Byte
Dim W As Word At B1 Overlay


I2cinit ' we need to set the pins in the proper state


Twbr = 12 'bit rate register
Twsr = 0 'pre scaler bits

Dim B As Byte , X As Byte
Print "Mega32 TWI master demo"

Do
I2cstart
I2cwbyte &H70 ' slave address write
I2cwbyte &B10101010 ' write command
I2cwbyte 2
I2cstop
Print "Error : " ; Err ' show error status

I2cstart
I2cwbyte &H71
I2crbyte B1 , Ack
I2crbyte B2 , Nack
I2cstop
Print "Error : " ; Err ' show error
Print "received A/D : " ; W
Waitms 500 'wait a bit
Loop
End

In welche Sub soll ich den Code im Slaveprogramm reinschreiben?
You need to insert code in these subroutines

Bluesmash
08.01.2006, 15:50
hast du die TWI_I2C_Slave.lib? ich hatte auch mal probleme auf nem mega32... aber wenn ich mich recht erinnere hatte ich schon probleme beim compilieren...
ich musste in der lib die 2. letzte zeile von TWSI=$026 auf TWI=$026

gruss bluesmash

--Edit---

dies mus man nicht in der slave lib sondern in der M32def.dat ändern...

Zeroeightfifteen
08.01.2006, 15:54
Ja die TWI I2C slave hab ich. beim compilieren gibts jetzt auch keine Probleme mehr. Ich glaube aber dass da noch im Slave etwas geschrieben werden muss. warum steht da sonst "You need to insert code in these subroutines". Ich will einfach mal ein Byte senden und empfangen. In jede richtung.

Bluesmash
08.01.2006, 16:01
wenn der master daten will muss das zu übertragende byte in twi gespeichert werden etwa so:

Twi_master_needs_byte:
twi = variable
Return

und wenn der master daten senden will springt der slave in folgende sub und twi enthält das byte das vom master gesendet wird:

Twi_gotdata:
variable = twi
Return

gruss bluesmash

Zeroeightfifteen
08.01.2006, 17:37
und was muss ich dann im master eingeben?

I2creceive &H40 , twi
und
I2csend &H40, twi
stimmt das so weit?

und was bedeutet dann das
I2cwbyte &H70 ' slave address write
I2cwbyte &B10101010 ' write command
I2cwbyte 2

muss das twi sein oder kann ich das auch anders benennen?
muss das dann zur gleichen zeit sein oder reicht das wenn ich das Byte im Slave ins Twi schreibe und z.B 10 Min später frag ich mit dem Master Twi ab oder wie funktioniert das?

hab ich da nicht die falsche Slaveadresse?
der master schreibt
I2cwbyte &H70 ' slave address write
und der slave schreibt
Twar = &H40 ' Slaveadresse setzen

Zeroeightfifteen
09.01.2006, 16:14
ich werde aus dem Datenblatt nicht schlau. kann mir keiner Helfen? Da muss es doch auch etwas deutschsprachiges geben. Ich habe die Programme nun ein bisschen gekürzt und schon einiges ausprobiert. Ich bekomm aber immer noch Error 1 und kann nichts senden und nichts empfangen.

Master:

$regfile = "M32def.dat" ' the used chip
$crystal = 16000000 ' frequency used
$baud = 9600 ' baud rate

$lib "i2c_twi.lbx" ' we do not use software emulated I2C but the TWI

Config Scl = Portc.0 ' we need to provide the SCL pin name
Config Sda = Portc.1 ' we need to provide the SDA pin name
'the M8 slave uses a simple protocol
'WRITE -> Start-address-B1-B2-STOP
'READ -> start-address-B1-B2-STOP
'start -> I2CSTART
'address-the slave address
'B1 and B2 are 2 bytes that when written, write to B1
' when read , return A/D converter value
Dim B1 As Byte , B2 As Byte
Dim W As Word At B1 Overlay

I2cinit ' we need to set the pins in the proper state

Twbr = 12 'bit rate register
Twsr = 0 'pre scaler bits

Dim B As Byte , X As Byte
Print "Mega32 TWI master demo"

Do
I2cstart
I2cwbyte &H70 ' slave address write
I2cwbyte &B10101010 ' write command
I2cwbyte 2
I2cstop
Print "Error : " ; Err ' show error status

I2cstart
I2cwbyte &H71
I2crbyte B1 , Ack
I2crbyte B2 , Nack
I2cstop
Print "Error : " ; Err ' show error
Print "received A/D : " ; W
Waitms 500 'wait a bit
Loop
End

Slave:


$regfile = "M32def.dat" ' the chip we use
$crystal = 16000000 ' crystal oscillator value
$baud = 9600 ' baud rate

Config Scl = Portc.0 ' we need to provide the SCL pin name
Config Sda = Portc.1 ' we need to provide the SDA pin name

Config Adc = Single , Prescaler = Auto
'Now give power to the chip
Start Adc

Print "MCS Electronics M8 TWI-slave demo"
Print "Use with M32-TWI master demo"

Dim W As Word
Config Porta = Output
W = 123

Dim Twi_status As Byte
Dim Twi_data As Byte
'Dim Status As Byte
'Print Hex(status)

Twsr = 0 ' status und Prescaler auf 0
Twdr = &HFF ' default
Twcr = &B00000100 ' erstmal nur TWI aktivieren
Twar = &H40 ' Slaveadresse setzen
Twcr = &B01000100 ' dann ACK einschalten

'The variables Twi , Twi_btr and Twi_btw are created by the compiler. These are all bytes
'The TWI interrupt is enabled but you need to enabled the global interrupt
Dim Twi As Byte , Twi_btr As Byte , Twi_btw As Byte

Enable Twi
Enable Interrupts
'this is just an empty loop but you could perform other tasks there
Do
Twi_gotdata:
Print "received : " ; Twi ; " byte no : " ;
Select Case Twi_btw
Case 1 : Porta = Twi ' first byte
Case 2: 'you go figure
End Select


Loop
End
'The following labels are called from the library. You need to insert code in these subroutines

'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 when master send stop or start
Twi_stop_rstart_received:
Print "Master sent stop or repeated start"
Return
'master sent our slave address and will not send data
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

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
Select Case Twi_btr
Case 1: ' first byte
W = Getadc(0)
Twi = Low(w)
Case 2 ' send second byte
Twi = High(w)
End Select
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

Das sind die Programme doch was soll das ganze im Slave bedeuten? Muss ich im Master nun noch etwas verändern oder passt das und wie sieht das im Slave aus, gehört da noch etwas gemacht?
Danke für jede Hilfe

Bluesmash
09.01.2006, 18:46
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:


'-------------------------------------------------------------------------------
' (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:



'-------------------------------------------------------------------------------
' (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

Zeroeightfifteen
09.01.2006, 18:57
Danke ich werd es gleich mal ausprobieren. Die Demos hab ich aus dem samples ordner. Die heißen M8 Twi-Master und M8 Twi-Slave. Was bedeutet denn das nop in der Do Loop schleife oder hat das keine bestimmte Bedeutung?
Wenn ich das Slave Programm für den M32 umschreibe dann meldet er mir bei der Zeile

Config Twislave = &H70 , Btr = 1 , Bitrate = 100000 Unknown Interrupt

Diese Zeile ersetz ich aber durch diese

Twsr = 0 ' status und Prescaler auf 0
Twdr = &HFF ' default
Twcr = &B00000100 ' erstmal nur TWI aktivieren
Twar = &H40 ' Slaveadresse setzen
Twcr = &B01000100 ' dann ACK einschalten
das is doch das gleiche oder?

Bluesmash
09.01.2006, 19:12
jetzt musst du in der M32def.dat die 2. letzte zeile von TWSI=$026 auf TWI=$026 ändern...
wenn du eine neuere bascom version hast verwende:

Config Twislave = &H70 , Btr = 1 , Bitrate = 100000

gruss bluesmash

--edit--
so weit ich weiss bedeutet nop = tu nichts... aber genau weiss ichs auch nicht...

Zeroeightfifteen
09.01.2006, 19:24
in der M32def.dat steht nur in der letzten zeile .equ twsiADDR=$026 ;2wire serial int
doch wenn ich Config Twislave = &H70 , Btr = 1 , Bitrate = 100000 schreibe dann meldet bascom mir immer unknown interrupt Twi
und das I2c start und stop brauch ich nicht?

Das mit deinem Beispielprogramm funktioniert auch nicht. Mit dem hab ich schon mal rum probiert.

aber was bedeutet denn das
'the M8 slave uses a simple protocol
'WRITE -> Start-address-B1-B2-STOP
'READ -> start-address-B1-B2-STOP
'start -> I2CSTART
'address-the slave address
'B1 and B2 are 2 bytes that when written, write to B1
' when read , return A/D converter value

warum kann bascom keine Demo schreiben die auch mal funktioniert?

das sind die originaldemos:

master:

'-------------------------------------------------------------------------------
' (c) 2004 MCS Electronics
' This demo shows an example of the M8 TWI
' Not all AVR chips have TWI (hardware I2C)
'-------------------------------------------------------------------------------

'The chip will work in TWI/I2C master mode
'Connected is another Mega8 in TWI-slave mode


$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 slave Mega8
'scl=PC5 , pin 28 scl=PC5 , pin 28
'sda=PC4 , pin 27 sda=PC4 , pin 27

'the M8 slave uses a simple protocol
'WRITE -> Start-address-B1-B2-STOP
'READ -> start-address-B1-B2-STOP
'start -> I2CSTART
'address-the slave address
'B1 and B2 are 2 bytes that when written, write to B1
' when read , return A/D converter value


Dim B1 As Byte , B2 As Byte
Dim W As Word At B1 Overlay


I2cinit ' we need to set the pins in the proper state


Twbr = 12 'bit rate register
Twsr = 0 'pre scaler bits

Dim B As Byte , X As Byte
Print "Mega8 TWI master demo"

Do
I2cstart
I2cwbyte &H70 ' slave address write
I2cwbyte &B10101010 ' write command
I2cwbyte 2
I2cstop
Print "Error : " ; Err ' show error status

I2cstart
I2cwbyte &H71
I2crbyte B1 , Ack
I2crbyte B2 , Nack
I2cstop
Print "Error : " ; Err ' show error
Print "received A/D : " ; W
Waitms 500 'wait a bit
Loop
End

slave:

'-------------------------------------------------------------------------------
' (c) 2004 MCS Electronics
' This demo shows an example of the TWI in SLAVE mode
' Not all AVR chips have TWI (hardware I2C)
' This demo is a Mega8 I2C A/D converter slave chip
' NOTICE that this demo will only work with the TWI slave library which is avaialble as an add on
'-------------------------------------------------------------------------------
$regfile = "M32def.dat" ' the chip we use
$crystal = 4000000 ' crystal oscillator value
$baud = 19200 ' baud rate

Print "MCS Electronics M8 TWI-slave demo"
Print "Use with M8-TWI master demo"

Config Adc = Single , Prescaler = Auto
'Now give power to the chip
Start Adc

Dim W As Word
Config Portb = Output

'Dim Status As Byte
'Print Hex(status)


Config Twislave = &H70 , Btr = 2 , Bitrate = 100000
' ^--- slave address
' ^---------- 2 bytes to receive
' ^--- bitrate is 100 KHz


'The variables Twi , Twi_btr and Twi_btw are created by the compiler. These are all bytes
'The TWI interrupt is enabled but you need to enabled the global interrupt


Enable Interrupts

'this is just an empty loop but you could perform other tasks there
Do
'Print Getadc(0)
'Waitms 500
nop
Loop
End


'The following labels are called from the library. You need to insert code in these subroutines

'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 when master send stop or start
Twi_stop_rstart_received:
Print "Master sent stop or repeated start"
Return

'master sent our slave address and will not send data
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 ; " byte no : " ; Twi_btw
Select Case Twi_btw
Case 1 : Portb = Twi ' first byte
Case 2: 'you go figure
End Select
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
Select Case Twi_btr
Case 1: ' first byte
W = Getadc(0)
Twi = Low(w)
Case 2 ' send second byte
Twi = High(w)
End Select
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

Danke für deine Bemühungen.

hüpft der Slave von alleine in die Sub oder muss ich da in der Do Loop schleife Call Master needs byte
schreiben?

Bluesmash
09.01.2006, 19:30
das sind bei mir die 3 letzten zeile:

ACI =$024 ;Analog Comparator Interrupt Vector Address
TWI=$026 ;2wire serial int
SPMR=$028 ; Store Program Memory Ready Interrupt Vector Address

dann ändere deine mal in TWI=$026

Bluesmash
09.01.2006, 19:33
mit I2csend &H70 , B
mit I2creceive &H70 , X

ich habe bis jetzt immer nur mit diesen beide befehlen gearbeitet...

Bluesmash
09.01.2006, 19:33
mit I2csend &H70 , B
mit I2creceive &H70 , X

ich habe bis jetzt immer nur mit diesen beide befehlen gearbeitet. so weit ich weiss enthalten diese befehle schon start, stop und so weiter...

Zeroeightfifteen
09.01.2006, 19:39
hab jetzt ne neue Bascom version jetzt passt das auch mit der 2. letzten zeile. Mal sehen was er jetzt zum programm sagt.

Edit:
sieht schon besser aus also die Zeile gefällt ihm jetzt schon mal.

Zeroeightfifteen
09.01.2006, 19:46
wie sieht das jetzt mit der sub aus?
erst mal muss ich ja
Declare Sub Master needs byte

Do
Call Sub Master needs byte

Loob


Sub Master needs byte
Print "Master needs byte : " ; Twi_btr
Twi = 65 ' twi must be filled with a value
Return
end sub

Bluesmash
09.01.2006, 19:52
nein die sub wird automatisch aufgerufen wen die hardware TWI meldet das der master daten will einfach genau so stehen lasen wie in der demo... kanst du den slave nicht per rs232 an einen pc hängen? dann siehts du mit der demo genau welche subs aufgerufen werden...

gruss bluesmash

Zeroeightfifteen
09.01.2006, 19:55
doch hab ich dran also muss ich auch nicht declare sub schreiben oder wie? jetzt schreibt er
We were addressed and master will send data
und was nun.
jetzt funktioniert wenigstens das schon mal.

edit:

jetzt hab ich kurz ausgesteckt dann wieder eingesteckt.
jetzt meldet er
We were addressed and master will read data
Master needs byte: 1

m.artmann
09.01.2006, 20:00
Hallo,

NOP = No OPeration zu deutsch: keine Operation
Dieser Befehl hat keinerlei Funktion und auch sonst keine Auswirkungen auf z.B. die Register einer CPU.

Gruß
m.artmann

Bluesmash
09.01.2006, 20:07
das tönt ja schon tip top :) auf jedenfall merkt der slave schonmal das der master etwas von ihm will... :)

mach mal beim master nur senden...

dann sollte der slave zuerst in folgende sub springen da er merkt dass er adressiert wurde:

Twi_addressed_goread:
Print "We were addressed and master will send data"
Return

gleich darauf springt er dann sovielmal in folgende sub wie der master bytes sendet:

Twi_gotdata:
Print "received : " ; Twi
Return

und jetzt soltle auf dem bildschirm received: "empfangene zahl" stehen...
vorausgesetzt du sendest auch eine zahl... ;)

Zeroeightfifteen
09.01.2006, 20:10
Ne er sagt nur We were addressed and master will send data
dann wieder das gleiche.
ich habe beim master jetzt dies geschrieben:
Dim B As Byte , X As Byte
Print "TWI master"
B = 125

Do
' increase value
I2csend &H70 , B ' send the value


Print X ; " " ; Err ' show error
Waitms 500 'wait a bit
Loop
End


edit:

aber auch wenn ich im master nun die scl und sda pins umstelle dann meldet der slave immer noch master will send data. kann das sein dass die scl und sda pin falsch konfiguriert sind?

Bluesmash
09.01.2006, 20:17
hmmm komisch........

eigentlich solte es jetzt funktionieren... er sollte direkt nacheinander in beide subs springen...

poste doch bitte mal deine jetzigen beiden programme...
bei mir funktiniert das so...

Zeroeightfifteen
09.01.2006, 20:31
Komisch ist das schon weil egal was ich bei scl und sda eingebe er meldet immer master needs byte

Master:

$regfile = "M32def.dat" ' the used chip
$crystal = 16000000 ' frequency used
$baud = 9600 ' baud rate


$lib "i2c_twi.lbx" ' we do not use software emulated I2C but the TWI

Config Scl = Portc.0 ' we need to provide the SCL pin name
Config Sda = Portc.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
'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"
B = 125

Do
' increase value
I2csend &H70 , B ' send the value


' show error
Waitms 500 'wait a bit
Loop
End


Slave:

$regfile = "M32def.dat" ' the chip we use
$crystal = 16000000 ' crystal oscillator value
$baud = 9600 ' baud rate

Print "MCS Electronics TWI-slave demo"


Config Twislave = &H70 , Btr = 1 , Bitrate = 100000
' the code below #else is executed by the compiler when you use version 11175 or higher

'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

Bluesmash
09.01.2006, 20:48
ich habe gerade in meinen programmen geschaut und habe festgestellt das ich da die falschen pins für sda und scl definiert habe... anscheinend spielt es keine rolle was man da eingiebt... ;)

aber im moment sehe ich keine fehler mehr in deinen prog... sollte eigentlich funktionieren so... es hat ja bei mir auch funktioniert mit nem mega32 als master und nem mega8 als slave und auch umgekehrt...

vielleicht sieht hier noch jemand anderes einen fehler...

Bluesmash
09.01.2006, 20:50
ist bei dir die verdrahtung in ordnung? hast 10k pullup's an den leitungen?

Zeroeightfifteen
09.01.2006, 20:50
Ich hab den fehler.
ich habe die baudrate auf 19200 gestellt sont war sie immer auf 9600.
jetzt funktionierts.
warum weis ich aber auch nicht.

Danke für deine Hilfe und dass du so viel Gedult mit mir gehapt hast.

nur mit dem empfangen klapts noch nicht so ganz.

Bluesmash
09.01.2006, 20:52
was?? aber das hat ja gar nichts mit dem i2c zu tun.... und die ausgabe auf den pc war ja ok?? komisch....

dank dir steige ich bald einen rang auf :)

Zeroeightfifteen
09.01.2006, 20:56
Da sendet mir der master
TWI master
X113
X196
X136
X113
X113
X196
X136
X113
X113
X196
X136
X113
X113
X196

und der Slave
MCS Electronics TWI-slave demo
We were addressed and master will read data
Master needs byte : 1
We were addressed and master will read data
Master needs byte : 1
We were addressed and master will read data
Master needs byte : 1


und das steht im Masterprogramm:

Do
' increase value
I2creceive &H71 , X ' send the value
Print "X" ; X


' show error
Waitms 500 'wait a bit
Loop
End


fehlt da nich noch eine sub mit sent oder so. weil received gibts ja auch

Bluesmash
09.01.2006, 21:03
das sieht doch schonmal nicht schlecht aus... was übergibst du den der twi variabel im slave?

das senden vom master aus klapt? hast du den beitrag oben von mir gesehen wegen der pullup's?

Zeroeightfifteen
09.01.2006, 21:09
Ja die Pullups hab ich. jeweils einen 10k an scl und sda.
die verkabelung muss ja passen da das senden vom master ja super funktioniert.
im slave steht
Twi_master_needs_byte:
Print "Master needs byte : " ; Twi_btr
Twi = 65
' twi must be filled with a value
Return

Bluesmash
09.01.2006, 21:13
komisch sind die zahlen die du im master bekommst eigentlich sollte er ja 65 zurückbekommen...

ach mal weiter oben im code:
dim b as byte
b=65

und dann in der sub twi=b

dann soltest du beim master x65 erhalten... wenn nicht kann ich im moment auch nicht sagen wiso...

Zeroeightfifteen
09.01.2006, 21:27
ne der sendet mir immer noch diese Zahlen:
X113
X196
X136
X113
X113
X196
X136
X113
X113
X196
X136
X113
X113
X196

Bluesmash
09.01.2006, 21:35
du hast da im masterprogramm:
I2creceive &H71 , X

hast du die slaveadresse auch auf 71 geändert? ich kann mir nicht vorstellen woher der diese zahlen (113,136,196) bekommt...

Zeroeightfifteen
09.01.2006, 21:42
ja das hab ich im master wieder auf 70 geändert. ich habs nur mal ausprobiert dass man dann 71 statt 70 schreiben soll wenn man was liest oder schreibt.

Zeroeightfifteen
09.01.2006, 21:47
ich hab jetzt mal master und slave getauscht aber der gibt mir immer noch diese 3 Zahlen aus.

Bluesmash
09.01.2006, 22:04
ja das mit 70/71 stimmt schon aber das wird vom programm selber gemacht... die adresse besteht ja aus 8Bit, die ersten 7bit werden durch die adresse gesetzt die du vorgibst und das letze bit (LSB) das vorgibt ob gelesen oder geschrieben wird wird vom programm gesetzt... darum must du dich also nicht kümmern...

aber warum er diese zahlen zurückbekommt kann ich mir nicht erklären...
du kanst noch folgendes probieren:
schreib mal oben im prog noch:
$framesize = 300
$swstack = 300
$hwstack = 300

--edit--
im prog vom slave!

Zeroeightfifteen
09.01.2006, 22:09
Bald versteh ich gar nix mehr.

Also jetzt hab ich mal im Master die Adresse auf 40 gesetzt.
Im Slave aber noch nicht!!!!
So jetzt gibt er mir X 65 aus.
so jetzt ändere ich im Slave auch die Adresse auf 40.
Jetzt gibt er mir
X4
X8
X65
X65
X4
X8
X65
X65
X65
aus.
so was aber komisch ist, ich habe X schon länger nicht mehr auf 65 sondern auf 120.
65 hatt ich vorher mal dazwischen hab ich den atmega sicherlich 10 mal neu programmiert.

edit:

das mit $framesize = 300
$swstack = 300
$hwstack = 300
bringt auch nichts.



wenn ich die Adresse ändere ändert sich auch der Wert den ich bekomme. bei Adresse 50 bekomm ich 81
bei Adresse 60 bekomm ich 97


egal was ich im slave schreibe wenn die Adresse eine andere ist als im Master dann bekomm ich 113 wenn sie auch 70 ist bekomm ich wieder die 3 zahlen

Zeroeightfifteen
10.01.2006, 16:45
sogar wenn ich das Programm in Bascom simuliere, gibt er mir immer 113 aus.
wenn ich die Adresse auf H40 ändere dann gibt er 65 aus.

so wenn ich nun im slave Twi=1 schreibe, dann sendet er mir nur einmal
We were addressed and master will read data
Master needs byte : 1
bis Twi=64
schreibt er mir das nur einmal
wenn ich nun Twi=65 schreibe dann schreibt er mir endlos
We were addressed and master will read data
Master needs byte : 1

Ich hab gelesen, dass man für die Pullups 1,8 K braucht. Kanns daran vieleicht legen, weil ich 10 k dran hab?


edit: so also jetzt hab ichs so geschrieben:
I2cstart
I2creceive &H70 , X
I2cstop
Print "X" ; X
Print "error" ; Err ' show error
Waitms 500
und dann bekomm ich dies hier:
X113
error1
X65
error0
X113
error1
X65
error0
X113
error1
X65
error0
X113
error1


Die 65 passt jetzt aber die 113 noch nicht.

Zeroeightfifteen
10.01.2006, 20:41
so funktionierts nun aber warum?

Master:

$regfile = "M32def.dat" ' the used chip
$crystal = 16000000 ' frequency used
$baud = 19200 ' baud rate

$lib "i2c_twi.lbx" ' we do not use software emulated I2C but the TWI

Config Scl = Portc.0 ' we need to provide the SCL pin name
Config Sda = Portc.1 ' we need to provide the SDA pin name

'On the Mega8, On the slave Mega8
'scl=PC5 , pin 28 scl=PC5 , pin 28
'sda=PC4 , pin 27 sda=PC4 , pin 27

'the M8 slave uses a simple protocol
'WRITE -> Start-address-B1-B2-STOP
'READ -> start-address-B1-B2-STOP
'start -> I2CSTART
'address-the slave address
'B1 and B2 are 2 bytes that when written, write to B1
' when read , return A/D converter value


Dim B1 As Byte , B2 As Byte
Dim W As Word At B1 Overlay


I2cinit ' we need to set the pins in the proper state


Twbr = 12 'bit rate register
Twsr = 0 'pre scaler bits

Dim B As Byte , X As Byte
Print "Mega8 TWI master demo"

Do
I2cstart
I2cwbyte &H70 ' slave address write
I2cwbyte &B10101010 ' write command
I2cwbyte 2
I2cstop
Print "Error : " ; Err ' show error status

I2cstart
I2cwbyte &H71
I2crbyte B1 , Ack
I2crbyte B2 , Nack
I2cstop
Print "Error : " ; Err ' show error
Print "received A/D : " ; W
Waitms 500 'wait a bit
Loop
End

Slave:
$regfile = "M32def.dat" ' the chip we use
$crystal = 16000000 ' crystal oscillator value
$baud = 19200 ' baud rate

$lib "i2c_twi-slave.lbx"

Print "MCS Electronics M8 TWI-slave demo"
Print "Use with M8-TWI master demo"

Config Adc = Single , Prescaler = Auto
'Now give power to the chip
Start Adc

Dim W As Word
Config Portb = Output

Dim Status As Byte 'only for debug
'Print Hex(status)

Config Twislave = &H70 , Btr = 2 , Bitrate = 100000
' ^--- slave address
' ^---------- 2 bytes to receive
' ^--- bitrate is 100 KHz


'The variables Twi , Twi_btr and Twi_btw are created by the compiler. These are all bytes
'The TWI interrupt is enabled but you need to enabled the global interrupt


Enable Interrupts

'this is just an empty loop but you could perform other tasks there
Do
'Print Getadc(0)
'Waitms 500
nop
Loop
End




'The following labels are called from the library. You need to insert code in these subroutines
'Notice that the PRINT commands are remarked.
'You can unmark them and see what happens, but it will result in occasional errors in the transmission
'The idea is that you write your code in the called labels. And this code must execute in as little time
'as possible. So when you slave must read the A/D converter, you can best do it in the main program
'then the data is available when the master needs it, and you do not need to do the conversion which cost time.


'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 when master send stop or start
Twi_stop_rstart_received:
' Print "Master sent stop or repeated start"
Return

'master sent our slave address and will not send data
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 ; " byte no : " ; Twi_btw
Select Case Twi_btw
Case 1 : Portb = Twi ' first byte
Case 2: 'you can set another port here for example
End Select
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
Select Case Twi_btr
Case 1: ' first byte
W = 100 'in this example the conversion is done here
' but a better option would have been to just pass the value of W and do the conversion in the main loop
Twi = Low(w)
Case 2 ' send second byte
Twi = High(w)
End Select
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

Was bedeutet das Case 1 und 2?
wie kann ich das umschreiben damit er nur 1 Byte sendet?
wenn ich case 2 weg mache kommt wieder irgendwas raus.

m.artmann
10.01.2006, 22:16
Wenn der Master zwei Bytes anfordert (B1 und B2) dann muß der Slave
auch zwei Bytes liefern ( Config Twislave = &H70 , Btr = 2 , Bitrate = 100000).
Wenn Du nur ein Byte zurück schickst kommt er irgendwie durcheinander.

Gruß
m.artmann

Bluesmash
14.01.2006, 21:51
@Zeroeightfifteen: wollte mal fragen ob dein slave jetzt funktioniert...

gruss bluesmash

Zeroeightfifteen
15.01.2006, 11:52
Ja jetzt funktioniert er schon mal so weit. Hab die letzten Tage nicht viel daran gearbeitet. aber nun kann ich ein byte senden und empfangen. das Problem war, dass meine printbefehle zu lang waren. keine ahnung warum das nicht funktioniert hat. Jetzt hab ich alle printbefehle gelöscht und nun funktionierts. Aber danke für deine super Hilfe
jetzt versteh ich erst einmal wie der bus überhaupt arbeitet.