I2C with BS2
Archiver
Posts: 46,084
Is there an easy way to interface the BS2 (not the BS2P) to an I2C
device such as the MAX127 A/D converter???
thanks
-S.M.
device such as the MAX127 A/D converter???
thanks
-S.M.
Comments
logicfreak8@y... writes:
> Is there an easy way to interface the BS2 (not the BS2P) to an I2C
> device such as the MAX127 A/D converter???
>
There is a way but I would not say that is easy.
Sid
[noparse][[/noparse]Non-text portions of this message have been removed]
-- Jon Williams
-- Applications Engineer, Parallax
-- Dallas Office
Original Message
From: logicfreak8 [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=OkLmbWa49NWZ-dtXjJLqsyZbdzSZAps_XGXrvVbECYC4rEkjpCHvSt8VhPmbn5yHdOlB4Q1oFbr0thTWXf8]logicfreak8@y...[/url
Sent: Wednesday, June 16, 2004 11:59 PM
To: basicstamps@yahoogroups.com
Subject: [noparse][[/noparse]basicstamps] I2C with BS2
Is there an easy way to interface the BS2 (not the BS2P) to an I2C
device such as the MAX127 A/D converter???
thanks
-S.M.
I've done it.
See below. I took StampWorks example and modified it so that I use
as simple, elementary commands as possible, so as to clearly
illustrate what is going on at lowest level, step by step. You might
find it interesting.
'{$STAMP BS2}
'{$PBASIC 2.5}
SDA CON 8
SCL CON 9
SDAin VAR IN8
DevType CON %1010 << 4
DevAddr CON %000 << 1
Wr2432 CON DevType | DevAddr | 0
Rd2432 CON DevType | DevAddr | 1
ACK CON 0
NAK CON 1
closed CON 0
open CON 1
greenled CON 13
redled CON 15
i2cSDA VAR Nib
i2cData VAR Byte
i2cWork VAR Byte
i2cAck VAR Bit
eeAddr VAR Word
counter VAR Nib
outVal VAR Byte
inVal VAR Byte
obyte VAR Byte
ibyte VAR Byte
N VAR Nib
b VAR Bit
SW0 VAR IN0
'CRSRXY CON 2
Initialize:
PAUSE 250
i2cSDA =SDA
Main:
FOR eeAddr = 30 TO 32
DEBUG CRSRXY, 11, 2, DEC eeAddr, " "
FOR counter = 0 TO 3
LOOKUP counter, [noparse][[/noparse]$FF, $AA, $55, $00], outVal
DEBUG CRSRXY, 11, 3, IHEX2 outVal
i2cData = outVal
GOSUB Write_2432
PAUSE 10
GOSUB Read_2432
inVal = i2cData
DEBUG CRSRXY, 11, 4, IHEX2 inVal, " "
IF (inVal <> outVal) THEN Bad_Addr
DEBUG "Pass"
GOTO Next_Addr
Bad_Addr:
DEBUG "Fail"
Next_Addr:
PAUSE 50
NEXT
NEXT
DEBUG CR, CR, "Done!"
DO
IF SW0 = CLOSED THEN
LOW greenled
HIGH redled
ELSE
LOW redled
HIGH greenled
ENDIF
LOOP
END
Write_2432:
GOSUB I2C_Start
i2cWork = Wr2432
GOSUB I2C_TX_Byte
GOSUB Get_Ack
IF (i2cAck = NAK) THEN Write_2432
i2cWork = eeAddr / 256
GOSUB I2C_TX_Byte
GOSUB Get_Ack
i2cWork = eeAddr // 256
GOSUB I2C_TX_Byte
GOSUB Get_Ack
i2cWork = i2cData
GOSUB I2C_TX_Byte
GOSUB Get_Ack
GOSUB I2C_Stop
RETURN
Read_2432:
GOSUB I2C_Start
i2cWork = Wr2432
GOSUB I2C_TX_Byte
GOSUB Get_Ack
IF (i2cAck = NAK) THEN Read_2432
i2cWork = eeAddr / 256
GOSUB I2C_TX_Byte
GOSUB Get_Ack
i2cWork = eeAddr //256
GOSUB I2C_TX_Byte
GOSUB Get_Ack
GOSUB I2C_Start
i2cWork = Rd2432
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
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
--- In basicstamps@yahoogroups.com, "logicfreak8" <logicfreak8@y...>
wrote:
> Is there an easy way to interface the BS2 (not the BS2P) to an I2C
> device such as the MAX127 A/D converter???
>
> thanks
>
> -S.M.