- 12V Akku mit 280 Ah bauen         
Seite 1 von 5 123 ... LetzteLetzte
Ergebnis 1 bis 10 von 41

Thema: M8 TWI-slave in M32 umschreiben.

  1. #1
    Erfahrener Benutzer Roboter-Spezialist
    Registriert seit
    24.04.2005
    Ort
    Bayern
    Alter
    38
    Beiträge
    336

    M8 TWI-slave in M32 umschreiben.

    Anzeige

    E-Bike
    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.


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

  2. #2
    Erfahrener Benutzer Roboter Genie
    Registriert seit
    16.04.2005
    Ort
    Aarau
    Alter
    41
    Beiträge
    982
    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...

  3. #3
    Erfahrener Benutzer Roboter-Spezialist
    Registriert seit
    24.04.2005
    Ort
    Bayern
    Alter
    38
    Beiträge
    336
    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.

  4. #4
    Erfahrener Benutzer Roboter Genie
    Registriert seit
    16.04.2005
    Ort
    Aarau
    Alter
    41
    Beiträge
    982
    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

  5. #5
    Erfahrener Benutzer Roboter-Spezialist
    Registriert seit
    24.04.2005
    Ort
    Bayern
    Alter
    38
    Beiträge
    336
    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

  6. #6
    Erfahrener Benutzer Roboter-Spezialist
    Registriert seit
    24.04.2005
    Ort
    Bayern
    Alter
    38
    Beiträge
    336
    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:
    Code:
    $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:

    Code:
    $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

  7. #7
    Erfahrener Benutzer Roboter Genie
    Registriert seit
    16.04.2005
    Ort
    Aarau
    Alter
    41
    Beiträge
    982
    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

  8. #8
    Erfahrener Benutzer Roboter-Spezialist
    Registriert seit
    24.04.2005
    Ort
    Bayern
    Alter
    38
    Beiträge
    336
    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
    Code:
       Config Twislave = &H70 , Btr = 1 , Bitrate = 100000
    Unknown Interrupt

    Diese Zeile ersetz ich aber durch diese
    Code:
    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?

  9. #9
    Erfahrener Benutzer Roboter Genie
    Registriert seit
    16.04.2005
    Ort
    Aarau
    Alter
    41
    Beiträge
    982
    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...

  10. #10
    Erfahrener Benutzer Roboter-Spezialist
    Registriert seit
    24.04.2005
    Ort
    Bayern
    Alter
    38
    Beiträge
    336
    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:
    Code:
    '-------------------------------------------------------------------------------
    '                            (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:
    Code:
    '-------------------------------------------------------------------------------
    '                            (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?

Seite 1 von 5 123 ... LetzteLetzte

Berechtigungen

  • Neue Themen erstellen: Nein
  • Themen beantworten: Nein
  • Anhänge hochladen: Nein
  • Beiträge bearbeiten: Nein
  •  

Labornetzteil AliExpress