CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

pic as i2c client, run time determination of i2c address

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
jds-pic



Joined: 17 Sep 2003
Posts: 205

View user's profile Send private message

pic as i2c client, run time determination of i2c address
PostPosted: Tue Nov 08, 2005 9:21 am     Reply with quote

i have an application with several i2c client PICs hanging off an i2c bus.
each, of course, needs to have a unique i2c client address.

i would like to use the same code in all PICs, and have them set up the client address at run time. i have set a few pins aside on the PIC for addressing, much as you would use A0, A1, A2 on a serial eeprom.

the CCS directive for setting a slave up looks as follows:
#use i2c(Slave,Fast,sda=PIN_SDA,scl=PIN_SCL,force_hw,address=0xa0)
for address 0.

i know from past expericence using multiple master i2c directives that the following doesn't work:
address=get_address_from_pins();
if (address==0)
#use i2c(Slave,Fast,sda=PIN_SDA,scl=PIN_SCL,force_hw,address=0xa0)
else
#use i2c(Slave,Fast,sda=PIN_SDA,scl=PIN_SCL,force_hw,address=0xa1)
...

but, it is possible to write two complete sets of functions for the two i2c addresses. however, this is very cumbersome when using interrupts, as you always would with client i2c.

so my question is, what is the best way to set the client i2c address at runtime?

thanks
jds-pic
Ttelmah
Guest







PostPosted: Tue Nov 08, 2005 9:50 am     Reply with quote

Realistically, probably just change the I2C address register directly. So something like:
Code:

#use i2c(Slave,Fast,sda=PIN_SDA,scl=PIN_SCL,force_hw,address=0x00)
#if defined(__PCM__)
#byte SSPADD=0x93
#elif defined(__PCH__)
#byte SSPADD=0xFC8
#endif
#define set_I2C_ADDRESS(x) SSPADD=(X & 0xFE)


Then you can just use 'set_I2C_ADDRESS(value)' when required.

Best Wishes
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Tue Nov 08, 2005 9:53 am     Reply with quote

Assign the address directly to the SSPADD register. However, if your code switches between master and slave mode then the address needs to be set each time the device is put back into slave mode (SSPADD is used as a baud rate generator for master mode). This probably won't be a problem for you though. You should probably scan the lst file to make sure that the SSPADD is being set from somewhere that you don't expect.
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Tue Nov 08, 2005 3:10 pm     Reply with quote

I've used this code in slaves before. I have a system that has 10 identical slave PIC's hooked up to the same I2C bus with the identical code in each one. On power-up, each Slave is assigned address 0x90. It will then read certain I/O pins and depending on which ones are high and low it will modify it's address. It's made so that any pin can be used. They don't need to be on the same port either.

Code:

int8 slave_addr;// global var

main()
{
  get_add();
  SSPADD = slave_addr;

  while(1)
  {
    // code body goes here
  }
}

void get_add(void)        // possible combinations
{// base address is 0x90   0x90 92 94 96 98 9A 9C 9E A0 A2  BASE:1001 0000

  if(input(PIN_A0))    //     0  1  0  1  0  1  0  1  0  1
  {
    bit_set(SLAVE_ADDR, 1);
  }

  if(input(PIN_A7))//         0  0  1  1  0  0  1  1  0  0
  {
    bit_set(SLAVE_ADDR, 2);
  }

  if(input(PIN_A6))//         0  0  0  0  1  1  1  1  0  0
  {
    bit_set(SLAVE_ADDR, 3);
  }

  if(input(PIN_B7))//         0  0  0  0  0  0  0  0  1  1
  {
    bit_clear(SLAVE_ADDR, 4);
    bit_set(SLAVE_ADDR, 5);
  }
}// end of get_add()



Clear as mud? Wink

Ronald
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group