Understanding I2C just a bit better..

RogerTango

Senior Member
Ive been looking at the I2C bus, and am getting ready to experiment with it some. Looks like it would be a lot better than serial coms so far.


However, I have a question...

Lest say I have these devices:


Master 1

Slave 1

Slave 2

Slave 3


Now, I know the masters talk to the slaves, but the slaves also talk to the master?

I know the slaves cannot talk to each other.


Lest say Master 1 wants to ask the temp from Slave 1, Slave 1 can respond to the master with the value, correct?

Then Master 1 wants to ask the time of day from Slave 2, then Slave 2 can respond with the information, correct?

All this on a 2 wire system seams very attractive, I just wanted to know that my understanding was correct.

It appears the slave cannot just call the master with new information, the master would have to poll the slave. All this is fine by me.

I am wanting to do a Picaxe I2C LCD interface too....



Oh, I just thought of another question... Can 2 masters reside on the same bus???


Thanks for your patience in reading my FNG post. :D

Andrew
 

RogerTango

Senior Member
I just thought of something else... Is there an "I2C DIRECTORY" of slave numbers for the various devices?

If I assign a slave value, Id like it to be 'original' so as to not conflict with an already assigned value.

Thx,
Andrew
 

mattallen37

New Member
...Now, I know the masters talk to the slaves, but the slaves also talk to the master?
The slaves can't initiate communications, but they can return requested values.
I know the slaves cannot talk to each other.

Lest say Master 1 wants to ask the temp from Slave 1, Slave 1 can respond to the master with the value, correct?

Then Master 1 wants to ask the time of day from Slave 2, then Slave 2 can respond with the information, correct?

All this on a 2 wire system seams very attractive, I just wanted to know that my understanding was correct.
Correct, correct, correct, and yes, it is very nice to be able to use I2C.
It appears the slave cannot just call the master with new information, the master would have to poll the slave. All this is fine by me.

I am wanting to do a Picaxe I2C LCD interface too....



Oh, I just thought of another question... Can 2 masters reside on the same bus???


Thanks for your patience in reading my FNG post. :D

Andrew
Correct, a slave must be polled before giving out information.

Yes, I have heard that there CAN be multiple masters, but only in rare situations, so the answer for most of us hobbyists, is NO.

I just thought of something else... Is there an "I2C DIRECTORY" of slave numbers for the various devices?

If I assign a slave value, Id like it to be 'original' so as to not conflict with an already assigned value.

Thx,
Andrew
"slave numbers"? You mean addresses and register layouts? Usually that is all in the datasheet of the parts.

Normally you don't use many devices on an I2C bus. Sure, there are 7 address bits available on most systems (127 possibilities), but I have never seen any instance where there were more than a couple devices on a bus (vary rarely even two for most hobby stuff). Don't worry too much about conflicting addresses, as there are plenty to choose from (just make it so you can change it in the future if needed).
 

westaust55

Moderator
@Andrew,

you might like to have a quick read of this document by Philips (now NXP) who invented the i2c (or iic) bus and comms protocol.
http://ics.nxp.com/support/documents/interface/pdf/i2c.bus.specification.pdf


There is a list of slave addresses by Philips/NXP but while I have seen same I do not have a copy to hand. Semiconductor manufacturers usually register their devices with Philips/NXP who then allocate the slave addresses in an organized manner. AS there only 127 addresses on the 7-bit address system (there is also a 10-bit system) many devices do use the same address.
If you have a look in PICAXE manual 2 under the i2c setup commands you will find a partial list covering many devices typically used with PICAXE chips.

For example:
all memory type devices use %10100000 (+A0) whether they are EPROM, EEPROM, RAM or F-RAM.
all IO expansion type devices use the same slave address


@mattallen37,
Wow! only one or 2 devices on hobby buses
I permanently have in my main hobby "Experimenters box":
  • 8 x memory chips of assorted types
  • 1 x DS1338 RTC
  • 1 x uMPFU Math co processor
  • 1 x SPE030 speech module
  • 2 x 8-bit IO extenders
  • 1 x SRF008 ultrasonic distance sensor with light level sensor

then I have various permanently built-up modules with i2c systems that connect into an expansion port on the experimenters box. For examples:
  • an i2c compass module with i2c a 16-bit IO expander
  • an 8x8 RGB LED matrix driven with 2 x 16-bit i2c IO expanders

Forum member InglewoodPeter was last year involved with some other "artists" who built a project involving numerous PICAXE (from memory) 28X1 chips all conencted via i2c bus. That project has been on tour around the world - Ireland recently I believe.


EDIT:
From the NXP website:
Q: Is it possible to receive a survey of all I2C-slave addresses used to date?
A: No. Philips Semiconductors never issues this list of all previously assigned slave addresses, as this is the only way we can guarantee the list stays up to date and each assigned address is unique. If this list is made available, I2C-bus licensees would start selecting slave addresses themselves and the central list would soon become incomplete, leading to address conflicts. The principle established, proven to work well, is that each licensee sends an address request to a single contact within Philips Semiconductors, who then allocates the slave address based on a single master list.
But there is an informal list somewhere out there
 
