AnsweredAssumed Answered

Creating an i2c address scanner

Question asked by Stephen Cripps on Nov 18, 2014
Latest reply on Nov 18, 2014 by Erich Styger

Hello,

 

I'm looking to write some code that will scan the i2c bus for devices. From what I understand from existing i2cspy implementations, you:

 

  • send a start condition
  • send an address over the bus
  • check to see if an ack was received
  • send a stop condition
  • repeat

 

So I'm still learning the libraries provided by processor expert, and attempted to do this with the select slave method and receive block, so it would look like:

 

  LDD_TDeviceData iic_handle = CI2C1_Init( &flags );

 

  for( byte addr = 0x10; addr < 0x70; addr++ ) {

      printf("S: %02x ", addr);

 

      while( CI2C1_SelectSlaveDevice( iic_handle, LDD_I2C_ADDRTYPE_7BITS, addr ) != ERR_OK );

      CI2C1_MasterSendBlock( iic_handle, NULL, 0, LDD_I2C_SEND_STOP );

      while( flag == 0 );

      // ...

  }

 

Where the flags variable is set by an interrupt event, and would be checked later to see what kind of event it was. I also tried this using the non-LDD "Internal I2C" device, and found that it had a nice event for SCL Timeout, which doesn't appear to be present in the LDD version.


Has anyone tried doing something like this before? I'm going to see if I can access more fine grained control over the device by looking at the generated code and the Technical manual for the device family, but wanted to know if anyone else had tried this first.


Thanks

 

Accidentally posted half the message instead of the whole thing

Outcomes