![]() |
Forum Index : Microcontroller and PC projects : Help with 1-Wire Temp Readings.
Page 1 of 2 ![]() ![]() |
|||||
Author | Message | ||||
Phil23 Guru ![]() Joined: 27/03/2016 Location: AustraliaPosts: 1667 |
Wondering if someone can help me with my 1-Wire temp reading function, (Again). What I want to do is split into 2 Functions, so I can start the conversion, & not wait around for it to complete. Much like TEMPR START. 4 Sensors is bogging my main loop a bit & think this would be a better approach. The 5th sensor will only make it worse... Something like this is what I have in mind[Code] Function InitTemp(TsSerial As Integer) 'Starts the conversion on the DS18B20 .. .. .. End Function Function ReadTemp(TsSerial As Integer) As Float 'Reads the DS18B20 .. .. .. End Function[/code] I have this function working & partially understand it. This line sends a reset before the command, matches the ROM (&H55) & starts the conversion (&H44). [Code]OneWire Write PinTmp, 1, 10, &H55,&H28,Ts1,Ts2,Ts3,Ts4,Ts5,Ts6,Ts7,&H44[/code] What does &H28 do? This reads the scratchpad, a bit instead of a byte,[Code]OneWire Read PinTmp, 4, 1, Status[/code]presume that's the bit that indicated the conversion status. This line sends a reset before the command, matches the ROM (&H55) & issues read the scratchpad (&HBE). [Code]OneWire Write PinTmp, 1, 10, &H55,&H28,Ts1,Ts2,Ts3,Ts4,Ts5,Ts6,Ts7,&HBE[/code] I've tried to make starts on nutting it all out, interruptions, constantly put that to an end. Read the data sheet a dozen times. I can't see that the precision is being set; I presume it's using the default. Would like to incorporate that too. Read also that the Skip Rom command (CCh), will start conversions on all Sensors & wonder if that can be incorporated when making it 2 separate functions. Doesn't seem it would be necessary to address each sensor when starting the conversion if I can start them all with one command. Close to out of my depth here, & hoping to others it will be just plain English. Thanks Phil. Here's all the relevant code I'm using. [Code] Dim Integer Ts1,Ts2,Ts3,Ts4,Ts5,Ts6,Ts7 Dim Integer LTemp,HTemp,Status Dim Float TmpAmb,TmpCur,TmpInp,TmpOut 'Pins used - Inputs Dim Integer PinTmp=22 '1-Wire DS18B20 Input Pin ' Real Sensor Serial Numbers Dim Integer TsAmb=&HFFA667A815016F 'Sensor0 Ambient Air Temp Dim Integer TsCur=&H74413C0700005E 'Sensor1 Water Temp Dim Integer TsInp=&HBF4C3B07000053 'Sensor2 Panel Input Temp Dim Integer TsOut=&H901C3B07000054 'Sensor3 Panel Output Temp Do Watchdog 15000 ' Get the temperatures at timer interval using Multi device read routine if timer>5000 then Pin(PinLed1)=1 'Hold Status LED on whilst reading temps TmpAmb=ReadTemp(TsAmb) TmpCur=ReadTemp(TsCur) TmpInp=ReadTemp(TsInp) TmpOut=ReadTemp(TsOut) Timer=0 Endif Loop '========================OneWire Temperature Function========================= ' Get the temperatures from DS18B20 - multiple probes on a single pin '============================================================================= Function ReadTemp(TsSerial As Integer) As Float Ts1=(TsSerial >> 48) and &hFF : Ts2=(TsSerial >> 40) and &hFF : Ts3=(TsSerial >> 32) and &hFF Ts4=(TsSerial >> 24) and &hFF : Ts5=(TsSerial >> 16) and &hFF : Ts6=(TsSerial >> 8) and &hFF : Ts7=(TsSerial) and &hFF OneWire Write PinTmp, 1, 10, &H55,&H28,Ts1,Ts2,Ts3,Ts4,Ts5,Ts6,Ts7,&H44 'Check sensor is ready Timer=0 : Status=0 Do If Timer > 1000 Then Error "Sensor Error" OneWire Read PinTmp, 4, 1, Status ' Conversion status Loop Until Status = 1 OneWire Write PinTmp, 1, 10, &H55,&H28,Ts1,Ts2,Ts3,Ts4,Ts5,Ts6,Ts7,&HBE OneWire Read PinTmp, 2, 2, LTemp, HTemp 'Calculate the Temp in C ReadTemp=((HTemp And &b111) * 256 + LTemp) / 16 If HTemp And &b1000 Then ReadTemp=-ReadTemp ' adjust if negative End Function [/code] |
||||
mikeb![]() Senior Member ![]() Joined: 10/04/2016 Location: AustraliaPosts: 174 |
Hi Phil, Page 70 of the Micromite User Manual explains how to change the precision measurement of the DS18B20. Note that the highest precision requires approx 750 msecs for conversion. Use STR$ formatting (padding) to display correctly. Also can you use a '°' symbol instead of Deg ? Might be in one of the fonts but failing that draw a little circle. Is there a 'Cfunction' available ? 'alt 248' for windows (and LCD's) and 'alt 176' for a VT100 terminal. You've got 8 core data cable. Use another (bigger) MM. ![]() ![]() ![]() Mike B. There are 10 kinds of people in the world. Those that understand binary and those that don't. |
||||
Phil23 Guru ![]() Joined: 27/03/2016 Location: AustraliaPosts: 1667 |
Searched for some 12 core, Lol. Found lot's of bits & pieces, even some 16, but only a few metres at a time. Stuff connected on the roof will be 4 relays. Temp & Solar Sensor, few limits in the ball valves. Numbers are adding up fast... CHeers. |
||||
mikeb![]() Senior Member ![]() Joined: 10/04/2016 Location: AustraliaPosts: 174 |
Hi Phil, Have uploded my datasheet so page references are correct. To change resolution write to 'scratch pad memory' at address 4Eh (configuration register). See page 12. You must write 3 bytes. The first two bytes (high\low alarm spt) are irrelevant with the 3rd (bits 5 and 6) being resolution. See page 9. Once each device is configured you can then just issue a global 'Convert T' command (44h). All devices on the bus will begin a conversion. You will then need to issue the 'Match ROM' command to read data into the correct variable. 2016-06-20_232945_DS18B20.pdf There are 10 kinds of people in the world. Those that understand binary and those that don't. |
||||
jman![]() Guru ![]() Joined: 12/06/2011 Location: New ZealandPosts: 711 |
Hi This is part of the Id and H28 indicates this is a temperature sensor See this page for a list of the family types 1Wire Family Types From the data sheet the conversion times are listed below RESOLUTION(BITS) MAX CONVERSION TIME 9 93.75ms 10 187.5ms 11 375ms 12 750ms The below will set the resolution 7f=12 bit 5f=11 bit 3f=10 bit 1f=9 bit Onewire WRIte PinTmp, 2, 5, &HCC, &H4E,&H00,&H00,&H7F 'set the resolution To save time and start the conversion on all the sensors try this OneWire Write PinTmp, 1, 2, &HCC, &H44 'start conversion on all sensors Then read each one to get the results Regards Jman |
||||
robert.rozee Guru ![]() Joined: 31/12/2012 Location: New ZealandPosts: 2442 |
a question: will the command TEMPR START pin [, precision] successfully start the conversion on multiple DS18B20 sensors that are all attached to a single pin? if so, you then just need to wait the prescribed time (100, 200, 400, 800ms) and issue multiple ONEWIRE commands for each sensor to be read. admitedly this is an unholy mixture of TEMPR and ONEWIRE, but if it works and makes life a little simpler... cheers, rob :-) |
||||
Geoffg![]() Guru ![]() Joined: 06/06/2011 Location: AustraliaPosts: 3292 |
Sorry, no. It will only work with one sensor at a time. Geoff Geoff Graham - http://geoffg.net |
||||
robert.rozee Guru ![]() Joined: 31/12/2012 Location: New ZealandPosts: 2442 |
looking at the source to void Init_ds18b20(int pin, int precision) in Onewire.C there doesn't seem (at a quick glance) to be anything that obviously precludes it starting the conversion on multiple sensors, with the possible exception of ds18b20Timer being set to a value that might preclude a second call to it (i couldn't quite follow the logic at 3:30am!) btw, does the line 'PinSetBit(pin, ODCCLR); // set strong pullup' mean that an external pullup is optional on the DS18B20's pin? the TEMPR( pin ) function would certainly fail with multiple sensors, but then with multiple sensors attached this function would never be called. phil: i do see that the datasheet linked to by mikeb does state that it is allowable to start up the conversion on multiple sensors at once ("For example, the master can make all DS18B20s on the bus perform simultaneous temperature conversions by issuing a Skip ROM command followed by a Convert T [44h] command."). cheers, rob :-) |
||||
Phil23 Guru ![]() Joined: 27/03/2016 Location: AustraliaPosts: 1667 |
Thanks all, That seems to all make sense. [Code] '========================OneWire Temperature Functions========================= ' Sub Initialises all DS18B20 Sensors on the Bus 'The below will set the resolution 'Onewire Write PinTmp, 2, 5, &HCC, &H4E,&H00,&H00,&HXX ' 9 bit=0.5 :1f - 93.75ms '10 bit=0.250 :3f - 187.5ms '11 bit=0.125 :5f - 375.0ms '12 bit=0.0625:7f - 750.0ms ' 'Function Returns the Temperature for the Serial# passed. '============================================================================= Sub InitTemp Onewire WRIte PinTmp,2,5,&HCC,&H4E,&H00,&H00,&H7F 'Set the Resolution of all Sensors OneWire Write PinTmp,1,2,&HCC,&H44 'Start Conversion on all Sensors End Sub Function ReadTemp(TsSerial As Integer) As Float Ts1=(TsSerial >> 48) and &hFF : Ts2=(TsSerial >> 40) and &hFF : Ts3=(TsSerial >> 32) and &hFF Ts4=(TsSerial >> 24) and &hFF : Ts5=(TsSerial >> 16) and &hFF : Ts6=(TsSerial >> 8) and &hFF : Ts7=(TsSerial) and &hFF 'Check sensor is ready Timer=0 : Status=0 Do If Timer>1000 Then Error "Sensor Error" OneWire Read PinTmp,4,1,Status ' Conversion status Loop Until Status=1 OneWire Write PinTmp,1,10,&H55,&H28,Ts1,Ts2,Ts3,Ts4,Ts5,Ts6,Ts7,&HBE OneWire Read PinTmp,2,2,LTemp,HTemp 'Calculate the Temp in C ReadTemp=((HTemp And &b111)*256+LTemp)/16 If HTemp And &b1000 Then ReadTemp=-ReadTemp 'Adjust if negative End Function [/code] Any Suggestions on improvements Welcome. Cheers Phil. Out of curiosity, Page 90 of the manual; [Code] flag - A combination of the following options: 1 - Send reset before command 2 - Send reset after command [/code] What exactly does the "Reset" do, clear, etc. Obviously not everything, otherwise you'd start a conversion then reset the whole device & achieve nothing.... |
||||
robert.rozee Guru ![]() Joined: 31/12/2012 Location: New ZealandPosts: 2442 |
i'm not too sure about this bit of code: Timer=0 : Status=0
Do If Timer>1000 Then Error "Sensor Error" OneWire Read PinTmp,4,1,Status ' Conversion status Loop Until Status=1 i think it may only be valid if you have a single sensor attached and/or parasitic power is not used. if there are multiple sensors attached, there seems a possibility for conflicts. according to the datasheet, "In parasite power mode this notification technique cannot be used since the bus is pulled high by the strong pullup during the conversion.", while with multiple (non-parasitic powered) sensors every single one will need to become ready before the loop will exit (which may or may not be what is desired). a far safer approach may be to set timer = 0 in your initialize routine, then in your read routine have a delay loop, do loop until timer > n where n is 100, 200, 400, or 800 as appropriate. [all the above is based ENTIRELY upon reading the datasheet, no code was tried and no sensors were sacrificed] cheers, rob :-) |
||||
Phil23 Guru ![]() Joined: 27/03/2016 Location: AustraliaPosts: 1667 |
Did wonder about that, as there is no serial number reference. Not sure if the others would be ignoring this read because of previous commands written. Not using Parasitic Power. The bus is on 5V. If anyone wants a look at what they can see on the live board, I've got the ESP's IP address forwarded to a random external port. Don't mind if someone would like to Telnet to it & take a look. Cheers Phil |
||||
Phil23 Guru ![]() Joined: 27/03/2016 Location: AustraliaPosts: 1667 |
Seen to have an unusual sensor failure this morning:- [code] Water Temperature = 14.50 Target Temperature = 35.00 Input Temperature = 8.06 Output Temperature = 10.69 Temperature Difference = 2.63 Temp Difference Setting = 0.50 Solar Panel Voltage (mV)= 0.00 Solar Threshold Voltage (mV)= 450.00 Solar Minimum Voltage (mV)= 100.00 Air Temperature = 1.44 Panel Temperature = -125.81 Panel Temp Differential = 10.00 Time is 06:29:49 Pump is Stopped Pump Run Time is 00:00:00 [/code] Can't seeing it being anything other than a faulty sensor. A completely disconnected one returns -127.9. The value returned is changing, but way out with Pluto. Can't see code relating at all; was working perfectly last night. Any other suggestions? They are all Star connected at a piece of strip board, 5V powered & 4.7k pull-up. Ambient is on a metre of cable. Three water temps on 3 metres. Panel temp is at the end of about 12 metres. Pull-up is close to the micro. Will check the connections up there & can try a different sensor; But not at 6:30 in the morning........ Cheers Phil |
||||
Phil23 Guru ![]() Joined: 27/03/2016 Location: AustraliaPosts: 1667 |
|
||||
mikeb![]() Senior Member ![]() Joined: 10/04/2016 Location: AustraliaPosts: 174 |
Hi Phil, You may notice from the release notes (ver 5.2) that Geoff has given us a degree (°) symbol, in the standard font. Can you use that instead of Deg ? ![]() Well done on the progress of your project. There are 10 kinds of people in the world. Those that understand binary and those that don't. |
||||
Phil23 Guru ![]() Joined: 27/03/2016 Location: AustraliaPosts: 1667 |
Hi Mike, I did read that in the release notes & it got my attention strait away. You might have noticed that more than not often I use ° in posts. You don't happen to know the character code off the top of your head? Phil. |
||||
mikeb![]() Senior Member ![]() Joined: 10/04/2016 Location: AustraliaPosts: 174 |
Hi Phil, I'm wondering if there is a 'bug' in the embedded Cfunction for the DS18B20. The output code of the device is 2's complement, from memory, and it is being returned into a signed variable. If it returned a minus value then I would've thought that is what you would get. You shouldn't have to offset or 'AND' anything. Geoff, Matherp, anyone ? Not really sure. Haven't tried it yet. Had to use dec 176 for output to TeraTerm. 'alt' 248 generally works in Windows. I usually use a 'brute force' attack, when using different display devices, to get what I want as follows - Use a 'for \ next' test loop, incrementing the code at some readable time period (500 msec maybe, or use a pushbutton input). The key is speed versus readability. Output the code as readable text, at the same time, so you can determine the correct decimal number for any given character. Hey.....works for me ![]() Mike B. There are 10 kinds of people in the world. Those that understand binary and those that don't. |
||||
Phil23 Guru ![]() Joined: 27/03/2016 Location: AustraliaPosts: 1667 |
Can't be a Bug as my temperature is being derived from the two bytes read directly from the 1-Wire bus, so the firmware's internal DS18B' code is totally irrelevant. Bits 11-15 are Sign bits, Bits 0-10 are the data, so if 1111 1111 1111 1000 is -0.5°C, (from the data sheet) then the data bits must represent 127.5. So that would confirm that it needs 128 subtracted from it. Cheers |
||||
mikeb![]() Senior Member ![]() Joined: 10/04/2016 Location: AustraliaPosts: 174 |
hi Phil, Looks like an XOR is needed at negative temps. Get back to you. There are 10 kinds of people in the world. Those that understand binary and those that don't. |
||||
Phil23 Guru ![]() Joined: 27/03/2016 Location: AustraliaPosts: 1667 |
Hi Mike, Have a look on page 6 of your data sheet. If you do the binary conversion for say -0.5°C in table one. It converts to 127.5. Cheers. |
||||
mikeb![]() Senior Member ![]() Joined: 10/04/2016 Location: AustraliaPosts: 174 |
Hi Phil, I wrongly assumed you were using the 'built in' Cfunction. Assuming DS18B20 setup for 12 bits - Carry out an XOR (flip the bits) on the 12 bits, or whatever resolution you used when configuring the conversion command (12 bits by default), add 1, and multiply that by 0.0625 (for 12 bits). Works for me. There are 10 kinds of people in the world. Those that understand binary and those that don't. |
||||
Page 1 of 2 ![]() ![]() |
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |