Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 19:00 13 Nov 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Micromite 1-wire search help

Author Message
helltek
Newbie

Joined: 04/02/2015
Location: United States
Posts: 29
Posted: 06:21pm 13 Apr 2020
Copy link to clipboard 
Print this post

Is there somewhere basic program for Micromite to search ONE WIRE bus? I tried to make sense of the Maxim flowchart but can't figure it out. I can read 1 or 2 devices but when I connect more, my search fails or runs in never-ending loop.
https://pdfserv.maximintegrated.com/en/an/AN187.pdf     'Maxim 1-wire search algorithm
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2171
Posted: 07:44pm 13 Apr 2020
Copy link to clipboard 
Print this post

I have never used it directly (if you discount the DS18B20 temp sensor where I use TEMPR) but, One-wire support is part of MMBasic. Taking a look at Appendix C of the Micromite Manual;

It says "For users of MMBasic on earlier devices... The OWSEARCH command and the OWCRC8() and OWCRC16() functions are not implemented", so I suspect any querying of the bus would be successive progression of the READ and dealing with null-responses (MM.ONEWIRE) - don't know what qualifies as "earlier"(?)
Edited 2020-04-14 05:53 by CaptainBoing
 
helltek
Newbie

Joined: 04/02/2015
Location: United States
Posts: 29
Posted: 09:04pm 13 Apr 2020
Copy link to clipboard 
Print this post

I tried to write basic program following the Maxim flowchart and my program doesn't work. Reads one device over and over. Someone good in basic programming would find my errors easily. I'm not able to figure it out.

--------------------------------


'Hardware
'--------------------
'PIC32MX170F256B-50I/SP 28 pin DIP                       'Micro with MMBasic firmware V5.5.2
'1k pullup resistor to 5V on PIN owp                     '1-wire data bus pullup on 5V tolerant pin


'Description, Firmware, Manuals
'--------------------
'http://geoffg.net/micromite.html                        'Firmware download (Basic w/ built-in editor)
'https://pdfserv.maximintegrated.com/en/an/AN187.pdf     'Maxim 1-wire search algorithm


Const owp = 15                                          'define pin numbers - one_wire_pin

maxdevices = 16                                         'maximum number of 1-wire devices
Dim ROMcode%(maxdevices)                                'ROMcode is the full 64-bit device number
Dim Family(maxdevices)                                  'Family code is 1 byte
Dim SerNo%(maxdevices)                                  'Serial number is 6 bytes
Dim CRC(maxdevices)                                     'CRC is 1 byte



GOSUB one_wire_search

END
'End of one_wire_search prog
'####################







'SUBROUTINES


one_wire_search:

devices = 0


discover_first_device:

'Maxim flowchart:
cmp_id_bit = 0                                         'complement of id_bit
id_bit = 0                                             'one bit of id_bit_number
id_bit_number = 1                                      'ROM bit number 1 to 64
LastDeviceFlag = 0                                     'last device in search
LastDiscrepancy = 0                                    'next search discrepancy check start
LastFamilyDiscrepancy = 0                              'last discrepancy in family code
last_zero = 0                                          'last zero written in discrepancy
ROM_NO = 0                                             '8-byte buffer
search_direction = 0                                   'devices with this bit stay in search


discover_next_device:

Family_nbr=0
SerNo_nbr%=0
CRC_nbr=0
ROMcode_nbr%=0

OneWire RESET owp
If MM.OneWire = 0 Then GoTo one_wire_owERROR1          'MM.ONEWIRE returns 0 if no presence pulse

OneWire WRITE owp, 0, 1, &hF0                          'pin, flag, length, data [, data] (F0h = Search)

count64bits:
For id_bit_number = 1 To 64
OneWire READ owp, 4, 1, a                              'read 1 bit of 1-wire devices
OneWire READ owp, 4, 1, b                              'read complement of 1 bit
'Print "search "i"   rd_a " Bin$(a,1)"   rd_b " BIN$(b,1);

