View previous topic :: View next topic |
Author |
Message |
barryg
Joined: 04 Dec 2006 Posts: 41
|
i2c slave, set a variable address |
Posted: Tue Dec 18, 2007 8:09 pm |
|
|
I've got an application where I'm talking to several identical cards. Each card has a PIC set up as an i2c slave. Each card needs to operate at a different i2c address. For this go 'round, I can get an address off some ID bits from the backplane. Later, I may have to do this again but have some kind of arbitration where the slave talks on address 0 and then switches to its own address.
So I tried putting a variable in the #USE statement and found that to be illegal. This means I will have to stuff the address into SSPADD at some point. What I'm concerned about is having the i2c library changing SSPADD on me. I also have master code on some other pins so I have to put #USE i2c here and there to be sure the right bus is in use.
I also don't want to specify some random address and hope to change it over before accidentally receiving something at that address. I seem to be able to #USE i2c(SLAVE) without specifying an address. Does that really leave it as is?
I know I can examine the LST file and maybe discover where the code is, but I'd rather have some real knowledge about it, and not base my code on some reverse-engineered theories.
Thanks. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
barryg
Joined: 04 Dec 2006 Posts: 41
|
|
Posted: Wed Dec 19, 2007 4:30 pm |
|
|
I said I was hoping to find a method I'd have more confidence in, but I guess this is the way for now.
While I'm here, some more findings:
Code: | #use i2c(SLAVE, SCL=SCL_SLAVE, SDA=SDA_SLAVE, address=0x83, FORCE_HW)
MOVLW 83
MOVWF FC8 | An odd address is sort of nonsense, but it does what you tell it...
Code: | #use i2c(SLAVE, SCL=SCL_SLAVE, SDA=SDA_SLAVE, FORCE_HW)
CLRF FC8 | Leaving out the address results in it being set to zero, rather than being skipped!
But it does happen only in main (so far). |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Dec 19, 2007 4:38 pm |
|
|
The address should not be odd. Here is an example of how to change
the slave address in your program:
Code: |
#byte SSPADD = 0xFC8
void main()
{
int8 slave_address;
slave_address = 0x80;
SSPADD = slave_address;
while(1);
} |
|
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Thu Dec 20, 2007 9:58 am |
|
|
Like PCM stated, your slave address cannot be odd. This is because the LSB is used to determine if the command is a write or read.
Ronald |
|
|
barryg
Joined: 04 Dec 2006 Posts: 41
|
|
Posted: Thu Dec 20, 2007 1:14 pm |
|
|
I thought my use of the word "nonsense" would make it clear I knew this was inappropriate. But really, I was testing to see just what the compiler would take.
As far as "cannot", well, I just did. Presumably the odd bit gets stored in SSPADD. Proving that it is, and demonstrating whether it's ignored, or just what it does to address recognition, ...is left to the student |
|
|
|