phil99
 Guru
 Joined: 11/02/2018 Location: AustraliaPosts: 3321 |
| Posted: 05:39am 03 Jun 2026 |
|
|
|
The latest PicoMite firmware - V6.03.00RC15 can now use strings and arrays in OneWire commands. Here is the previous program converted to use both. A more streamlined result and simpler to write.
' Running 2 DS18x20 sensors on the same pin. Get their ROM codes and replace those below. Dim PinNbr = MM.Info(pinno gp6) Dim integer n=0, ROMcode(1) = (2909716863767585976, 2900111825584602189)
Dim DStemp0 = DS18x20(PinNbr, ROMcode(0)) 'purge old data and start new conversion Pause 750 Dim DStemp1 = DS18x20(PinNbr, ROMcode(1)) 'purge old data and start new conversion
Do ' Main Loop D = DS18x20(PinNbr, ROMcode(n)) If D<>1000 Then Print "Device";n; D;"`C", If n=1 Then Print Pause 1000 n = Not n Loop
Function DS18x20(PinNbr As INTEGER, ROM As INTEGER) As FLOAT ' Adapted from DS18x20 test code by TassyJim ' This version for MMbasic v6.03.00RC15 with array and string support for OneWire Local INTEGER done, T(7), Fam = ROM >> 56 'get family code from ROM code Local ROMcode$ = Bin2str$(Uint64,ROM,big) 'convert ROM code to string DS18x20 = 1000' standard error code OneWire READ PinNbr, 4, 1, done' is conversion finished? If Not done Then Exit Function ' if not return to program
If ROM Then OneWire WRITE PinNbr, 1, 1, &h55 ' reset then match ROM Code command OneWire WRITE PinNbr, 0, 8, ROMcode$ ' send ROM Code to the DS18x20 OneWire WRITE PinNbr, 0, 1, &hBE ' read scratchpad command Else OneWire WRITE PinNbr,1,1,&h33 'If no ROM code given assume only one DS18x20 connected OneWire READ PinNbr, 0, 1, Fam ' read "family" ROM code OneWire WRITE PinNbr, 1, 2, &hCC, &hBE ' command read data EndIf OneWire READ PinNbr, 0, 8, T() ': Math V_Print T()' get the data
' process the data. Fam = Family ROM codes - 16=DS1820/DS18S20, 34=DS18S22, 40=DS18B20 If Fam = 16 Then Value = Str2bin(int16,Chr$(T(0))+Chr$(T(1))\2 -0.25+(T(7)-T(6))/T(7)) If Fam = 40 Or Fam = 34 Then Value = Str2bin(int16,Chr$(T(0))+Chr$(T(1)))/16 DS18x20 = Cint(Value * 100) / 100 'round to 1/100ths
' Start conversion, ready for next read If ROM Then OneWire WRITE PinNbr, 1, 1, &h55 'reset then match ROM Code command OneWire WRITE PinNbr, 0, 8, ROMcode$ 'send ROM Code OneWire WRITE PinNbr, 2, 1, &h44 'Master issues Convert Temp. command Else OneWire RESET PinNbr' ' reset before command OneWire WRITE PinNbr, 8, 2, &hCC, &h44' ' start conversion EndIf End Function End Output.Device 0 13.06`C Device 1 22.56`C Device 0 23`C Device 1 22.56`C Device 0 23.06`C Device 1 22.56`C Device 0 23.06`C Device 1 22.56`C Device 0 23.06`C Device 1 22.5`C
Reading the ROM codes is simpler too.
> clear : dim integer ROM, PinNo=mm.info(pinno GP6) > OneWire WRITE PinNo, 1, 1, &h33 : OneWire READ PinNo, 2, 8, RC$ > ROM = str2bin(Uint64,RC$,big) : ? ROM 2909716863767585976 > Edited 2026-06-03 23:01 by phil99 |