search_direction=a
If a + b = 2 Then GoTo one_wire_owERROR2               'bit and complement bit don't match
If a + b = 0 And id_bit_number=lastdiscrepancy Then search_direction=1
If a + b = 0 And id_bit_number>lastdiscrepancy Then search_direction=0
If a + b = 0 And id_bit_number<lastdiscrepancy Then search_direction=a
If search_direction=0 THEN last_zero=id_bit_number
If last_zero<9 THEN LastFamilyDiscrepancy=last_zero
a=search_direction

'Print".   wr_a "Bin$(a,1)".   lastdiscrepancy "lastdiscrepancy"   last_zero "last_zero

OneWire WRITE owp, 4, 1, a                             'write bit to continue search

Print Bin$(a,1);
'################

If id_bit_number>0 And id_bit_number<9 Then Family_nbr=Family_nbr+(a<<(id_bit_number-1))
If id_bit_number>8 And id_bit_number<57 Then SerNo_nbr%=SerNo_nbr%+(a<<(id_bit_number-9))
If id_bit_number>56 And id_bit_number<65 Then CRC_nbr=CRC_nbr+(a<<(id_bit_number-57))
ROMcode_nbr%=ROMcode_nbr%+(a<<(id_bit_number-1))

Next id_bit_number

devices = devices + 1

If devices <= maxdevices Then Family(devices)=Family_nbr
If devices <= maxdevices Then SerNo%(devices)=SerNo_nbr%
If devices <= maxdevices Then CRC(devices)=CRC_nbr
If devices <= maxdevices Then ROMcode%(devices)=ROMcode_nbr%


Print

Print "Maxim flowchart: "
Print "cmp_id_bit              "Bin$(b,1)
Print "id_bit                  "Bin$(a,1)
Print "id_bit_number          "id_bit_number
Print "LastDeviceFlag         "LastDeviceFlag
Print "LastDiscrepancy        "LastDiscrepancy
Print "LastFamilyDiscrepancy  "LastFamilyDiscrepancy
Print "last_zero              "last_zero
Print "ROM_NO                 "ROM_NO
Print "search_direction       "search_direction

Print

Print "Family"(devices)"     = " Hex$(Family(devices), 2)"h"
Print "SerNo%"(devices)"     = " Hex$(SerNo%(devices), 12)"h"
Print "SerNo_nbr%   = "Hex$(serNo_nbr%, 12)"h"
Print "CRC"(devices)"        = " Hex$(CRC(devices), 2)"h"
Print "ROMcode%"(devices)"   = "Hex$(ROMcode%(devices), 16)"h"

Print "MM.ONEWIRE"(devices)" = " MM.ONEWIRE"  "
Print "owERROR"(devices)   " = " owERROR"  "
PRINT

Print "Device CRC:"

'check crc
c1%=0                      'clear crc "c1" prior sending first byte
b1%=Family(devices): n=1   'byte or bytes to calculate crc on and the number of bytes sent
GoSub crc_8
b1%=SerNo%(devices): n=6   'byte or bytes to calculate crc on and the number of bytes sent
GoSub crc_8
b1%=CRC(devices): n=1      'byte or bytes to calculate crc on and the number of bytes sent
GoSub crc_8

If c1%<>0 Then id_bit_number=devices: GoSub one_wire_owERROR3: RETURN
IF ROMcode%(1)=0 THEN devices=0: GOSUB one_wire_owERROR1: RETURN

Print
Print "-------------------"
Print

LastDiscrepancy=last_zero
If LastDiscrepancy <> 0 Then GoTo discover_next_device     'search for more devices
LastDeviceFlag=1


one_wire_search_end:
owERROR=0
Return

one_wire_owERROR1:
owERROR=1
Print: Print "owERROR1 MM.ONEWIRE = 0, Devices not found": Print
if TLCD_MENU=4 then Text 3, 42, "Device   Not Found", LT, 1, 1, &Hff3333, &H000000  'LCD TEXT
devices = 0: dev1wire = 0
Return