Last edited:

mattallen37

New Member
Well, then it is obvious that I haven't seen your "Experimenters box". I just haven't seen many projects with enough I2C devices that the designer had to be worried about keeping a list of address dos and don'ts.
 

hippy

Technical Support
Staff member
Ive been looking at the I2C bus, and am getting ready to experiment with it some. Looks like it would be a lot better than serial coms so far.
Have a look at the I2C Tutorial ( Help -> PICAXE 18 Datasheets ).

Now, I know the masters talk to the slaves, but the slaves also talk to the master?
No, the master puts data into the slaves or takes data from the slaves. The slaves can only talk to the master by the slave taking on the master role and the master taking on a slave role - this can get complicated so it's usually best to just have one permanent master when you can.

There is an officially defined mechanism for slaves to signal the master but it's not always implemented and is not supported by PICAXE.

I know the slaves cannot talk to each other.
They can if a slave takes on the role of a master, but again this can get complicated.

Lest say Master 1 wants to ask the temp from Slave 1, Slave 1 can respond to the master with the value, correct?
Correct, though it's more the master simply takes the data. Getting a slave to provide specific data is where I2C can get more complicated than serial.

Then Master 1 wants to ask the time of day from Slave 2, then Slave 2 can respond with the information, correct?
Yes ( as above ).

It appears the slave cannot just call the master with new information, the master would have to poll the slave. All this is fine by me.
That's correct.

Can 2 masters reside on the same bus???
Yes, but only one can be active as a master at a time. The PICAXE does not AFAIR support multi-master mode and that would have to be implemented using your own arbitration mechanisms.

Is there an "I2C DIRECTORY" of slave numbers for the various devices?
There are some 'standard numbers' used, but it's basically a free-for-all. You have to pick numbers which don't clash with other devices used in your application. The I2C Tutorial has a list of some common devices.
 

Adamey

Senior Member
I have been playing around with i2c a lot lately. For me I find it one of the best features of PICAXE's.

RogerTango, I find the best way to think of a slave PICAXE is that it behaves exactly like an EEPROM. The master can read and write data to the slave just like it was reading/writing to an EEPROM. The main difference is the data is stored in the scratchpad memory of the PICAXE. I find this concept extremely useful.

Your slave PICAXE can be executing code and the master can read & write data to/from it whenever it wants without disturbing your slaves program operation (there are some restrictions, mainly revolving around whether your slave is executing a command like serout that disables interrupts). When your master writes data there's an interrupt flag set. In this way your slave can respond to any new data that it has received by monitoring the interrupt.

There is no way for a master to know when there is data waiting in the slave without actually polling it. This is not much of a problem, as the whole idea of having a 'master' is that it keeps track of everything that's going on. For my project (1 master, 3 slaves) I simply have one byte set aside for 'flags'. I read this byte at regular intervals with my master. Depending on how the flags are set the master will know what the slave is doing and if there's data available.


The reason I like i2c so much is that it provides a simple method for sharing data between multiple PICAXE's. It's also an easy way to do multitasking. If I find that I need to do 2-3 things at the same time I find it easier to add another PICAXE than it is to try and write more complex code to "simulate" multitasking. PICAXE's are cheap, and adding an extra PICAXE makes more sense to me than wasting countless hours writing code.

I'm currently fooling around with a PICAXE quadrotor. These require a lot of calculations to be performed. The way I'm trying it is by using 3 slave PICAXE's. Each slave does a portion of the calculations (after reading sensor inputs) and places the results in scratchpad. My master only has to read the 'results' from each slave and then do the final processing to control the motors. I've almost quadrupled my processing power (I say almost since there is a small amount of overhead with i2c and having to poll my slaves). Some people might think it's a waste of time when I could buy a premade quadrotor controller, but then what would I learn?


Sorry if I seem to be rambling on, but I absolutely LOVE the concept of i2c. :)
 

RogerTango

Senior Member
Wow! So much great information to digest! It really expanded on what I have been reading.

I am awaiting my 14M2 and 18X2 so I hope to soon be tinkering with I2C physically, with better understanding I am sure to have good success!

Thanks much,
Andrew
 

PerthEng

Member
I am awaiting my 14M2 and 18X2 so I hope to soon be tinkering with I2C physically, with better understanding I am sure to have good success!

Thanks much,
Andrew
14M2 ?
18X2 ?

awaiting = already ordered ?
Tell us more. Where from ?

Though the 14M2 was a future product
Had not heard of an 18X2 - the 18M2 has recently been released and has i2c.
 

RogerTango

Senior Member
Sorry, I meant to say

