phil99
 Guru
 Joined: 11/02/2018 Location: AustraliaPosts: 3013 |
| Posted: 12:45am 10 Mar 2025 |
|
|
|
TassyJim's test program with some mods for the PicoMite.
'DS18x20 test code by TassyJim ' Input "Type Pin number ",PinNbr Input "Type PicoMite GPxx number for the DS18B20 (include the GP) "; GPPin$ Dim INTEGER PinNbr = MM.Info(pinno GPPin$)
Print Print Print "Family name: ";FamilyName$(PinNbr) Print "Rom Code: ";getRomCode$(PinNbr) Print "ScratchPad: ";getScratchpad$(PinNbr)
If powerMode(PinNbr) Then Print "Power mode External" Else Print "Power mode Parasitic" EndIf
Print For n = 9 To 12 Print x = setResolution(PinNbr, n) Print "ScratchPad: ";getScratchpad$(PinNbr) Timer = 0 Print "Temperature: ";getTemp(PinNbr);" ";Timer;"mS,";n;" bit" Next n
Print : Print "Tempr() without start", TEMPR(PinNbr) TEMPR START PinNbr,0 : Print "Tempr Start Pin,0", TEMPR(PinNbr) TEMPR START PinNbr,1 : Print "Tempr Start Pin,1", TEMPR(PinNbr) TEMPR START PinNbr,2 : Print "Tempr Start Pin,2", TEMPR(PinNbr) TEMPR START PinNbr,3 : Print "Tempr Start Pin,3", TEMPR(PinNbr) End
Function getRomCode$(PinNbr As INTEGER) ' get ROM Code - useful if you want more than 1 device on wire Local INTEGER a1,a2,a3,a4,a5,a6,a7,a8 OneWire WRITE PinNbr,1,1,&h33 'read ROM code OneWire READ PinNbr,0,8,a1,a2,a3,a4,a5,a6,a7,a8 getRomCode$ = Hex$(a1,2)+" "+Hex$(a2,2)+" "+Hex$(a3,2)+" "+Hex$(a4,2)+" "+Hex$(a5,2)+" "+Hex$(a6,2)+" "+Hex$(a7,2)+" "+Hex$(a8 ,2) End Function
Function getScratchpad$(PinNbr As INTEGER) 'ONEWIRE Write PinNbr,1,9,&h55,a1,a2,a3,a4,a5,a6,a7,a8 'read from scratchpad OneWire WRITE PinNbr,1,2,&hcc,&hbe OneWire READ PinNbr,0,8,a,b,c,d,e,f,g,h getScratchpad$ = Hex$(a,2)+" "+Hex$(b,2)+" "+Hex$(c,2)+" "+Hex$(d,2)+" "+Hex$(e,2)+" "+Hex$(f,2)+" "+Hex$(g,2)+" "+Hex$(h,2) End Function
Function powerMode(PinNbr As INTEGER) As INTEGER ' check whether power external (1) or parasitic (0) OneWire RESET PinNbr OneWire WRITE PinNbr, 1,2,&hcc, &hb4 OneWire READ PinNbr,4,1,powerMode End Function
Function getFamily(PinNbr As INTEGER) As INTEGER getFamily = Val("&h"+Left$(getRomCode$(PinNbr),2)) End Function
Function FamilyName$(PinNbr As INTEGER) Local INTEGER fn fn = getFamily(PinNbr) Select Case fn Case 16 FamilyName$ = "DS1820/DS18S20" Case 34 FamilyName$ = "DS18S22" Case 40 FamilyName$ = "DS18B20" Case Else FamilyName$ = "Unknown" End Select End Function
Function setResolution(PinNbr As INTEGER,r As INTEGER) r = r-9 If r < 0 Then r = 0 If r > 3 Then r = 3 r = r*32+31 OneWire WRITE PinNbr, 1,5,&hcc, &h4E, &hFF,&hFF,r End Function
Function getTemp(PinNbr As INTEGER) As FLOAT Local INTEGER fn, t, T1, T2, a,b,c,d,e,f,g,h Local FLOAT Value fn = getFamily(PinNbr) power = powerMode(PinNbr) OneWire RESET PinNbr ' reset before command OneWire WRITE PinNbr, 8, 2, &hcc, &h44 ' start conversion
'read external when b goes high, for parasitic just wait If power = 0 Then Pause 750 Else t = Timer Do If Timer - t > 1000 Then Value = 1000 Exit Do EndIf OneWire READ PinNbr, 4 , 1 , b ' conversion done? Loop Until b = 1 EndIf If Value = 0 Then ' we have not timed out yet OneWire WRITE PinNbr, 1, 2, &hcc, &hbe ' command read data OneWire READ PinNbr, 2, 2, T1, T2 ' get the data OneWire RESET PinNbr Select Case fn Case 16 'DS18S20 or DS1820 OneWire WRITE PinNbr,1,2,&hcc,&hbe OneWire READ PinNbr,0,8,a,b,c,d,e,f,g,h If T2 And &h80 Then 'if MSB of T2=1 then negative 'Read 12bit resolution and adjust 'truncate 0.5deg value (or use integer division \) T1 = T1 And &hFE T1=T1 / 2 'make whole degrees 'add compensation values read from scratchpad T1=T1*16 'make lsb 1/16 degree T1 = T1 -4 +(16 - g) 'add 12 bit value 'take 2s complement T1 = (T1 Xor &hFF) + 1 Value = -T1/16 'make decimal value in degrees Else 'positive temp Value = T1 / 2 '9bit value 'Read 12bit resolution and adjust T1 = T1 And &hFE 'truncate 0.5deg(or use integer division \) Value = T1/2- 0.25 + (16 - g) /16 '12 bit value EndIf Case 34, 40 ' DS18S22 or DS18B20 If T2 And &b1000 Then 'negative temp 'make 2s complement (1s complement+1) T2 = (T2 Xor &hFF) T1 = (T1 Xor &hFF)+1 If T1=1 Then T2=T2+1 'add the carry if required Value = -((T2 And &b111) * 256 + T1) / 16 Else 'positive temp Value = ((T2 And &b111) * 256 + T1) / 16 EndIf Case Else Value = 1000 End Select EndIf getTemp= Value End Function And the output with my non-genuine units.
> RUN Type PicoMite GPxx gp6
Family name: Unknown Rom Code: 29 61 64 12 3D D4 A4 B8 ScratchPad: 7B 01 FF FF 7B FF FF FF Power mode External
ScratchPad: FF FF FF FF FF FF FF FF Temperature: 1000 17.838mS, 9 bit
ScratchPad: FF FF FF FF FF FF FF FF Temperature: 1000 17.851mS, 10 bit
ScratchPad: BD 80 FF FF AF FF FF FF Temperature: 1000 17.827mS, 11 bit
ScratchPad: 7B 01 FF FF 7F FF FF FF Temperature: 1000 17.815mS, 12 bit
Tempr() without start 23.625 Tempr Start Pin,0 23.625 Tempr Start Pin,1 23.625 Tempr Start Pin,2 23.375 Tempr Start Pin,3 23.375 > RUN Type PicoMite GPxx gp7
Family name: DS18B20 Rom Code: 28 3F 44 57 04 E1 3C 4D ScratchPad: 80 01 00 00 7F A5 A5 66 Power mode External
ScratchPad: 80 01 FF FF 1F A5 A5 66 Temperature: 22.5 495.694mS, 9 bit
ScratchPad: 68 01 FF FF 3F A5 A5 66 Temperature: 22.75 495.831mS, 10 bit
ScratchPad: 6C 01 FF FF 5F A5 A5 66 Temperature: 22.75 495.681mS, 11 bit
ScratchPad: 6C 01 FF FF 7F A5 A5 66 Temperature: 22.8125 495.811mS, 12 bit
Tempr() without start 1000 Tempr Start Pin,0 1000 Tempr Start Pin,1 22.75 Tempr Start Pin,2 1000 Tempr Start Pin,3 22.8125 > Edited 2025-03-10 10:50 by phil99 |