PicBasic Pro, PicBasic, BASIC Stamp, Microchip PIC, 8051, and Remote Control Projects

-- Reading ROM Codes From 1-Wire Devices  --

PicBasic Pro version 2.4 now includes commands specifically for using the Dallas Semiconductor 1-Wire™ components. This short article will show how to use these new 1-Wire commands to build a simple device that identifies Dallas 1-Wire components.

If you're planning to use 1-wire components you need to identify individual components before placing them on the 1-wire network, this little project comes in handy for doing this.

Components required for this project:

bulletPIC16F877 microcontroller
bullet20 x 4 Serial LCD
bulletA Couple of 1-Wire Devices
bullet4.7K Resistor

You're not necessarily limited to using the components listed above. The code is easy to convert to any PIC, and the serial LCD can easily be replaced by using any RS-232 terminal program.

By first identifying each 1-wire device, and recording the unique 64-bit ROM number contained within each device, you can then communicate with individual 1-wire components on the 1-wire bus. To do this you use the "Match Rom" command followed by the unique 64-bit ROM number matching a specific 1-wire component on the network you wish to communicate with.

Here's an example of talking to a 1-wire DS2405 addressable switch located on the 1-wire network.

Note: You can toggle the output of the DS2405 by simply sending the Match Rom command followed by the 64-bit ROM code of the switch. This ROM code is unique from one DS2405 to the other.

Assuming you have first read the unique 64-bit ROM code from the DS2405 switch:

' Use the routine below to toggle the addressable switch
Toggle_Switch:
    OWOUT DQ, 1, [$55,$05,$C5,$C3,$08,$00,$00,$00,$CD]

The Match ROM command is $55 or 55h. Following the Match ROM command is the 64-bit ROM code read from the DS2405 as shown in the table below. Notice data is sent LSB first. When communicating with devices on the 1-wire network, data is always read/written LSB first.

 

MSB   LSB
8-Bit CRC Code 48-Bit Serial Number 8-Bit Family Code
CDh 00h 00h 00h 08h C3h C5h 05h

Refer to the datasheet for the specific device you plan to use. The datasheet will show a table similar to the grey section of the table above. All 64-bits of data in the table above were first read from a DS2405 switch placed in circuit with the code example shown below. $55 is the Match Rom command. Following the Match Rom command is the unique 64-bit ROM code read from the DS2405 addressable switch. This allows the PicBasic Pro command OWOUT to address this particular DS2405 addressable switch on the 1-wire network.

The photo above shows the LCD display with the DS2405 inserted in the circuit. Here's the simple circuit to build for this project for connecting 1-wire components to the PIC.

After sending the unique ROM code matching the switch you wish to control, this device matching the unique ROM code will be the only one on the 1-wire net that responds to further commands. The remaining 1-wire components on the network will sit by waiting for a reset pulse.

Since the DS2405 doesn't require a specific command to toggle its switch output from 0-1 or 1-0, you're finished commanding the DS2405 to toggle its switch output. Issuing the exact same sequence [shown above] again, will command the DS2405 to toggle its output once again.

The DS18B20 Temperature Sensor

Below is a photo of the results shown on the serial LCD after we insert the DS18B20 temperature sensor into the 1-wire device identifier circuit.

Notice how each different 1-wire device contains a totally unique 64-bit ROM code that identifies the:

1. 8-Bit Family Code
2. Unique
48-Bit Serial Number
3. 8-Bit CRC Code

This project shows how to identify each 1-wire device prior to inserting them into the 1-wire network. By identifying each component you'll know the unique 64-bit ROM code for each 1-wire device, and be able to talk to individual components on your 1-wire network as necessary. You can use the 1-wire command Search ROM [$F0], but this doesn't tell you where each device is physically located on the 1-wire network. It only identifies all components on the network.

The next project will give more detailed explanations of how the PicBasic Pro OWIN & OWOUT mode numbers affect different devices on the 1-wire network.

LCD Connections

The 20 x 4 LCD used for this project is a standard backlit serial LCD similar to this Serial LCD , but any serial LCD should work. You may or may not need to change the LCD commands, depending on which type you use. Just connect PortB.0 to the serial 'data' input of the one you use.

The Code

'****************************************************************
'*  Name    : 1-Wire-ID.Bas                                     *
'*  Author  : Bruce Reynolds                                    *
'*  Date    : 11/10/2001                                        *
'*  Version : 1.0                                               *
'*  Notes   : 1-Wire Identifier                                 *
'*          ; Identifies & Prints 1-Wire Device Information     *
'*          : Uses 20 x 4 Serial LCD for Display                *
'****************************************************************
DEFINE  LOADER_USED 1   ' Boot loader is being used
DEFINE  DEBUG_MODE  1   ' Send Inverted serial data with debug
DEFINE  DEBUG_REG PortB ' Debug Port = PortB
DEFINE  DEBUG_BIT 0     ' Debug.bit = PortB.0
DEFINE  DEBUG_BAUD 9600 ' Default baud rate = 9600
DEFINE  OSC 4           ' We're using a 4 MHz oscillator
DQ      VAR PortC.0	' One-wire data pin "DQ" on PortC.0
Clr     CON 1           ' Serial LCD command to clear LCD
LINE1   CON 128         ' Line #1 of serial LCD
LINE2   CON 192         ' Line #2 of serial LCD
LINE3   CON 148         ' Line #3 of serial LCD
LINE4   CON 212         ' Line #4 of serial LCD
Ins     CON 254         ' Instruction for LCD command-mode
ID      VAR BYTE[8]     ' Array storage variable for 64-bit ROM code

Begin:
    PAUSE 500           ' Wait .5 second
    DEBUG Ins,Clr       ' Clear LCD on power-up
    
Start_Convert
    OWOUT DQ, 1, [$33]  ' Issue Read ROM command

ID_Loop:
    OWIN DQ, 0, [STR ID\8]' Read 64-bit device data into the 8-byte array "ID"
    DEBUG Ins,Line1,"Family Code = ",HEX2 ID[0],"h"
    DEBUG Ins,Line2,"Ser# = ",HEX2 ID[1],HEX2 ID[2],HEX2 ID[3],HEX2 ID[4],HEX2 ID[5],HEX2 ID[6],"h"
    DEBUG Ins,Line3,"CRC Value = ",HEX2 ID[7],"h"
ID_Device:
    IF ID[0] = $5 THEN
       DEBUG Ins, Line4, "Device = Switch     "
    ENDIF
    IF (ID[0] = $28) OR (ID[0] = $22) THEN
       DEBUG Ins,Line4, "Device = Temp Sensor"
    ENDIF
    IF ID[0] = $01 THEN
       DEBUG Ins,Line4, "Device = Serial #   "
    ENDIF
    PAUSE 10000         ' 10-second pause
    GOTO Start_Convert 
    
    END
Part 2 is ready. Click HERE

Part 2. How to communicate with multiple 1-wire devices on the network, and how to use PicBasic Pro 1-wire command mode switches. How to read/display temperatures from 2 DS18B20 temp sensors, and control 2 DS2405 1-wire addressable switches. Complete with °C to °F conversion, how to display -°F temperatures, individual DS2405 switch status displays, and more.

For your convenience, we keep a few of the DS18B20 temperature sensors in-stock HERE.

Until the next project - have fun - and don't blow anything up...;o]

Regards,

-Bruce

  BASIC Stamp, Microchip PIC, 8051, and Remote Control Mailing-List"Micro-News"BASIC Stamp, Microchip PIC, 8051, and Remote Control Mailing-List
   Micro-Mailing-List

Copyright © 1999-2008 Reynolds Electronics

| Contact Information |