18M2

and

20X2


I had it in my head I ordered the 14M2... Wish it was out...

Wouldnt mind seeing an 08X2 also! :D
 

westaust55

Moderator
Is there an "I2C DIRECTORY" of slave numbers for the various devices?
Yes of sorts as I had mentioned earlier. Now I have found a NXP version:
http://www.nxp.com/acrobat_download2/selectionguides/SELGUIDE.PDF
This give a good idea of how the i2c devices are grouped according to types.

There are also reserved address details:

Reserved Addresses
The I2C specification has reserved two sets of eight addresses,
1111XXX and 0000XXX.
These addresses are used for special purposes.

The following table has been taken from the Philips/NXP i2c specification:
Code:
[B]Slave Addr	R/W Bit	Description[/B]
000 0000       0	      General call address
000 0000	   1	      START byte
000 0001	   X	      CBUS address
000 0010	   X	      Reserved for different bus format
000 0011	   X	      Reserved for future purposes
000 01XX	   X	      Hs-mode master code
111 10XX	   X	      10-bit slave addressing
111 11XX	   X	      Reserved for future purposes
AS a result of these reserved addresses, there are in fact only 112 addresses available with the 7 bit address scheme.


Folks can also read this on the NXP website about the slave address allocation policy for non NXP parts:
http://www.nxp.com/products/interface_control/i2c/support/requestform/


If one takes for example the Microchip MCP23017 i2c IO Expander chip which has been used in various projects discussed on this forum, the slave address is in the format:
0__1__0__0 A2 A1 A0 R/W
Which is in group 4 of the slave address allocations just the same as the Philips/NXP PCF8575 Remote 16-bit I/O expander chip.
 
Last edited:

RogerTango

Senior Member
I got my 18M2 this week, I hope to play with I2C this weekend, thanks West for your friendly help and information!

Andrew
 

inglewoodpete

Senior Member
Reserved Addresses
The I2C specification has reserved two sets of eight addresses,
1111XXX and 0000XXX.
These addresses are used for special purposes.
Kind of looks complicated? It isn't really, or it needn't be.

All of those addressing rules only matter if you're going to mix some of those features or devices on your i2c bus.

If not, use any address. I use addresses from 1 to 64 on i2c slaves (28x1s set to slave mode), driven by another 28x1 configured as a master.

Actually, that network is busy working in the Science Gallery, Dublin until 25th Feb.
 

hippy

Technical Support
Staff member
Based on the I2C spec westaust55 points to one should use one of the valid 112 addresses for PICAXE I2C Slaves. In PICAXE commands these will be the even numbers ( lsb low ) $10 to $EE inclusive.

It's probably also best to avoid $A0 to $AE inclusive as these are used by most Eeprom and a casual reader may assume it was reference to Eeprom rather than an I2C Slave.
 

John West

Senior Member
While we've had similar discussions before, I'd like to reiterate my desire for the release of an 09M, for those difficult times when I'm just-one-pin-short of meeting my project goals with an 08M. :D
 

Grogster

Senior Member
I'd just like to add my support for using multiple PICAXE's if you need to do multiple things.

In my case, I have become quite a fan of the 08M chip for basic tasks such as data-filtering etc, which then feeds this data to another PICAXE of the X2 variety which is the main brain of the system.

Although not I2C in my case, the 08M can forward serial commands only when correct data has been received, and the X2 part with serin timeouts can do loops while checking for any valid commands from the 08M - works beautifully, and I too think that it is far easier to build up a system using multiple PICAXE chips then to try to squeeze everything into one chip.

But I digress...

The fact that any I2C capable PICAXE can be a master or a slave is great, as already highlighted, as you can have several PICAXE chips doing different things, which the master PICAXE can then make use of - if Adamey does not mind my paraphrasing him/her a little here... ;)
 

westaust55

Moderator
I'd just like to add my support for using multiple PICAXE's if you need to do multiple things.
Hippy has recently expounded much the same here:

The fact that any I2C capable PICAXE can be a master or a slave is great,
Not entirely true.
Only the X1 and X2 parts can be an i2c slave device
18X, 28X and 18M2 (plus future M2 parts) can only be i2c master devices

Seems Manual 2 (page 76 in V7.2) also has a typo in that the heading:
hi2csetup - slave mode (X2 parts only)
suggests only X2 parts can be i2c slave devices even though the words under that heading also includes the X1 parts.
 
Last edited:

Grogster

Senior Member
So noted. :)

In all honesty, I don't use I2C that much - yet.
I still do inter-chip comms using standard 2400 baud serial.

Mainly as chips like the 08M simply don't have I2C ability, whereas every PICAXE chip, as far as I am aware, can input and output standard 8N1 serial.
 

hippy

Technical Support
Staff member
Serial also has that advantage that if you're not sure data is being sent or correct you can tee off a signal into a PC and watch it.
 
Top