Shop OBEX P1 Docs P2 Docs Learn Events
I2C scanner ? — Parallax Forums

I2C scanner ?

BS2p has this I2CIN and I2COUT set of commands that might help to interface the I2C devices.
I wonder if anybody came up with a code that figures out the addresses of what's been connected to the bus.

Sort of like a "I2C scanner"

Comments

  • kwinnkwinn Posts: 8,697
    Shouldn't be hard to scan through all the addresses to see which ones return an ACK, but I'm not sure how difficult it would be to identify the chip at that address.
  • I made one in Spin, but it should be easy to transcribe:
    PUB WhoOnBus(ptrOnBusArr) | onbus, addr                             'Fills an array with max 119 elements with addresses that get a response
                                                                        'and writes how many is onbus to the 0th element
       onbus:= 1
       
       REPEAT addr FROM %0000_1000 TO %0111_0111                        'Scan the entire address space, exept for reserved spaces
         IF CallChip(addr << 1)== ACK                                   'If a chip acknowledges,
           LONG[ptrOnBusArr][onbus]:= addr                              'put that address in the callers array
           LONG[ptrOnBusArr][0]:= onbus                                 'and update the total count of chips on the bus
           onbus++                                                      
           IF onbus> 119                                                'until loop expires or maximum number of elements in the array is reached
             Stop
             QUIT
         Stop                                                           'After each call send a stop signal to avoid confusion
    

    Erlend
  • Erlend, don't you need to put some provision for time gap expired while waiting for ACK?
  • True, but I2C is so fast, and Spin is so comparatively slow, that the few executions in the loop is sufficient. Same with any intrepeted lanuage I would think.

    Erlend
  • Tracy AllenTracy Allen Posts: 6,656
    edited 2017-02-11 18:33
    While it is not documented, the I2CIN and I2COUT commands will themselves time out if they do not receive the expected data. I forget the exact duration. Unlike SERIN there is no provision for a timeout vector, and there is no error code associated with a timeout. It is easy enough to test for i2c inputs that you expect to be present by reading a register for valid response. However, just to say "hello" to an arbitrary address, I think maybe the only thing to go on would be the timing of the response, and the Stamp does not have a built-in timer to support that. Good question.
Sign In or Register to comment.