i2c slave / scratchpad problems!

Brietech

Senior Member
So I am attempting to build a keyboard FIFO buffer using a 28X1 as an i2c slave to another 28X1. I developed a sort of handshaking protocol to let them exchange data: In the scratchpad ram, 0x00 = keyboard status byte (Empty=0, ready=1), and 0x01 = current character. Here is the code:
Code:
'0x0 = status, 1=ready, 0=empty
'0x1 = latest key value
startup:
put 0,0
hi2csetup i2cslave,%11100000

main:
	kbin [400,kbtimeout],b0	'grab one character
	get 0,b1
	if b1=1 then main	'if status=ready, discard character
	put 1,b0		'if status <> ready, store character
	put 0,1		'update status=ready
	pause 400

kbtimeout:
	goto main

For some reason, however, whenever I try to read the first two bytes repeatedly from another chip via i2c, location 0x0 of the slave = 128 initially, and whenever I hit a key the value gets placed into location 0x0 instead of 0x1 like it is supposed to, with none of the status updating being processed at all!

I'm just reading it with the following code:

kbtest:
hi2cin 0,(b0,b1)
sertxd(#b0,",",#b1,cr,lf)
pause 10000
goto kbtest


What is going on with this code?!?!
 

hippy

Technical Support
Staff member
You haven't shown the HI2CSETUP command in the PICAXE doing the pulling. As you're currently using HI2CIN / HI2CSETUP you could try using READI2C / I2CSLAVE to see if that changes things.

Maybe also have the keyboard buffer replaced by a simple loop which may give some better indication of what's going on ...

Code:
Do
  b0 = b0 * 2
  If b0 = 0 Then : b0 = 1 : End If
  b1 = b0 ^ $FF
  Put 0,b0
  Put 1,b1
  Pause 1000
Loop
Slave address $E0 should be okay, but you could try something else; $A0 etc.

I cannot see anything obviously wrong in what you're trying to do, nor explain the values you're getting back.
 

Technical

Technical Support
Staff member
Once you have 'put 0,1' once, you never clear the flag, so no other key hits should register. Or does the other chip clear the byte via a write - your program is incomplete so please post the whole program you are using.
 

Brietech

Senior Member
Sorry - That's exactly what it does. Chip A (the I/O controller with code posted above) grabs a byte, places it into location 0x1, and then writes a "1" to 0x0. If 0x0=1, it ignores all new characters. Chip B polls chip A whenever it is ready for a new key, and if 0x0=1, it grabs byte 0x1 and then writes "0" to 0x0.

The only other setup code I have for Chip B is:
Code:
symbol SLAVE = %11100000
hi2csetup i2cmaster,SLAVE,i2cslow_16,i2cword
setfreq em16
I wasn't attempting to implement the full handshaking, I was just trying to test to see if 1) the slave booted properly, and 2) I could see the change when I hit the first key. It's like the "get" and "put" commands don't function properly or something!


By the way, what speed does the i2c run at when a 28x1 is in "slave" mode, and how does clock rate affect that?
 

Technical

Technical Support
Staff member
Your problem is 'i2cword' in the missing bit of code just posted.
It should be i2cbyte.

At present your master is incorrectly outputting an extra second address byte every time - hence the unexpected results.

The i2c clock speed is set by the master (the speed it outputs clock pulses), hence the slave just tries to respond at the speed set by the master. Eventually it could get so fast that the slave can't keep up, but you should be ok as shown.
 
Top