I2C 20m2 master 20x2 slave chip talk

newplumber

Senior Member
Hi to understandable people

I wanted to read my slave 20x2 C.x (C.0-C.7) pins as inputs to be read on the (master 20m2) and
after studing goeytex's sweet post "Communication Between 2 Picaxe 20X2's Using I2C " and other forums
I finally came out of the fog which is one great step for the plumber :) (I'm DONE searching/reading/googling meanings for this)
So I figured since I have it working with out fire alarms going off I figured I would post my unprofessional code
And I can not believe it is so easy, I was thinking to deep like "send out bytes from slave when master starts to read etc..way off!
But here is my code that i have working
SLAVE CODE picaxe 20X2
Code:
#picaxe 20x2
#no_data
#no_table

init: 
setfreq m16
hi2csetup i2cslave, %10100000
dirsc = %00000000


main:
let b0 = pinsc
pause 100
put 0,b0
goto main
MASTER picaxe 20m2
Code:
'picaxe 20M2 master
'#no_data
'#no_table

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

main:
gosub read_data
wait 2
goto main

Read_Data: 
hi2cin 0,(b0)  'read 1 byte from slave 
                                                
pause 10

Sertxd ("Read From Slave",cr,lf) 'baud 19200
sertxd (#b0,cr,lf,cr,lf)     

return
If it needs more work I am sure to correct it but my first IC2 program from chip to chip and thanks
to all picaxe forums :)
your happy friend
 

inglewoodpete

Senior Member
Well done! Yes, it is surprisingly easy once you take the plunge.

My only suggestion is that you use i2cSlow_16 for the master's setup command. As a slave, PICAXEs can't always keep up with the 400kbits/s 'fast' rate.
 

newplumber

Senior Member
Thanks inglewoodpete I will use that suggestion ... now just to make it a grand celebration I am going to try adding 8 more 20x2 slaves just because I can lol or not but its fun learning
 

westaust55

Moderator
I have several boards with a 20X2 running at 64 MHz as the slave device and a 28X2 master overclocked to 80 MHz (with 20 MHz resonator) then the i2c set up for around 1,000 kHz clock speed (PIC datasheet for 28X2 indicates this speed is out of spec) but is all working well on 4 separate projects/boards.
 

hippy

Technical Support
Staff member
your happy friend
Glad to hear everything is working and it was as easy to do as we had hoped it would be.

Now you have it working with an I2C Slave Address of %10100000 ($A0) it might be worth changing that to something else, such as %10110000 ($B0). It is not essential to do that, but it can help make it clearer to any readers of your code that you are accessing something other than EEPROM which usually uses $A0 addressing.

It's also fun to experiment! Any even numbered address between $10 and $EE should be usable for I2C slave access. We recommend avoiding addresses outside that range.
 

newplumber

Senior Member
@ westaust but 1,000 khz? that is cool, so your wires are the same pin to pin with same pull up 4k7 resistors?
@ hippy yes but what made me confused is I couldn't find clearer information what/how the slave picaxe puts out to the master when the master reads data
So I understand it as "put 0,b0" the slave always sends bit 0 first then bit 1,bit 2 etc till no more bits to send? so in my code it sends b0 = 8 bits then magically (for me)
behind the scenes tells the master "thats all folks" or am I off the tracks again lol
"Any even numbered address between $10 and $EE should be usable for I2C slave access"
That is good to know thanks
 

hippy

Technical Support
Staff member
@ hippy yes but what made me confused is I couldn't find clearer information what/how the slave picaxe puts out to the master when the master reads data
So I understand it as "put 0,b0" the slave always sends bit 0 first then bit 1,bit 2 etc till no more bits to send? so in my code it sends b0 = 8 bits then magically (for me)
behind the scenes tells the master "thats all folks" or am I off the tracks again lol
We will take a look at the documentation to see if things can be made clearer. Basically ...

Up to 256 bytes of scratchpad memory in an I2C slave can be read by an I2C master. Using PUT ( or @ptr= etc ) places a byte value in scratchpad at the specified location in the slave, then the master can read that byte by specifying the same location -

b0 = 123
PUT 15, b0

will put the value 123 in scratchpad location 15 on the slave side. Then, on the master side -

HI2CIN 15, (b9)

Will read the slave's scratchpad location 15, put the value read into 'b9', so in this example 'b9' ends up with the value 123.

There is no real need to worry about how I2C works behind the scene; whole byte values magically get transported between slave scrachpad and master with HI2CIN or the other way with HI2COUT.

Data transfer only occurs when the master executes a HI2CIN or HI2COUT command. So if you had in your slave ...

Do
Put 0, 99
Put 0, 123
Pause 1000
Loop

A "HI2CIN 0,(b0)" would be far more likely to read 123 than 99. That 99 will only be available for reading for a very short period of time.
 
Last edited:

newplumber

Senior Member
hippy Thanks for the explanation now that all makes sense to me , btw you don't need to change documentation its only me
that was delusional but in the examples at bottom page it would be nice to have example 4 picaxe chip to picaxe chip
 

lbenson

Senior Member
Just to clarify or restate what I think you understand now, the slave does not send anything to the master under PICAXE BASIC control. The slave puts bytes of data into a portion of its memory which in PICAXE lingo is called the scratchpad (accessible on the slave with PUT, GET, and @ptr). Because of special hardware in the PIC chip, a master is able to read (and write to) this memory as if it were a 128-byte I2C eeprom, without any slave PICAXE code being involved (after setup and ongoing updating of the scratchpad memory as necessary).

Hippy's point about the pauses to prevent or reduce clashing of slave updating of scratchpad with master I2C retrieval is well taken. Realistically, how often will you need to sample your inputs (for instance, every quarter second--PAUSE 250)?
 

newplumber

Senior Member
Okay thanks its getting clearer and clearer and that is why a 20m2 wont work for a slave because it has no scratchpad memory
another good piece of info.
"how often will you need to sample your inputs"
yes about that 250 because once I have them then tell the led strip what to do
when they arrive I will have more info what problems I will have etc
 

westaust55

Moderator
@ westaust but 1,000 khz? that is cool, so your wires are the same pin to pin with same pull up 4k7 resistors?
Checked my schematic and yes standard 4k7 resistors.
The Two PICAXE chips are in effect back to back so the track/wire length for the two wires is as short as possible - around 20 mm.

For such speeds you have to calculate your own Hi2cSETUP speed parameter value.
In my case, the values based on 28X2 clock speed and desired i2c clock are:
Code:
;  SYMBOL i2cFAST_72 = 17   ; 22   ; 17 equates to ~1 MHz i2c clock speed / 22 for ~800 kHz
   SYMBOL i2cFAST_80 = 19   ; 24   ; 19 equates to ~1 MHz i2c clock speed / 24 for ~800 kHz

Hi2cSETUP i2CMASTER, %10100000, i2cFAST_80, i2cBYTE   ; Set up i2c comms for reading data from slave PICAXE #1 (20X2 at 64 MHz)
 
Last edited:

hippy

Technical Support
Staff member
Okay thanks its getting clearer and clearer and that is why a 20m2 wont work for a slave because it has no scratchpad memory
That's it. Not entirely; one could have the variable space exposed as if scratchpad but that is not so easy to use, could cause all sorts of problems for the user, and there are some other technical issues because the chips have different silicon. As it stands; if there is no scratchpad memory there will be no I2C slave support.
 

newplumber

Senior Member
That is good to know westaust thanks and someday I will try to test my 20x2's but its always fun to see you stretch the limits on everything lol
Someday I hope China is going to call you and see if you can work your magic in their shipping department (at least to usa) :)

thanks hippy... yes and for me to make it work would take years to understand which would never do because
for me the 20x2 is the best chip on the market for the space/price/speed.
 
Top