Mcp23008
![smhair.gif](http://forums.parallax.com/images/smilies/smhair.gif)
Hello.
I've used MCP23016 before.· So I thought using the new MCP23008 should be a cinch.
Wrong!!!
Has anyone used this device with Stamp before?
I've used following code and cannot figure out what the problem is.· Hope someone can point the way.
The program just lights 8 LEDs with a given pattern.·
'{$STAMP BS2}
'{$PBASIC 2.0}
SDA CON 2
SCL CON 1
SDAin VAR IN2
MCP23008 CON %0100 << 4
Addr23008_1 CON %000 << 1
Wr_23008_1 CON MCP23008 | Addr23008_1 | 0
Rd_23008_1 CON MCP23008 | Addr23008_1 | 1
ACK CON 0
NAK CON 1
GPIO CON 9
OLAT CON 10
IPOL CON 1
IODIR CON 0
INTCAP CON 8
IOCON CON 5
'i2cSDA VAR Nib
i2cData VAR Byte
i2cWork VAR Byte
i2cAck VAR Bit
Reg VAR Nib
outVal VAR Byte
inVal VAR Byte
obyte VAR Byte
ibyte VAR Byte
N VAR Nib
b VAR Bit
CRSRXY CON 2
Initialize:
PAUSE 250
'i2cSDA =SDA
'Set up GPI0 as output
Reg = IODIR
i2cData = %00000000
GOSUB Write_23008
Reg = IOCON
i2cData = %00111110
GOSUB Write_23008
Main:
· Reg = GPIO
· outVal = %10100101
· i2cData = outVal
· GOSUB Write_23008
· GOTO Main
END
Write_23008:
· GOSUB I2C_Start
· i2cWork = Wr_23008_1
· GOSUB I2C_TX_Byte
· GOSUB Get_Ack
· 'IF (i2cAck = NAK) THEN Write_23008
· i2cWork = Reg
· GOSUB I2C_TX_Byte
· GOSUB Get_Ack
· i2cWork = i2cData
· GOSUB I2C_TX_Byte
· GOSUB Get_Ack
· GOSUB I2C_Stop
· 'DEBUG CR, "Wrote ", IBIN8 i2cData, " to output", CR
· RETURN
Read_23008:
· GOSUB I2C_Start
· i2cWork = Wr_23008_1
· GOSUB I2C_TX_Byte
· GOSUB Get_Ack
· IF (i2cAck = NAK) THEN Read_23008
· i2cWork = Reg
· GOSUB I2C_TX_Byte
· GOSUB Get_Ack
· GOSUB I2C_Start
· i2cWork = Rd_23008_1
· GOSUB I2C_TX_Byte
· GOSUB Get_Ack
· GOSUB I2C_RX_Byte
· GOSUB Send_NAK
· GOSUB I2C_Stop
· i2cData = i2cWork
· RETURN
I2C_Start:
· LOW SCL
· INPUT SDA
· INPUT SCL
· LOW SDA
· Clock_Hold:
· IF (INS.LOWBIT(SCL) = 0 ) THEN Clock_Hold
· LOW SCL
· RETURN
I2C_TX_Byte:
· LOW SCL
· OUTPUT SDA
· obyte = i2cWork
· FOR N=0 TO 7
··· b = (obyte >> 7) & 1
··· IF (b=1) THEN Out_1
··· LOW SDA
··· INPUT SCL
··· LOW SCL
··· GOTO nextb
· Out_1:
··· INPUT SDA
··· INPUT SCL
··· LOW SCL
·Nextb:
··· obyte = obyte << 1
··· NEXT
· RETURN
Get_Ack:
· LOW SCL
· INPUT SDA
· INPUT SCL
· b = SDAin
· 'INPUT SCL···· 'This line could be used in lieu of above line.· Both works.· This is why SHIFTIN works.
· LOW SCL
· i2cAck = b
·RETURN
I2C_RX_Byte:
· LOW SCL
· INPUT SDA
· ibyte = 0
· FOR N = 0 TO 7
··· INPUT SCL
··· ibyte = (ibyte <<1) | SDAin
··· LOW SCL
· NEXT
· i2cWork = ibyte
· RETURN
Send_NAK:
· LOW SCL
· INPUT SDA
· INPUT SCL
· LOW SCL
· RETURN
Send_ACK:
· LOW SCL
· OUTPUT SDA
· LOW SDA
· INPUT SCL
· LOW SCL
· RETURN
I2C_Stop:
· LOW SCL
· LOW SDA
· INPUT SCL
· INPUT SDA
· LOW SCL
· RETURN
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"If you want more fiber, eat the package.· Not enough?· Eat the manual."········
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen