Shop OBEX P1 Docs P2 Docs Learn Events
Bs2p and 1 wire? — Parallax Forums

Bs2p and 1 wire?

ArchiverArchiver Posts: 46,084
edited 2001-03-07 14:33 in General Discussion
On 6 Mar 01 at 20:53, Robert Kubichek wrote:

> I was wondering if anyone can help me with the code for reading more
> than 1 device on the 1 wire buss??? I would like to read multiple
> DS1820 chips...

You'll need to know the 8-byte serial ID for each device and use the
appropriate ID with MATCH ROM commands to select the desired device.
You can visually read the ID on most iButtons. For devices like the
DS1820, the following program will read a device's serial ID out of the
lasered ROM (there must be one and only one device on the bus when this
program runs!)

I hope this helps,

Steve


'**************************************************************************
'
' Serial Number Extractor
'
'**************************************************************************

READ_ROM CON $33

' I/O pin constants
BUS CON 15 ' 1-Wire bus I/O pin

' conventional variables
in_byte VAR BYTE(10)
i VAR BYTE


'**************************************************************************
' read ROM-based identifier...this will work with any 1-Wire device...
' assumes one and only one device present on bus...
'
' 1-Wire transactions summary:
' ---> [noparse][[/noparse]reset]
' ---> READ ROM
' <--- 8 Serial # identifier bytes
'**************************************************************************
OWOUT BUS,1,[noparse][[/noparse]$33] ' send READ ROM
OWIN BUS,0,[noparse][[/noparse]STR in_byte\8] ' read 8-byte serial identifier
DEBUG CR,"Serial # "
FOR i = 7 TO 0 ' display identifier high order
DEBUG HEX2 in_byte(i) ' byte first
NEXT



Make a note of each ID. The following program shows one way to use
them, and shows most of the tricks a DS18S20 knows.



'**************************************************************************
'
' DS18S20 Digital Thermometer/DS1920 Temperature iButton
'
'**************************************************************************

' Universal 1-Wire ROM commands
READ_ROM CON $33
SKIP_ROM CON $CC
MATCH_ROM CON $55
SEARCH_ROM CON $F0
SEARCH_FUNC CON $EC

' I/O pin constants
BUS CON 15 ' 1-Wire bus I/O pin

' OWIN, OWOUT constants
RESET_NONE CON 0
RESET_PRE CON 1
RESET_POST CON 2
RESET_BOTH CON 3

' conventional variables
temperature VAR WORD
current_device VAR NIB
io_bytes VAR BYTE(10)
i VAR BYTE
j VAR BYTE


' define 8-byte serial identifiers for each device...family code first,
' then low order thru high order serial number, finally crc byte
thermo_1 DATA $10,$7E,$85,$48,$00,$00,$00,$95
thermo_2 DATA $10,$8F,$94,$48,$00,$00,$00,$61
devices CON 2

'**************************************************************************
' Load the serial ID's into scratchpad RAM for easy recall
'**************************************************************************
FOR i = 0 TO 8 * devices - 1
READ i,j
PUT 100 + i,j
NEXT

current_device = 0
GOSUB readTemperature
GOSUB showScratchpad
GOSUB readPower
GOSUB setAlarmLimits

current_device = 1
GOSUB readTemperature
GOSUB showScratchpad
STOP

readTemperature:
'**************************************************************************
' get temperature data, convert from Centigrade to Farenheit...
'
' 1-Wire transactions summary:
' ---> [noparse][[/noparse]reset]
' ---> MATCH ROM
' ---> 8 serial # identifier bytes
' ---> CONVERT command
' [noparse][[/noparse]pause]

' ---> [noparse][[/noparse]reset]
' ---> MATCH ROM
' ---> 8 serial # identifier bytes
' ---> READ SCRATCHPAD command
' <--- 9 data bytes
'**************************************************************************
GOSUB selectDevice ' select device with MATCH ROM
OWOUT BUS,RESET_NONE,[noparse][[/noparse]$44] ' send convert command
PAUSE 750 ' allow conversion to complete
GOSUB selectDevice ' select device with MATCH ROM
OWOUT BUS,RESET_NONE,[noparse][[/noparse]$BE] ' send scratchpad read command
OWIN BUS,RESET_NONE,[noparse][[/noparse]temperature.LOWBYTE,temperature.HIGHBYTE] ' read
OWIN BUS,RESET_NONE,[noparse][[/noparse]STR io_bytes\7] ' scratchpad data
DEBUG CR,CR,"Temperature: ": GOSUB centigradeToFarenheit
RETURN

setAlarmLimits:
'**************************************************************************
' set alarm upper and lower limits, show new scratchpad data, verify CRC:
'
' 1-Wire transactions summary:
' ---> [noparse][[/noparse]reset]
' ---> MATCH ROM
' ---> 8 serial # identifier bytes
' ---> WRITE SCRATCHPAD command
' ---> 2 bytes (high temperature alarm, low temperature alarm)

' ---> [noparse][[/noparse]reset]
' ---> MATCH ROM
' ---> 8 serial # identifier bytes
' ---> READ SCRATCHPAD command
' <--- 9 data bytes

' ---> [noparse][[/noparse]reset]
' ---> MATCH ROM
' ---> 8 serial # identifier bytes
' ---> COPY SCRATCHPAD command
' [noparse][[/noparse]pause]

' ---> [noparse][[/noparse]reset]
' ---> MATCH ROM
' ---> 8 serial # identifier bytes
' ---> RECALL E^2 command

' ---> [noparse][[/noparse]reset]
' ---> MATCH ROM
' ---> 8 serial # identifier bytes
' ---> READ SCRATCHPAD command
' <--- 9 data bytes
'**************************************************************************
GOSUB selectDevice ' select device with MATCH ROM
OWOUT BUS,RESET_NONE,[noparse][[/noparse]$4E,$50,$10] ' send write scratchpad command, data
GOSUB selectDevice ' select device with MATCH ROM
OWOUT BUS,RESET_NONE,[noparse][[/noparse]$BE] ' send scratchpad read command
OWIN BUS,RESET_NONE,[noparse][[/noparse]STR io_bytes\9] ' read in scratchpad data
DEBUG CR,CR,"DS18S20 temperature limits:"
DEBUG CR,"TH: ",DEC io_bytes(2),CR,"TL: ",DEC io_bytes(3)
GOSUB selectDevice ' select device with MATCH ROM
OWOUT BUS,RESET_NONE,[noparse][[/noparse]$48] ' send scratchpad copy command
PAUSE 750
GOSUB selectDevice ' select device with MATCH ROM
OWOUT BUS,RESET_NONE,[noparse][[/noparse]$B8] ' send recall e^2 to scratchpad command
OWOUT BUS,RESET_PRE,[noparse][[/noparse]SKIP_ROM,$BE] ' command scratchpad read
OWIN BUS,RESET_NONE,[noparse][[/noparse]temperature.LOWBYTE,temperature.HIGHBYTE] ' read
OWIN BUS,RESET_NONE,[noparse][[/noparse]STR io_bytes\7] ' scratchpad data
RETURN

readPower:
'**************************************************************************
' read power supply status
'
' 1-Wire transactions summary:
' ---> [noparse][[/noparse]reset]
' ---> MATCH ROM
' ---> 8 serial # identifier bytes
' ---> READ POWER SUPPLY command
' <--- 1 data byte (really need just one bit, but...)
'**************************************************************************
GOSUB selectDevice ' select device with MATCH ROM
OWOUT BUS,RESET_NONE,[noparse][[/noparse]$B4] ' send read power supply command
OWIN BUS,RESET_NONE,[noparse][[/noparse]io_bytes] ' read in power supply status
DEBUG CR,CR,"DS18S20 power supply status: "
DEBUG BIN1 io_bytes,CR
RETURN


centigradeToFarenheit:
'**************************************************************************
' if temperature is negative deg C, use absolute value in applying
' formula...deg F = deg C * 9 / 5 = (2 * deg C) * 9 / 10...if Farenheit
' < 0, show '-' and display absolute value
'**************************************************************************
i = temperature.HIGHBYTE.HIGHBIT ' 1 if deg Celsius negative else 0
temperature = temperature ^ (i * $FFFF) + i * 9 / 10 ^ (i * $FFFF) + i + 32
i = temperature.HIGHBYTE.HIGHBIT ' 1 if deg Farenheit negative else 0
DEBUG "-" - " " * i + " ", DEC (temperature ^ (i * $FFFF) + i)," "
RETURN


