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
Slave Code
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
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: