Getting Started with 2-wire Serial communication
GrendelT
Posts: 23
Where is a good starting point for serial 2-wire communications?
I have ordered several different ICs from various manufacturers that I would like to play with, but I'm having a hard time getting my brain wrapped around I2C.
Any links you can provide would be greatly appreciated. (Chiefly, I'm trying to get one of the Dallas Semiconductor 1621S temp senstors working with the BS2.)
I have ordered several different ICs from various manufacturers that I would like to play with, but I'm having a hard time getting my brain wrapped around I2C.
Any links you can provide would be greatly appreciated. (Chiefly, I'm trying to get one of the Dallas Semiconductor 1621S temp senstors working with the BS2.)
Comments
I've no idea what other IC's you've received, but the Dallas 1621S is NOT an I2C device, even as much a it may look like one. The reason it may look that way is because it's SM-Bus compatible, and contains device addressing within the command structure. In any case, you access it via the SHIFTIN/SHIFTOUT commands using the MSB first method. The only tricky part is the 9-bit data being presented by the device.
I have a sample program for the DS-1620 which is a similar device, but it uses a 3-wire, rather than a 2-wire connection scheme. That is more in the lines of SPI/Microwire compatible, and also uses SHIFTIN/SHIFTOUT for access. Let me know if you'd like that sample program.
Regards,
Bruce Bates
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
<!--StartFragment -->
I just looked up the datasheet and it seems the DS1621 is indeed an I2C device.· You have to read two bytes to get the temperature, and then shift that 16-bit value right by seven bits to correct the alignment.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Post Edited (Jon Williams (Parallax)) : 2/8/2006 4:33:19 PM GMT
' {$STAMP BS2}
' {$PBASIC 2.5}
' 24C01/24C01A eeprom programing
' make use of page write and sequential read
'#DEFINE _24C01A
eeprom_size CON 128· ' 128 x 8· = 1k Bits
size CON 8
' *** eeprom data is here ***
'DATA $FF (128)
' or
'DATA $0 (128)
' or
DATA $FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF
DATA $FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF
DATA $FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF
DATA $FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF
DATA $FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF
DATA $FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF
DATA $FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF
DATA $FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF
' *** end ***
baud CON 84· 'baud rate = 9600-8-N-1
A·· VAR Byte
r0· VAR Nib
r1· VAR Byte
r2· VAR Byte
vcc PIN 7· ' eeprom is power on/off by BS2
scl PIN 3· ' SCL pin pull up with 10k to +5V
sda PIN 1· ' ditto for SDA pin
LED PIN 6· ' LED, active low
'sw· PIN 2
OUTPUT scl
OUTPUT sda
'INPUT sw
start:
· LOW vcc
· HIGH led
· DO
· #IF _24C01A #THEN
··· SEROUT 16,baud,[noparse][[/noparse]LF,CR,
··················· "24C01A",LF,CR]
··· #ELSE
··· SEROUT 16,baud,[noparse][[/noparse]LF,CR,
··················· "24C01",LF,CR]
··· #ENDIF
··· SEROUT 16,baud,[noparse][[/noparse]"1- Copy, BS2 > eeprom",LF,CR,
··················· "2- Backup, eeprom > BS2",LF,CR,
··················· "3- Display eeprom",LF,CR,
··················· "4- Display BS2",LF,CR]
··· SERIN 16,baud,[noparse][[/noparse]r2]
'main:
'··· IF sw=1 THEN main
'release:
'··· IF sw=0 THEN release
··· SELECT r2
····· CASE "1"·· ' copy BS2 > eeprom
········ HIGH vcc
········ GOSUB I2C_start
········ #IF _24C01A #THEN
··········· A=%10100000
··········· GOSUB I2C_out
··········· GOSUB I2C_ack
········ #ENDIF
········ A=0
········ GOSUB I2C_out
········ GOSUB I2c_ack
········ FOR r2=0 TO eeprom_size-1
··········· #IF _24C01A #THEN
·············· A=r2//8
·············· IF A=0 THEN
················· GOSUB I2C_stop
················· GOSUB I2C_start
················· A=%10100000
················· GOSUB I2C_out
················· GOSUB I2C_ack
················· A=r2
················· GOSUB I2C_out
················· GOSUB I2C_ack
·············· ENDIF
··········· #ELSE
·············· A=r2//4
·············· IF A=0 THEN
················· GOSUB I2C_stop
················· GOSUB I2C_start
················· A=r2<<1
················· A.BIT0=0
················· GOSUB I2C_out
················· GOSUB I2c_ack
·············· ENDIF
··········· #ENDIF
··········· READ r2,A
··········· GOSUB I2C_out
··········· GOSUB I2C_ack
········ NEXT
········ GOSUB I2C_stop
········ LOW vcc
········ HIGH led
····· CASE "2"·· ' copy eeprom > BS2
········ HIGH Vcc
········ GOSUB I2C_start
········ #IF _24C01A #THEN
··········· ' start of DUMMY WRITE
··········· A=%10100000
··········· GOSUB I2C_out
··········· GOSUB I2C_ack
··········· A=0
··········· GOSUB I2C_out
··········· GOSUB I2C_ack
··········· ' end of DUMMY WRITE
··········· GOSUB I2C_start
··········· A=%10100001
········ #ELSE
··········· A=1· ' read
········ #ENDIF
········ GOSUB I2C_out
········ GOSUB I2C_ack
········ FOR r2=0 TO eeprom_size-1
··········· GOSUB I2C_in
··········· LOW sda·· ' sda pull low, eeprom inc data word addr
··········· HIGH scl
··········· LOW scl
··········· TOGGLE LED
··········· WRITE r2,a
········ NEXT
········ GOSUB I2C_no_ack
········ GOSUB I2C_stop
········ LOW vcc
········ HIGH led
····· CASE "3" TO "4"
······· IF r2="3" THEN··· ' display eeprom
·········· HIGH Vcc
·········· GOSUB I2C_start
·········· #IF _24C01A #THEN
··········· A=%10100000
··········· GOSUB I2C_out
··········· GOSUB I2C_ack
··········· A=0
··········· GOSUB I2C_out
··········· GOSUB I2C_ack
··········· GOSUB I2C_start
··········· A=%10100001
·········· #ELSE
············· A=1· ' read
·········· #ENDIF
·········· GOSUB I2C_out
·········· GOSUB I2C_ack
······· ENDIF
······· SEROUT 16,baud,[noparse][[/noparse]LF,CR,"DATA "]
······· FOR r1= 0 TO eeprom_size-1
········· IF r2="4" THEN
············ READ r1,A····· ' read from BS2
········· ELSE
············ GOSUB I2C_in·· 'read from eemprom
············ LOW sda·· ' sda pull low, eeprom inc data word addr
············ HIGH scl
············ LOW scl
············ TOGGLE LED
········· ENDIF
········· SEROUT 16,baud,[noparse][[/noparse]"$",HEX2 A]
········· A=(r1+1)//16
········· IF A=0 THEN
············ SEROUT 16,baud,[noparse][[/noparse]LF,CR,"DATA "]
········· ELSE
············ SEROUT 16,baud,[noparse][[/noparse]","]
········· ENDIF
······· NEXT
······· IF r2="3" THEN
·········· GOSUB I2C_no_ack
·········· GOSUB I2C_stop
·········· LOW Vcc
·········· HIGH LED
······· ENDIF
··· ENDSELECT
· LOOP
END
I2C_start:
· HIGH sda
· HIGH scl
· LOW sda
· LOW scl
RETURN
I2C_stop:
· LOW sda
· HIGH scl
· HIGH sda
· LOW scl
RETURN
I2C_ack:
· HIGH scl
· INPUT sda
· IF sda=0 THEN
··· TOGGLE LED
· ELSE
··· LOW scl
··· SEROUT 16,baud,[noparse][[/noparse]"Err",CR]
··· STOP
· ENDIF
· LOW scl
· OUTPUT sda
RETURN
I2C_no_ack:
· PULSOUT scl,5
RETURN
I2C_out:
· ' Byte to be send put into valuable A
· LOW scl····· ' low the scl to put the data in sda
· FOR r0=1 TO size
··· IF A.BIT7=1 THEN· ' MSB send out first
····· HIGH sda
··· ELSE
····· LOW sda
··· ENDIF
··· A=A << 1
··· PULSOUT scl,5
· NEXT
RETURN
I2C_in:
· LOW scl
· A=0
· FOR r0=1 TO size
··· A=A << 1
··· INPUT sda
··· IF sda=1 THEN
····· A.BIT0=1
··· ELSE
····· A.BIT0=0
··· ENDIF
··· PULSOUT scl,5
· NEXT
· OUTPUT sda·· 'sda pin in output mode
RETURN
Regarding differences,
For one, SPI can be full duplex [noparse][[/noparse]transmit and receive at the same time], but it is not generally supported that way on either the BasicStamp or the SX. I2C is always half duplex {transmit or receive, but not both at the same time}.
Regarding limitations on available uses,
Again there are limitations in using the BasicStamp as either the SPI or the I2C device must be a Slave and multiple masters is not available.
Regarding wiring,
Generally, the SPI requires a CS [noparse][[/noparse]chip select wire] to each device, while I2C sometimes use software addessses, but I seem to see some with a CS too. But, if you are only using one devices, the CS feature is often redundant and just pulled to the appropriate HIGH or LOW.
I think that you might be trying to fathom all the exotics - like Mutiple Masters or slowing down transmission. Learn how the BasicStamp uses the devices, get some working -- then think about what you can't have.
The Dallas ONE-Wire are really in their own world should be viewed as a completely different set of design issues.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"When all think alike, no one is thinking very much.' - Walter Lippmann (1889-1974)
······································································ Warm regards,····· G. Herzog [noparse][[/noparse]·黃鶴 ]·in Taiwan
·· Attaching the code would be better with a program of that size.· Thanks.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com