Shop OBEX P1 Docs P2 Docs Learn Events
Pcf8574 + bs sx — Parallax Forums

Pcf8574 + bs sx

o_wano_wan Posts: 15
edited 2005-07-27 14:16 in BASIC Stamp
hi,

I followed the tutorial here, http://www.phanderson.com/stamp/i2c/8574.html, flash a LED using below circuit and code. It didn't work and I'm not sure what happen.
Does anyone get a working SX example with 8574? Could you please share?

code.:

device var byte ' device 0-7

o_byte var byte ' byte to send to device
i_byte var byte ' byte from device

o_patt var byte ' byte to appear on output of 8574
i_patt var byte ' byte read from input of 8574

directions var byte ' defines which bits on 8574 are outputs

n var byte ' index
b var bit ' bit


SDA_PIN con 4
SCL_PIN con 5
SDA_OUT var out4
SCL_OUT var out5
SDA_IN var in4
SDA_DIR var dir4

OUT con 1
IN con 0

dirs=$f0ff

directions=$80 ' p7 of 8574 defined to be an input
device = 0 ' setting of A2 A1 A0

main
o_patt = $ff ' initialize all outputs to logic one
gosub out_patt

top
low SDA_PIN
gosub in_patt
debug hex i_patt
if ((i_patt & $80) >> 7 ) = 0 THEN flash
' if most sign bit is a zero then flash

o_patt = $01 ' otherwise, turn the LED off
gosub out_patt
goto top

flash ' wink the LED on and off one time
o_patt = $00
gosub out_patt
pause 300
o_patt = $01
gosub out_patt
pause 300
goto top

'*****
in_patt ' fetch input on addressed device
gosub start
o_byte = $40 | (device <&lt1) | $1
gosub out_byte
gosub nack
gosub in_byte
gosub nack
gosub sstop
i_patt = i_byte
return

out_patt ' output specified patt to addresses device
gosub start
o_byte = $40 | (device <&lt1)
gosub out_byte
gosub nack
o_byte = o_patt | directions ' bits defined as inputs set
' to logic one
gosub out_byte
gosub nack
gosub sstop
return

in_byte ' fetches 8 bits, most sign bit first
SDA_DIR=IN 'input

i_byte=0
for n=0 to 7
pause 200
high SCL_PIN ' clock high
pause 200
i_byte=(i_byte << 1) | SDA_IN 'read bit and or with prev
debug dec SDA_IN
low SCL_PIN
next

SDA_DIR=OUT 'output
return

out_byte ' output o_byte 'beginning with most sig bit

low SDA_PIN

for n=0 to 7
b= (o_byte >> 7) & 1
if (b=1) then out_one
SDA_DIR=OUT
debug "0"
_clk
high SCL_PIN
pause 100
low SCL_PIN
pause 100
o_byte=o_byte << 1
next
return

out_one
SDA_DIR=IN
debug "1"
goto _clk

nack
SDA_DIR=OUT ' bring SDA high and clock
high SCL_PIN
low SCL_PIN
return

start
low SCL_PIN
SDA_DIR=IN ' SDA at logic one
high SCL_PIN
low SDA_PIN
SDA_DIR =OUT 'bring SDA low while clock is high
low SCL_PIN
debug "START"
debug $0d
return

sstop
low SCL_PIN
SDA_DIR=OUT
high SCL_PIN
SDA_DIR=IN 'bring SDA high while clock is high
debug "STOP"
debug $0d
return

Comments

Sign In or Register to comment.