Author Topic: IIC  (Read 9499 times)

Offline Pursuit20

  • Newbie
  • *
  • Posts: 11
    • View Profile
IIC
« on: August 08, 2008, 04:31:03 PM »
Is there an example somewhere of how to setup and read from multiple slaves?  For example, I have a temp sensor and an external RTC that are both on the IIC bus.  I have read the IIC pdf but I am still unsure of how to read from both of them.  In this example, the RTC has a slave address of 0x68 and the temp sensor has an address of 0x49.  Do I need to open two interfaces or can one interface talk with multiple slaves. 

Thanks,

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3236
    • View Profile
    • uTasker
Re: IIC
« Reply #1 on: August 08, 2008, 09:16:50 PM »
Hi

You only need to open each physical interface (eg. if a processor has 2 x I2C interfaces, each has to be opened individually and each then has its own handle for communication).

In you case the addressing is purely in the message itself and only one handle exists.

Here is how the time is read from a DS1307 RTC:

    #define ADDRTC_READ        0xd1                                      // read address of DS1307
    #define ADDRTC_WRITE       0xd0                                      // write address of DS1307
    static const unsigned char ucGetTime[] =  {ADDRTC_WRITE, 0};
    static const unsigned char ucSlave[] = {7, (ADDRTC_READ), OWN_TASK}; // read 7 bytes from this address
    fnWrite(IICPortID, (unsigned char*)ucGetTime, sizeof(ucGetTime));    // set the read address
    fnRead(IICPortID, (unsigned char *)&ucSlave, 0);                     // start the read process of 7 byte


If you now want to read from an EEPROM it looks like this:

    #define ADD_EEPROM_READ   0xa5                                       // read address of I2C EEPROM
    #define ADD_EEPROM_WRITE  0xa4                                       // write address of I2C EEPROM
    static const unsigned char ucSetEEPROMAddress0[] = {ADD_EEPROM_WRITE, 0};
    static const unsigned char ucReadEEPROM[] = {16, ADD_EEPROM_READ, OWN_TASK};
    fnWrite(IICPortID, (unsigned char *)&ucSetEEPROMAddress0, sizeof(ucSetEEPROMAddress0)); // set the EEPROM address to read
    fnRead(IICPortID, (unsigned char *)&ucReadEEPROM, 0);                // start the read process of 16 bytes


Notice that the handle remains the same since it is using the same physical IIC interface.

To queue reads of both devices it would look like this:

    static const unsigned char ucGetTime[] =  {ADDRTC_WRITE, 0};
    static const unsigned char ucSlave[] = {7, (ADDRTC_READ), OWN_TASK}; // read 7 bytes from this address
    static const unsigned char ucSetEEPROMAddress0[] = {ADD_EEPROM_WRITE, 0};
    static const unsigned char ucReadEEPROM[] = {16, ADD_EEPROM_READ, OWN_TASK};
    fnWrite(IICPortID, (unsigned char*)ucGetTime, sizeof(ucGetTime));    // set the read address
    fnRead(IICPortID, (unsigned char *)&ucSlave, 0);                     // start the read process of 7 byte
    fnWrite(IICPortID, (unsigned char *)&ucSetEEPROMAddress0, sizeof(ucSetEEPROMAddress0)); // set the EEPROM address to read
    fnRead(IICPortID, (unsigned char *)&ucReadEEPROM, 0);                // start the read process of 16 bytes


Notice that the input and output buffers of the IIC interface (as defined when opening) need to be adequately large to accept all data in input and output queue at the same time.
When the reads complete, the first one read will arrive first (that is, in the order that they were requested).

In your case, you need only to change the commands to suit your IIC devices: change the read/write addresses and send the correct data to prepare the read. The RTC and EEPROM are quite similar since first a write is performed to set the internal address and then the read part is started to return a certain number of bytes starting at this internal address. Most IIC devices works very similarly to this.

Regards

Mark


Offline Pursuit20

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: IIC
« Reply #2 on: August 08, 2008, 09:39:43 PM »
Thanks Mark.

On a side note, If I want to use string.h and stdlib.h functions, is it ok to add those Libraries and if so, do you know how to do that in Codewarrior?  I will create a new thread if need be.

thanks again for your fast response.

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3236
    • View Profile
    • uTasker
Re: IIC
« Reply #3 on: August 09, 2008, 02:42:45 AM »
Hi

There are a couple of threads about this (or similar):
  http://www.utasker.com/forum/index.php?topic=96.0
  http://www.utasker.com/forum/index.php?topic=211.0
  http://www.utasker.com/forum/index.php?topic=30.0

Also try the Freescale CW forum:
  http://forums.freescale.com/freescale/board?board.id=CWCOMM

There are lots of general library threads there.

But, basically, there are no restrictions - they are simply 'in-addition' to what is already there.

Regards

Mark