![]() |
Forum Index : Microcontroller and PC projects : Serach for multiple 1-Wire devices
Author | Message | ||||
jman![]() Guru ![]() Joined: 12/06/2011 Location: New ZealandPosts: 711 |
Hi Inspired by this post Help with 1-Wire Temp reading I took a look into using MMBasic to search for multiple 1-wire devices below is port of a program that was originally written in PIC Basic that does just that. Here is a link to the Maxim application note this explains it rather well and includes a nice flow chart AppNote187 Option Explicit Dim A%, PinNbr Dim id_bit, cmp_id_bit, LastDeviceFlag, Last_zero Dim search_direction, id_bit_number, LastDiscrepancy Dim DeviceCount, ROM_NO(8) Dim LastFamilyDiscrepancy, I, J PinNbr = 17 '=================================================================== Device_loop: 'Device loop: loop for all devices on the bus OneWire Reset Pinnbr If LastDeviceFlag = 1 Then Print "--Done--": End id_bit_number = 1 last_zero = 0 OneWire Write PinNbr,1,1,&HF0 'Send the Search ROM command (FO) '=================================================================== Check_bit: OneWire Read PinNbr, 4 , 2, 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 I=Fix(((id_bit_number)-1)/8) Rom_NO(I) = Rom_NO(I) << 1 ROM_NO(I) = Rom_no(I)+search_direction EndIf If ((id_bit = 0) And (cmp_id_bit = 0)) Then GoSub conflict OneWire Write PinNbr,4,1,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 GoTo Check_bit 'Check for next bit If id_bit_number > 64 Then LastDiscrepancy = last_zero If LastDiscrepancy = 0 Then LastDeviceFlag = 1 For i= 0 To 7 A%=Rom_no(I) Rom_No(I) = ((A% * &h202020202) And &h10884422010) Mod 1023 'Thanks Rob Next I Print "Family ";Hex$(ROM_NO(0));"H" Print "ID:", Hex$(ROM_NO(1)),Hex$(ROM_NO(2)),Hex$(ROM_NO(3)),Hex$(ROM_NO(4)),Hex$(ROM_NO(5)),Hex$(ROM_NO(6)) Print "CRC:", Hex$(ROM_NO(7)) deviceCount = deviceCount + 1 For J=0 To 7 Rom_no(J)=0 Next J If LastDeviceFlag = 0 Then GoTo Device_loop Print "DeviceCount";deviceCount, " device(s)." Print "--Done--":End '=================================================================== NO_device: 'Since both response bits are 11, there are no devices Print " No devices present" End '=================================================================== Conflict: 'id_bit_number = LastDiscrepancy then take "1" path If (id_bit_number) = LastDiscrepancy Then search_direction = 1 I=Fix(((id_bit_number)-1)/8) Rom_NO(I) = Rom_NO(I) << 1 Rom_NO(I) = Rom_NO(I)+search_direction EndIf 'id_bit_number > LastDiscrepancy then take the "0" path If (id_bit_number > LastDiscrepancy) Then search_direction = 0 I=Fix(((id_bit_number)-1)/8) Rom_NO(I) = Rom_NO(I) << 1 ROM_NO(I) = ROM_NO(I)+search_direction Rom_NO(I) = Rom_NO(I)+search_direction GoSub Family EndIf ' id_bit_number < LastDiscrepancy then take same path as last time If (id_bit_number < LastDiscrepancy) Then I=Fix(((id_bit_number)-1)/8) search_direction = ROM_NO(I) 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 '=================================================================== Enjoy Jman |
||||
Phil23 Guru ![]() Joined: 27/03/2016 Location: AustraliaPosts: 1667 |
Hi Jman, Have run this and your other routine on my single sensor & noticed a slight issue in the MMChat terminal in ASCII mode. The tab characters seem to be suppressed between each Octet, and of particular relevance is the loss of the leading zero on the last octet. All ok on a VT100 terminal. No issues with the code at all. Run Family 28H ID:FFA667A8151 CRC:6F DeviceCount 1 device(s). --Done-- > Run Family 28H ID: FF A6 67 A8 15 1 CRC: 6F DeviceCount 1 device(s). --Done-- > Only a display issue in ASCII mode but did throw me for a few seconds. Cheers |
||||
Phil23 Guru ![]() Joined: 27/03/2016 Location: AustraliaPosts: 1667 |
Have to say, I thinks this code, along with the other to read multiple sensors on a single pin will be of huge benefit to anyone doing any heating/cooling type monitoring & control. With a minimum of 10 separate rooms/zones in our house, wiring that number of probes back to a controller would be a total nightmare. Daisy chaining 10 sensors room to room in the ceiling space and feeing them back on a single cable; an absolute snack. Here's hoping this develops further, maybe even into a cfunction or native command. Cheers |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
MMEdit doesn't process the tab character. Maybe one day... Re the leading zeros: Replace all Hex$(ROM_NO(x)) with Hex$(ROM_NO(x),2) on lines 45-47 The leading character option for HEX$ is a recent addition to MMBasic. It saves a lot of work. Jim VK7JH MMedit |
||||
Phil23 Guru ![]() Joined: 27/03/2016 Location: AustraliaPosts: 1667 |
Just noticed something interesting about this routine. It runs fine with 3 sensors connected, but when I added an extra 2 it seems to go into an indefinite loop. Cheers. |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |