20X2 i2cslave and serial issue

dworboys

New Member
I'm trying to use a 20X2 as a i2c slave. I also would like the 20X2 to execute serout and serin commands. The i2c master is an arduino uno. I can get the arduino to read in 2 bytes from the scratchpad ok when the 20X2 is not trying to read the serial port but as soon as the serial port is active I cant read the i2c data. The manual indicates a potential internal interrupts conflict. So to try and get around the issue I get the arduino to set an enable input on the 20X2. Whenever the enable is high the serin and serout commands are never called. No success. I was wondering if anyone has struck this issue before and have been able to successfully run a 20X2 as an i2c slave as well as have it execute serout and serin commands. My 20X2 slave code is attached. David

'20X2 Slave to Arduino interface
'xbee rx feeds signal to 20X2 pinc.7 then arduino reads 4 bytes of tempersture data from scratchpad

#picaxe 20x2
#no_data


dirsC=%10001111 'c.7 output to xbee, c.6=input from xbee rx, c.4 input from arduino/mcp23017
dirsB=%11111111
pullup ON

setfreq m16 'need m16 to make i2c slave work at i2c 400khz


hi2csetup i2cslave, 0xA0 'initialise 20x2 i2c slave operation

pinc.7 = 1 'see picaxe serout T2400 recommendation
pause 20

w8 = 0 'program counter

for b23 = 0 to 15 'initialise xbee link, send a few serout's, populate w2,w1 temperature registers
gosub poll_sensors
pause 2000
next b23

'----------------------------------------------
main:

if pinc.4 = 1 then :goto main :endif 'dont poll sensors if pinc.4 high

if w8 = 9200 then :gosub poll_sensors :w8 = 0 :endif 'poll ~ every 30 secs

w8 = w8+1 'inc program counter

goto main

'----------------------------------------------
poll_sensors:

serout c.7, T2400_16,("RP") 'poll remote temperature sensor
serin [200,jump], c.6,T2400_16,("RQ"),b5,b4,b3,b2 'look for 4 return bytes from remote

put 1,b5 'store in 20X2 scratchpad ready for arduino i2c master read
put 2,b4
put 3,b3
put 4,b2

jump:

return
 

Attachments

hippy

Technical Support
Staff member
While the PICAXE is waiting for SERIN, executing SEROUT, and similar commands which it needs to dedicate its entire time to, it can't be doing anything else, won't respond to requests as an I2C Slave or interrupts.

The best way to handle both on an X2 when it's acting as a bridge as you are doing here may be to use HSERIAL background receive and HSEROUT output, perhaps a byte at a time.
 

dworboys

New Member
While the PICAXE is waiting for SERIN, executing SEROUT, and similar commands which it needs to dedicate its entire time to, it can't be doing anything else, won't respond to requests as an I2C Slave or interrupts.

The best way to handle both on an X2 when it's acting as a bridge as you are doing here may be to use HSERIAL background receive and HSEROUT output, perhaps a byte at a time.
Thanks hippy. your suggestion is working fine. Here is my code. Much simpler than I had guessed.

David

'20X2 i2c slave to Arduino i2c master interface
'for reading 2x two byte temperature values from remote picaxe 14M2 and 2x DS18B20s via Xbee link

#picaxe 20x2
#no_data


dirsC=%11111111 'C.0 hserout '
dirsB=%10111111 'B.6 hserin
pullup ON

setfreq m16 'need m16 to make i2c slave work at i2c 400khz

hsersetup B2400_16, %0001 'T2400, background mode

hi2csetup i2cslave, 0xA8 'initialise 20x2 i2c slave operation

w8 = 0
w9 = 0 'program counters

for b23 = 0 to 15 'initialise xbee link, send a few hserout's, populate scratchpad
gosub poll_sensors
pause 2000
next b23

'----------------------------------------------
main:

if w8 = 17350 then :w9=w9+1 :w8=0 :endif 'select on test to give 15 secs
if w9 = 2 then :w9 = 0 :gosub poll_sensors :w9 = 0 :endif '2 = 2*15 secs = 30 secs >> poll

w8 = w8+1 'inc program counter

goto main

'----------------------------------------------
poll_sensors:

hserptr=0 'reset the scratchpad pointer
hserinflag=0 'reset the hserinflag

hserout 0,("RP") 'poll remote temperature sensor
'the response is deposited directly to scratchpad, no need for hserin
'6 byte response (R,Q,tempA hi byte, tempA lo byte, tempB hi byte, tempB lo byte)
return
 

Attachments

Top