- 12V Akku mit 280 Ah bauen         
Seite 4 von 5 ErsteErste ... 2345 LetzteLetzte
Ergebnis 31 bis 40 von 41

Thema: M8 TWI-slave in M32 umschreiben.

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

    E-Bike
    ne der sendet mir immer noch diese Zahlen:
    X113
    X196
    X136
    X113
    X113
    X196
    X136
    X113
    X113
    X196
    X136
    X113
    X113
    X196

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

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

  4. #34
    Erfahrener Benutzer Roboter-Spezialist
    Registriert seit
    24.04.2005
    Ort
    Bayern
    Alter
    38
    Beiträge
    336
    ich hab jetzt mal master und slave getauscht aber der gibt mir immer noch diese 3 Zahlen aus.

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

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

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

  8. #38
    Erfahrener Benutzer Roboter-Spezialist
    Registriert seit
    24.04.2005
    Ort
    Bayern
    Alter
    38
    Beiträge
    336
    so funktionierts nun aber warum?

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

  9. #39
    Erfahrener Benutzer Begeisterter Techniker
    Registriert seit
    26.07.2004
    Ort
    München
    Alter
    58
    Beiträge
    298
    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

  10. #40
    Erfahrener Benutzer Roboter Genie
    Registriert seit
    16.04.2005
    Ort
    Aarau
    Alter
    42
    Beiträge
    982
    @Zeroeightfifteen: wollte mal fragen ob dein slave jetzt funktioniert...

    gruss bluesmash

Seite 4 von 5 ErsteErste ... 2345 LetzteLetzte

Berechtigungen

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

Labornetzteil AliExpress