Picaxe To Picaxe I2c

zoid1

New Member
Hello everyone,

I want to establish a network of PICAXE 28X1 or 40X1 controllers (with one master and multiple slaves) using I2C.

Does anybody have any examples and/or example code of how to do that?

Regards
Akis
 

leftyretro

New Member
Hello everyone,

I want to establish a network of PICAXE 28X1 or 40X1 controllers (with one master and multiple slaves) using I2C.

Does anybody have any examples and/or example code of how to do that?

Regards
Akis
Well I have no code to share, but when I did aquire a second 28X1 I did wire them I2C to I2c to see if I could make them talk with just a simple test programs, one for master and one for slave.

It worked fine and I see this as a very viable option over the seriel to seriel link many use.

Lefty
 

BCJKiwi

Senior Member
Just a word of caution of where this can be used (before we all get too carried away!)

i2c starts with the 18X and is not available on smaller chips.
Hi2c starts on the X1 and X2 parts.
On 18X and 28X i2cslave relates only to the external part, not to the PICAXE itself.

On the X1 and X2 parts hi2csetup is able to be used to place a PICAXE chip into slave mode.

So this technique is limited to X1 and X2 parts only.
 

BeanieBots

Moderator
Well, not really BCJKiwi.
I played with PICAXE I2C 'networks' well before the 28X1 was on the scene.
With a little handshaking, it is possible to have many 18X's share a common memory device. From there it is not too difficult to create a command set and share data. Never took it very far but proved it can be done.
In fact PICAXE.net uses that very technique.

P.S.
I've moved this discussion thread from the finished projects section to the active forum.
 
Last edited:

zoid1

New Member
PICAXE to PICAXE I2C

In any case, what I have in mind is to use the 28X1 series, but it is good to know that it can be done with previous versions. Thx BeanieBots ;)

If anybody has any examples that I could use, I would be greatly appriciate it:)

Akis
 

BCJKiwi

Senior Member
BB,
My comment still stands I believe.
I guess it depends on what is meant by;
"I want to establish a network of PICAXE 28X1 or 40X1 controllers (with one master and multiple slaves) using I2C"
I took it to mean without the need for other parts.

Your example appears to relate to the X models using a common external memory device. This is not what I understood to be the idea - to have PICAXE (i2c Master) to PICAXEs (i2c Slaves) on the same bus - no external memory involved!

However it appears my cautionary note has served it's purpose - to make users aware of the issues.
 
Last edited:
well my solution isn't the most advanced or earth shattering one but i am using pulsout and pulsin on some 08Ms and i just connected an input/output pin on each together and ignored the clockline completely. there are somethings that i still need to fix but i'm it works quite well for short pulses.
 

Technical

Technical Support
Staff member
Master
Code:
#PICAXE 28X1
#terminal off
 
; This program can be used on a 28X or 28X1
; It setups the i2c master to output an ascii character every second
 
hi2csetup i2cmaster,%10100000, i2cslow, i2cbyte
 
main:
 
 inc b1
 if b1 < "a" then 
  b1 = "a"
 end if
 
 if b1 > "z" then 
  b1 = "a"
 end if
 
 hi2cout 5,(b1) 'write the character
 pause 1000
 hi2cin 5,(b2)  'read back as a test!
 debug
 goto main
slave
Code:
#PICAXE 28X1
#terminal 4800
 
; This program is the slave, for 28X1 only
; Waits for a background i2c receive and then echos to Terminal
sertxd ("RESET",cr,lf) ' transmit it
hi2csetup i2cslave, %10100000
 
main:
 if hi2cflag = 1 then got_it
 goto main
 
got_it:
 hi2cflag = 0  ' reset flag
 ptr = hi2clast  ' set scratchpad pointer to last byte received
 sertxd ("GOT",@ptr,CR,LF)
 goto main
To use more than one slave simply change the %10100000 to a different address for each chip.
 