one_wire_owERROR2:
owERROR=2
Print: Print "owERROR2 True bit + Complement bit in search" id_bit_number " do not match": Print
Return

one_wire_owERROR3:
owERROR=3
Print: Print "owERROR3 CRC(device"(id_bit_number)") does not match": Print
Return




'--------------------
'CRC-8
'--------------------
'Calculate CRC-8 for integer "b1%" of any size from 1 byte up to full 1-wire device number
'"b1%" is the integer,
'"n" = number of bytes
'Add the CRC-8 value into integer "c1%" after each byte
'"c1%" is cleared to zero for the first byte sent
'"d1%" is the data after each shift
'"e1%" is the CRC-8 Polynomial

'Compare CRC LSB bit with DATA LSB bit
'If they are equal shift right CRC and DATA
'If they are not equal shift both CRC and DATA and XOR
'the Polynomial to ehe CRC after being shifted
'Use the final CRC for the next byte

'The Polynomial e1% x8+x5+x4+1 is 10001100b in binary form

'XOR Function: x1  x2  |  y
'              ------------
'               0   0  |  0
'               0   1  |  1
'               1   0  |  1
'               1   1  |  0

crc_8:

e1% = &b10001100
d1%=b1% And &hFF
i=0
crc_8_loop:

If i<8 And (c1% And &b00000001)=(d1% And &b00000001) Then c1%=c1%>>1: d1%=d1%>>1: x=0 :i=i+1
If i<8 And (c1% And &b00000001)<>(d1% And &b00000001) Then c1%=c1%>>1: d1%=d1%>>1: c1%=(e1% Xor c1%): x=1: i=i+1

If i<8 Then GoTo crc_8_loop

Print "byte b1%="Hex$((b1% And &hFF),2)"h  crc="Hex$(c1%,2)"h"


If n>1 Then b1%=(b1%>>8): n=n-1: GoTo crc_8            'break integer b1 into separate bytes

Return
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1667
Posted: 10:27pm 13 Apr 2020
Copy link to clipboard 
Print this post

This is the code I played about with a few years back.
Credits go to Jman...

I didn't have a lot of success as it seemed to only find up to 3 of the 6 DS18B20's on the bus.

I did find that removing them one at a time produced different result; as if certain number patterns caused it to drop out.

Spent a fair bit of time looking for answers, but in the end just went back to reading serial numbers one at a time.

  Quote  '1-Wire Serial Number Search - Multiple Devices Attached.
'Code by Jman 04 April 2016
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 =
16

'===================================================================
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),2);"H"
Print "ID:", Hex$(ROM_NO(1),2),Hex$(ROM_NO(2),2),Hex$(ROM_NO(3),2),Hex$(ROM_NO(4),2),Hex$(ROM_NO(5),2),Hex$(ROM_NO(6),2)
Print "CRC:", Hex$(ROM_NO(7),2)
Print
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
'===================================================================
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1667
Posted: 10:43pm 13 Apr 2020
Copy link to clipboard 
Print this post

  CaptainBoing said  It says... "The OWSEARCH command and the OWCRC8() and OWCRC16() functions are not implemented"


I've got a vague recollection that matherp revisited OWSearch at one point in the MMExtreme firmware.

On checking the function is mentioned on Page 29 of the 5.3.15 MM Extreme Manual I have here.

Have never got to try in on my MMX though.


 
helltek
Newbie

Joined: 04/02/2015
Location: United States
Posts: 29
Posted: 12:41am 14 Apr 2020
Copy link to clipboard 
Print this post

I really wonder if the Maxim Dallas 1-wire theory actually works. Someone would have program that works reliably. The Maxim App Note is inconsistent and has errors and typos. I feel like reading Chinese manual. If Maxim released updated App Note easy to follow and without errors, they could revive the 1-wire product line and sell lot of 1-wire devices to the IoT and SmartHome market.
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025