Communication Between 2 Picaxe 20X2's Using I2C

Goeytex

Senior Member
#1
Using I2C to communicate between 2 20x2 Picaxe's

The example in Manual 2 still uses the Picaxe 28x1 which will soon be
become obsolete. The use of "outpins" in the master slave example is not
explained and does not work with X2 parts anyway, so I decided to
write test code to test I2C communication between 2 Picaxe 20X2's.

It works rather nicely and is a fast alternative to using software serial
communication via serout & serin and can have an advantage over hardware
serial in certain applications.

What This Code Does:

The Master sends 4 pseudo random bytes to the slave every 500ms.

1. When the slave receives the bytes, an interrupt is generated.
2. The slave program control goes to its Interrupt Subroutine.
3. The bytes are received into Scratchpad Ram starting at location 0.
4. The bytes are then read from Scratchpad memory and put into
variables B0 - b3 using @bptr.

5. Debug is called to verify data reception.
6. The bptr pointer is reset to zero
7. The Hi2cflag is reset
8. The interrupt is reset.
9. Program control returns to MAIN to wait for the next I2C data packet.

10. The Master Picaxe then reads the data from the slave using HI2CIN
and displays it using sertxd.


The circuit / connections are straight-forward.

1. Put 4.7K pullups on legs 11 & 13 on one of the 20x2's
2. Connect Leg 11 on Master to Leg 11 on Slave
3. Connect Leg 13 on Master to Leg 13 on Slave
4. Observe minimum circuit requirements for each Picaxe
(See Picaxe Manuals for minimum requirements)

Master Code
Code:
#picaxe 20x2
#no_data
#no_table

init: 
setfreq m16
hi2csetup i2cmaster, %10100000, i2cfast_16, i2cbyte

b1 = 6
b2 = 19
b3 = 107
b4 = 235

main:
pause 2000
inc b1[COLOR="#00FF00"] 'increment variables [/COLOR]
inc b2
inc b3
inc b4

hi2cout 0,(b1,b2,b3,b4) [COLOR="#00FF00"]' send 4 bytes beginning at
                        ' scratchpad  SP Location 0
                        ' on slave Picaxe[/COLOR]