Last edited:

zoid1

New Member
Technical thank you very much for your help...

I think that will do for starters :)

Cheers again,
Akis
 

hippy

Ex-Staff (retired)
Just to clarify; I2C slave devices can only be created using X1 ( and forthcoming X2 ) PICAXE's, but any PICAXE can communicate with an X1/X2 slave using I2C. The 18X has I2C commands built in and the 08, 08M and 14M can communicate by bit-banging but that does eat up a lot of code space.

Multiple PICAXE I2C masters can be used with X1/X2 slaves or any other I2C device providing the electrical interfacing is done correctly and there's a means of arbitration so only one master communicates at a time.

A single PICAXE master and a network of X1/X2 slaves should be a straight forward proposition.
 

zoid1

New Member
Hi again everybody,

Thx for the help so far... I have some questions that I would appriciate if you could help me with:

Q1. Let's say that my Master PICAXE (28X1) sends data to Slave device (another 28X1) in faster rate than the slave can read (e.g. slave has to execute a larger program than the master.) What happens? Can the slave device read all the data sequentially or only the last byte received?

Q2. Master sends hi2cout 5,(b1,b2,b3,b4). How can the slave device access all these data and use them? The ptr = hi2clast sets the pointer to the last byte received...Then how can I store the 4 bytes using @ptr? Can I use:
get hi2clast,b1,b2,b3,b4 ?

Thank you
Akis
 

Technical

Technical Support
Staff member
1) i2c writes to the slave happen on an internal interrupt in the slave ie they are more important than the slaves PICAXE program and happen automatically. Writing large amounts of data to the slave should all arrive, but could mean that the PICAXE program in the slave runs a bit slower than normal (e.g. pause timings could stretch a bit).

2) The data is saved in the slaves scratchpad automatically. You don't need to store it using @ptr, it's stored automatically in the background. Naturally you will probably want to read the scratchpad data at some point in the slave sprogram, but most systems would allocate a purpose to each byte in memory ie byte 5 does this, byte 6 does that etc.

In a simple system the slave can then just cycle through the bytes of interest, seeing if anything has changed recently (use get command).

As a simple output extender, the master writes a byte to address 1. This byte value is passed to the 8 output pins of the slave.

master:
Code:
main:
hi2cout 1,(255) 'all on
pause 1000
hi2cout 1,(0) 'all off
pause 1000
goto main
slave:
Code:
main:
get 1,b1
let outpins = b1 
goto main
 
Last edited:

hippy

Ex-Staff (retired)
My understanding is that the memory in the I2C Slave is simply overwritten by the latest data sent by the I2C Master so if you have a slow running slave program you will only be able to read the latest data received, the rest is lost and gone forever.

There's a potentially nasty issue here known as 'aliasing' where the master may be writing at the same time as you are reading. If the master sent four bytes "ABCD" then overwrote those with "WXYZ" you could read "ABYZ". Often that won't matter, but if "BC" or "XY" were important as a two byte entity getting "BY" is an incorrect value.

To get round aliasing you need to have a higher level protocol, an allocated byte in the I2C Slave memory which is set when the Master writes, for example sending "#ABCD" and the I2C Master always reads the first byte before sending to see if it's still "#", if it is the slave hasn't read the data sent and the master must wait, otherwise it sends new data, eg, "#WXYZ". The slave will read the data sent then clear the memory location containing "#" which will let the master send the next set of data.

Aliasing isn't always problem but one to be aware of.
 

zoid1

New Member
Hello,

Thx hippy and technical! That clarifications help me a lot :). Now that I am starting to understand a few thinks about the I2C protocol, what I need to do is to make the theory into practice...

One last question, if the master does not know the exact address of the slave( e.g. we are plugging in a variety of different sensors as slaves to master PICAXE without knowing which is which), can I use the general broadcast address, which is 0 if I am correct?

Thx again
Akis
 
Top