selectDevice:
'**************************************************************************
' send match rom and serial ID associated with current_device
'**************************************************************************
FOR i = 0 TO 7
j = current_device * 8 + i
GET 100 + j,io_bytes(i)
NEXT
OWOUT BUS,RESET_PRE,[noparse][[/noparse]MATCH_ROM,STR io_bytes\8]
RETURN


showScratchpad:
'**************************************************************************
' display temperature alarm limits, counts, crc, reserved bytes
'**************************************************************************
DEBUG CR,"DS18S20 scratchpad data:"
DEBUG CR,"TH: ",DEC io_bytes," TL: ",DEC io_bytes(1)
DEBUG CR,"CR: ",DEC io_bytes(4)," CP: ",DEC io_bytes(5)
DEBUG CR,"CRC:",DEC io_bytes(6)," Res: ",HEX2 io_bytes(2),HEX2 io_bytes(3)
RETURN

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2001-03-07 02:53
    I was wondering if anyone can help me with the code for reading more
    than 1 device on the 1 wire buss??? I would like to read multiple
    DS1820 chips. There is not much info in the latest manual.
    Thanks!
    --
    Robert Kubichek N9LVU
  • ArchiverArchiver Posts: 46,084
    edited 2001-03-07 03:41
    [font=arial,helvetica]In a message dated 3/6/01 9:54:19 PM Eastern Standard Time,
    bobn9lvu@lakefield.net writes:


    was wondering if anyone can help me with the code for reading more
    than 1 device on the 1 wire buss??? ·I would like to read multiple
    DS1820 chips. There is not much info in the latest manual.
    Thanks!
    --
    Robert Kubichek N9LVU



    Yes this would be helpful, (Im strictly a hobbyist) and I would like to see
    some code for other 1-wire devices also, reading files , etc. Thanks also.
    Lou[/font]
  • ArchiverArchiver Posts: 46,084
    edited 2001-03-07 04:00
    [font=arial,helvetica]In a message dated 3/6/01 8:54:19 PM Central Standard Time,
    bobn9lvu@lakefield.net writes:


    I was wondering if anyone can help me with the code for reading more
    than 1 device on the 1 wire buss??? ·I would like to read multiple
    DS1820 chips. There is not much info in the latest manual.



    You need to know the serial numbers for each device. Once known, here's how
    you do it. ·This code handles two sensors and can easily be updated to handle
    any number of sensors you have. ·This demo also shows effective use of the
    BS2p LCDOUT command.


    '
    [noparse][[/noparse] Title ]
    '
    ' File...... DS1820-2.BSP
    ' Purpose... BS2p <--> Multiple DS1820s Demo
    ' Author.... Jon Williams
    ' E-mail.... jonwms@aol.com
    ' Started... 04 NOV 2000
    ' Updated... 25 JAN 2001

    ' {$STAMP BS2p}

    '
    [noparse][[/noparse] Program Description ]
    '
    ' This program reads and displays the temperature from multiple DS1820
    ' (1-wire) sensors.
    '
    ' Program requires 2x16 LCD
    ' ··- LCD.E --> Pin0 (pulled down [noparse][[/noparse]to ground] through 4.7K)
    ' ··- LCD.R/W --> Pin2 (or grounded for write-only operation)
    ' ··- LCD.RS --> Pin3
    ' ··- LCD.D4 --> Pin4
    ' ··- LCD.D5 --> Pin5
    ' ··- LCD.D6 --> Pin6
    ' ··- LCD.D7 --> Pin7


    '
    [noparse][[/noparse] Revision History ]
    '


    '
    [noparse][[/noparse] I/O Definitions ]
    '
    LCDpin CON 0 ' data on pins 4 - 7
    DS1820pin CON 15 ' DS1820 bus on pin 15


    '
    [noparse][[/noparse] Constants ]
    '
    ' LCD control characters
    '
    NoCmd CON $00 ' just print
    ClrLCD · CON $01 ············ ' clear the LCD
    CrsrHm · CON $02 ············ ' cursor home
    CrsrLf · CON $10 ············ ' cursor left
    CrsrRt · CON $14 ' move cursor right
    DispLf · CON $18 ' shift display left
    DispRt · CON $1C ' shift displayright
    DDRam ·· CON $80 ' Display Data RAM control
    Line1 CON $80 ' address of line 1
    Line2 CON $C0 ' address of line 2

    DegSym CON 223 ' degrees symbol

    ' 1-Wire Support
    '
    OW_FERst CON %0001 ' Front-End Reset
    OW_BERst CON %0010 ' Back-End Reset
    OW_BitMode CON %0100
    OW_HighSpd CON %1000

    ReadROM CON $33 ' read ID, serial num, CRC
    MatchROM CON $55 ' look for specific device
    SkipROM CON $CC ' skip ROM (one device)
    SearchROM CON $F0 ' search

    ' DS1820 control
    '
    ConvertTemp CON $44 ' do temperature conversion
    ReadScratch CON $BE ' read DS1820 scratchpad

    NumSensors CON 2 ' number of DS1820s


    '
    [noparse][[/noparse] Variables ]
    '
    sensor VAR Byte ' sensor number to process
    idx VAR Byte ' loop counter
    eeAddr VAR Byte ' ee address of ROM match
    romData VAR Byte(8) ' ROM data to DS1820
    tempIn VAR Word ' raw temperature
    sign VAR tempIn.Bit8 ' 1 = negative temperature
    tInLow VAR tempIn.LowByte
    tInHigh VAR tempIn.HighByte
    tSign VAR Bit
    tempC VAR Word ' Celsius
    tempF VAR Word ' Fahrenheit
    rjNum VAR tempIn ' right justified number
    rjSign VAR Bit ' sign for rj number
    pos VAR Byte ' position to print
    digits VAR Nib ' digits in rjNum
    width VAR Nib ' width of display


    '
    [noparse][[/noparse] EEPROM Data ]
    '
    ' ROM codes for connected sensors

    Temp1 DATA $10,$C9,$70,$45,$00,$00,$00,$FE
    Temp2 DATA $10,$81,$CF,$45,$00,$00,$00,$0A


    '
    [noparse][[/noparse] Initialization ]
    '
    LCD_Setup:
    ·PAUSE 500
    ·LCDCMD LCDpin,%00110000 : PAUSE 5 ' 8-bit mode
    ·LCDCMD LCDpin,%00110000 : PAUSE 0
    ·LCDCMD LCDpin,%00110000 : PAUSE 0
    ·LCDCMD LCDpin,%00100000 ' 4-bit mode
    ·LCDCMD LCDpin,%00101000 ' 2-line mode
    ·LCDCMD LCDpin,%00001100 ' no crsr, no blink
    ·LCDCMD LCDpin,%00000110 ' inc crsr, no disp shift


    '
    [noparse][[/noparse] Main Code ]
    '
    Main:
    ·LCDOUT LCDpin,ClrLCD,[noparse][[/noparse]"BSP <---> DS1820"]
    ·LCDOUT LCDpin,Line2, [noparse][[/noparse]"Multiple Sensors"]
    ·PAUSE 2000
    ·LCDCMD LCDpin,ClrLCD


    ShowDS1820s:
    ·LOOKUP sensor,[noparse][[/noparse]Temp1,Temp2],eeAddr ' point to ROM code
    ·GOSUB GetTemp
    ·LCDOUT LCDpin,Line1,[noparse][[/noparse]"Sensor ",("1"+sensor),":"]
    ·PAUSE 1000
    ·sensor = sensor + 1 // NumSensors ' loop through all sensors
    ·GOTO ShowDS1820s

    ·END


    '
    [noparse][[/noparse] Subroutines ]
    '
    GetTemp:
    ·FOR idx = 0 TO 7 ' load ROM pattern
    ···READ (eeAddr+idx),romData(idx)
    ·NEXT

    ·OWOUT DS1820pin,OW_FERst,[noparse][[/noparse]MatchROM,STR romData\8,ConvertTemp]

    WaitForConversion:
    ·PAUSE 20
    ·OWIN DS1820pin,OW_BitMode,[noparse][[/noparse]tempIn]
    ·IF tempIn = 0 THEN WaitForConversion

    ·OWOUT DS1820pin,OW_FERst,[noparse][[/noparse]MatchROM,STR romData\8,ReadScratch]
    ·OWIN ·DS1820pin,OW_BERst,[noparse][[/noparse]tInLow,tInHigh]

    ·tSign = sign ' save sign bit
    ·tempIn = tempIn/2 ' round to whole degrees
    ·IF tSign = 0 THEN NoNeg1
    ·tempIn = tempIn | $FF00 ' extend sign bits for negs

    NoNeg1:
    ·tempC = tempIn ' save Celsius value
    ·tempIn = tempIn */ $01CC ' multiply by 1.8
    ·IF tSign = 0 THEN NoNeg2 ' if neg, extend sign bits
    ·tempIn = tempIn | $FF00

    NoNeg2:
    ·tempF = tempIn+32 ' finish C -> F conversion

    ShowTemps:
    ·rjNum = tempC : width = 4 : pos = Line1+10
    ·GOSUB RJ_Print
    ·LCDOUT LCDpin,NoCmd,[noparse][[/noparse]DegSym,"C"]
    ·rjNum = tempF : width = 4 : pos = Line2+10
    ·GOSUB RJ_Print
    ·LCDOUT LCDpin,NoCmd,[noparse][[/noparse]DegSym,"F"]
    ·RETURN


    RJ_Print:
    ·rjSign = rjNum.Bit15
    ·rjNum = ABS(rjNum)
    ·digits = width
    ·LOOKDOWN rjNum,<[noparse][[/noparse]0,10,100,1000,65535],digits
    ·LCDOUT LCDpin,pos,[noparse][[/noparse]REP " "\(width-digits-1),13 * rjSign + " ",DEC rjNum]
    ·RETURN


    [/font]
  • ArchiverArchiver Posts: 46,084
    edited 2001-03-07 14:18
    > You'll need to know the 8-byte serial ID for each device and use the
    > appropriate ID with MATCH ROM commands to select the desired device.
    > You can visually read the ID on most iButtons.

    This relates to mine previous question. I'm playing with an iButton (the
    thermochron) on a PC. By the way, I love it, it's the cat's meow. IButtons
    have two connections, a ground and a data/power. How does one connect an
    iButton to the BS2P?

    Paul
  • ArchiverArchiver Posts: 46,084
    edited 2001-03-07 14:27
    [font=arial,helvetica]In a message dated 3/7/01 8:20:27 AM Central Standard Time,
    pverhage@sd131.k12.id.us writes:


    This relates to mine previous question. ·I'm playing with an iButton (the
    thermochron) on a PC. ·By the way, I love it, it's the cat's meow. ·IButtons
    have two connections, a ground and a data/power. ·How does one connect an
    iButton to the BS2P?



    The ground connection is obvious. ·The power/data connection is sent to a
    BS2p pin (your OW bus pin) and is also pulled-up to +5 volts through a 4.7K
    resistor. ·The BS2p makes 1-Wire a breeze.

    I just submitted my April column to Nuts & Volts: the subject is how to do a
    search for 1-Wire devices with the BS2p. ·With this code (no, I can't release
    it publicly until it's printed...), you can determine how many and what types
    of 1-Wire devices are connected to the Stamp (also does a CRC check). ·The
    serial numbers of found devices are stored in EEPROM so you can individually
    access them (as in Steve's demo).

    -- Jon Williams
    -- Dallas, TX[/font]
  • ArchiverArchiver Posts: 46,084
    edited 2001-03-07 14:33
    The ground connection is obvious. The power/data connection is sent to a
    BS2p pin (your OW bus pin) and is also pulled-up to +5 volts through a 4.7K
    resistor. The BS2p makes 1-Wire a breeze.

    ** That's what I was thinking, thanks for the explanation.

    I just submitted my April column to Nuts & Volts.

    ** I'll keep my eyes open for it.

    Paul
Sign In or Register to comment.