sertxd ("Written to Slave",cr,lf)
sertxd (#b1," ", #b2," ",#b3," ",#b4," ",cr,lf,cr,lf)
pause 1000

Read_Data: 
hi2cin 0,(b5,b6,b7,b8)  [COLOR="#00FF00"]'read 4 bytes from slave starting[/COLOR]
                       [COLOR="#00FF00"] 'at location 0.  These should match[/COLOR]
                       [COLOR="#00FF00"] 'the sent bytes[/COLOR]
pause 10

Sertxd ("Read From Slave",cr,lf)
sertxd (#b5," ",#b6," ",#b7," ",#b8," ",cr,lf,cr,lf)     

goto main
Slave Code
Code:
#picaxe 20x2
#no_data
#no_table

init: 
setfreq m16
hi2csetup i2cslave, %10100000
setintflags %01000000,%01000000

main:
pause 1000
   [COLOR="#00FF00"] 'waiting for interrupt
    'do some other stuff here[/COLOR]
goto main

interrupt:
  
  bptr = 0
  pause 10
  for ptr = 0 to hi2clast
    get ptr,@bptrinc        [COLOR="#00FF00"]' get all received bytes starting at location 0[/COLOR]
  next                      [COLOR="#00FF00"]' and put them into variables B0 to Bx[/COLOR]

DEBUG   [COLOR="#00FF00"]'look at variables to confirm operation[/COLOR]

pause 500
let hi2cflag = 0      [COLOR="#00FF00"] 'clear interrupt & reset[/COLOR]
setintflags %01000000,%01000000
return   [COLOR="#00FF00"]'to main[/COLOR]
 
Last edited:
#2
Hi Goeytex,

Well done on taking the time to "publish" this information.

While folks experienced in i2c comms can work this out, for those new to the use of i2c comms and in particular i2c comms between 2 PICAXE chips, such information can be very helpful.
Let us hope that some actually do a forum search and come across your work. Otherwise we can at least point them to your work where appropriate.
 

Bill.b

Senior Member
#3
Hi Goeytex

all examples of i2c comms i have observes write data to a slave, seldom do they show hoe to retreive data from a slave picaxe.

alse what is the difference beteen hi2cout 0,(b1,b2,b3,b4) and writei2c 0,(b0,b1,b2,b3,b4)
 
#4
what is the difference beteen hi2cout 0,(b1,b2,b3,b4) and writei2c 0,(b0,b1,b2,b3,b4)
For the user there is no difference.

Earlier PICAXE chips being the "X" has a Synchronous Serial Port (SSP) for i2c and SPI comms and i2c was only available in slave mode hence the setup command was i2cSLAVE.

Later chips starting with the X1 series have a Master Synchronous Serial Port (MSSP) and for i2c provide both Master and Slave modes.
For the later chips an enhanced command was required to handle both Master and Slave modes and Rev Ed created the hi2c.... series of commands.

Now the older i2cSLAVE, i2cwrite and i2cread (also writei2c and readi2c) are deprecated and although still acceptred by the PE, the PE translates these older commands across to the newer commands.

For the older 18X which is still available, the hi2c.... series of commands are in effect are aliased back to the now deprecated commands with some compiler checking to ensure what's asked for is supported
 
Last edited:

Goeytex

Senior Member
#5
I can certainly add retrieving data from the slave.


NOTE:

Added HI2CIN routine to master code to read data from Slave
Added sertxd to master code for debugging
 
Last edited:
#6
Hi Goeytex, Can I connect more than 2 18m2 picaxes together using i2c protocole, for example some transmitters and one receiver. and what is the maximum number of them.

One more question, regarding the receiver address is it arbitrary or not.
Thanks.
 
#7
Can I connect more than 2 18m2 picaxes together using i2c protocole, for example some transmitters and one receiver. and what is the maximum number of them.
For the master devices (any X/X1/X2 or M2 PICAXE part), just change the i2c address to something different in the program line:
hi2csetup i2cmaster, %10100000, i2cfast_16, i2cbyte


Only X1 and X2 PICAXE parts can be slave devices. Again just change the program line
hi2csetup i2cslave, %10100000

Make sure that it will not conflict with any other i2c devices you may use now and possibly also in the future.
 
#8
For the user there is no difference.

Earlier PICAXE chips being the "X" has a Synchronous Serial Port (SSP) for i2c and SPI comms and i2c was only available in slave mode hence the setup command was i2cSLAVE.

Later chips starting with the X1 series have a Master Synchronous Serial Port (MSSP) and for i2c provide both Master and Slave modes.
For the later chips an enhanced command was required to handle both Master and Slave modes and Rev Ed created the hi2c.... series of commands.

Now the older i2cSLAVE, i2cwrite and i2cread (also writei2c and readi2c) are deprecated and although still acceptred by the PE, the PE translates these older commands across to the newer commands.

For the older 18X which is still available, the hi2c.... series of commands are in effect are aliased back to the now deprecated commands with some compiler checking to ensure what's asked for is supported
So is there no speed penalty during operation - just when compiling?
Al
 
#9
The short answer: No.


There would be no speed penalty for any PICAXE chip relative to that chips capabilities irrespective of which commands (older or newer) were used in the PE.
I doubt that folks would see any time differnce in the tokenising of the users BASIC program and/or downloading to the required PICAXE chip.
 
#13
Hi all,

I've been having a read and a play as you do and got a question. On copying the Master Code into PE6 and running the Check I get a Syntax error for COLOR, am I missing something?
 

lbenson

Senior Member
#14
Remove the [COLOR and [/COLOR tags (ending in "]"). They are not part of the actual code--I believe they were intended to add color coding to the comments when displayed in the old forum.
 

PieM

Senior Member
#15
Goeytex code with colors....


Master Code
Rich (BB code):
#picaxe 20x2
#no_data
#no_table

init:
setfreq m16
hi2csetup i2cmaster, %10100000, i2cfast_16, i2cbyte

b1 = 6
b2 = 19
b3 = 107
b4 = 235

main:
pause 2000
inc b1 'increment variables 
inc b2
inc b3
inc b4

hi2cout 0,(b1,b2,b3,b4) ' send 4 bytes beginning at
                        ' scratchpad  SP Location 0
                        ' on slave Picaxe

sertxd ("Written to Slave",cr,lf)
sertxd (#b1," ", #b2," ",#b3," ",#b4," ",cr,lf,cr,lf)
pause 1000

Read_Data:
hi2cin 0,(b5,b6,b7,b8)  'read 4 bytes from slave starting
                        'at location 0.  These should match
                        'the sent bytes
pause 10

Sertxd ("Read From Slave",cr,lf)
sertxd (#b5," ",#b6," ",#b7," ",#b8," ",cr,lf,cr,lf)  

goto main
Slave Code
Rich (BB code):
#picaxe 20x2
#no_data
#no_table

init:
setfreq m16
hi2csetup i2cslave, %10100000
setintflags %01000000,%01000000

main:
pause 1000
    'waiting for interrupt
    'do some other stuff here
goto main

interrupt:

  bptr = 0
  pause 10
  for ptr = 0 to hi2clast
    get ptr,@bptrinc        ' get all received bytes starting at location 0
  next                      ' and put them into variables B0 to Bx

DEBUG   'look at variables to confirm operation

pause 500
let hi2cflag = 0       'clear interrupt & reset
setintflags %01000000,%01000000
return   'to main
 
Last edited:
#16
Well I'll be some fine folk have posted recently here and I have been working on this i2c method with my two 2 picaxe 28x2s
How refreshing.

Thanks for the code peoples it's really helpful.
 
Top