I have been experimenting with Dallas 1-wire devices and ibuttons. I had a look though the melabs list but only found one search device example and did not manage to get it to work right.

I did a complete rewrite and here it is. It follows the routine as found in the Dallas aplication note 187.

The note has a nice flowchart and I used the variable names that are in the chart.


Hi, we are pleased to see that you are using our forum.

We'd like you to become a member of our community. Membership is FREE, please REGISTER and view our forums without these annoying reminders!

Once registered you may post on the forums, download from the file areas and use the WIKI without interruption.






'************************************************* ***************
'* Name : ID_ROM_9.pbp *
'* Author : Jim Morrissey *
'* Notice : Copyright (c) 2006 Jim Morrissey *
'* : All Rights Reserved *
'* Date : 07/04/2006 *
'* Version : 9.00 *
'* Notes : *
'* : using bootstrap loader on 16F876 *
'* : and 10 Mhz clock *
'* : Flow Chart in Dallas Application Note 187 *
'************************************************* ***************



DEFINE LOADER_USED 1 'for bootstrap loader
DEFINE OSC 10

DQ VAR PORTB.1 ' one wire data pin "DQ" on PORTB 1
SEARCH_ROM CON $F0

id_bit VAR Bit ' result of bit read from SearchROM command
cmp_id_bit VAR Bit ' complement of the bit read

LastDeviceFlag VAR Bit ' flag previous search was last device
search_direction VAR Bit ' bit indicates direction of search.
id_bit_number VAR Byte ' The ROM bit number currently being searched
LastDiscrepancy VAR byte ' bit index from which bit (next)search
' discrepency check should start
LastFamilyDiscrepancy VAR byte ' bit index identifies last discrepacy
' within first 8 bit family code of ROM
last_zero VAR byte ' bit position of last zero written
ROM_NO VAR Byte(8) ' Current ROM number being searched
DeviceCount VAR Byte ' devices found


'Set up VARiables to run program at start.


DeviceCount = 0
LastFamilyDiscrepancy = 0
LastDiscrepancy = 0
LastDeviceFlag = 0
last_zero = 0

Pause 3000

SEROUT2 PORTC.6,84,[ 13, 10, 13, 10, "1-wire Device Search Version 9.0", 10,13,10,13]



'Device loop: loop for all devices on the bus, until there are no more.

Device_loop:


OWOUT DQ,1 'Send the Search ROM command

IF LastDeviceFlag = 1 THEN Done

id_bit_number = 1
last_zero = 0

OWOUT DQ,0,[SEARCH_ROM] 'Send the Search ROM command (FO)



'************************** Check bit routine *************************
Check_bit:

OWIN DQ,4,[id_bit, cmp_id_bit] 'Get response from device



IF (id_bit = 1 And cmp_id_bit = 1) THEN GOTO No_device

IF NOT ((id_bit = 0) and (cmp_id_bit = 0)) THEN
search_direction = id_bit
ROM_NO.LowBit[id_bit_number-1] = search_direction
ENDIF

IF ((id_bit = 0) and (cmp_id_bit = 0)) THEN gosub conflict



OWOUT DQ,4,[search_direction] ' send 1 or 0 as search direction
id_bit_number = id_bit_number+1 ' incredement id_bit_number

IF id_bit_number <= 64 THEN Check_bit 'head back for next bit

IF id_bit_number > 64 THEN LastDiscrepancy = last_zero

IF LastDiscrepancy = 0 THEN LastDeviceFlag = 1


'************************************************* *********************
SEROUT2 PORTC.6,84,["Typ:", HEX2 ROM_NO[0], "h "]
SEROUT2 PORTC.6,84,["ID:", HEX2 ROM_NO[6],HEX2 ROM_NO[5],HEX2 ROM_NO[4],HEX2 ROM_NO[3],HEX2 ROM_NO[2],HEX2 ROM_NO[1],"h "]
SEROUT2 PORTC.6,84,["CRC:", HEX2 ROM_NO[7], "h", 13, 10]
deviceCount = deviceCount + 1

IF LastDeviceFlag = 0 THEN Device_loop
SEROUT2 PORTC.6,84,[13, 10, DEC deviceCount, " device(s).", 10, 13,10,13]
GOTO Done
END



'************************** Subs ************************************

NO_device: ' Since both response bits are 11, there are no devices
SEROUT2 PORTC.6,84,[" No devices present", 10, 13]
END
Done:
LastFamilyDiscrepancy = 0
LastDiscrepancy = 0
LastDeviceFlag = 0
SEROUT2 PORTC.6,84,[" ------------ All done ---------------", 10, 13]
END




conflict:
' id_bit_number = LastDiscrepancy then take "1" path
IF ((id_bit_number) = LastDiscrepancy) THEN
search_direction = 1
ROM_NO.LowBit[id_bit_number-1] = search_direction

ENDIF

'id_bit_number > LastDiscrepancy then take the "0" path
IF (id_bit_number > LastDiscrepancy) THEN
search_direction = 0
ROM_NO.LowBit[id_bit_number-1] = search_direction
GOSUB Family
ENDIF

' id_bit_number < LastDiscrepancy then take same path as last time
IF (id_bit_number < LastDiscrepancy) THEN
search_direction = ROM_NO.LowBit[id_bit_number-1]
IF search_direction = 0 THEN GOSUB Family
ENDIF

return

Family: 'LastFamilyDiscrepancy bit index set
last_zero = id_bit_number
IF last_zero <9 THEN
LastFamilyDiscrepancy = last_zero
endIF
RETURN