![]() |
Forum Index : Microcontroller and PC projects : Help with 1-Wire Temp Readings.
![]() ![]() |
|||||
Author | Message | ||||
mikeb![]() Senior Member ![]() Joined: 10/04/2016 Location: AustraliaPosts: 174 |
@me Edit The above is for working with a negative output code (msb's set). Return the sign back to the result. 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 |
Still a bit lost here & still using the Bandaid code mentioned ubove. What exactly do I XOR it against? For example this morning one sensor read:- [Code] HTemp=&b11111111 LTemp=&b11001100 [/code] Using a 12 bit conversion. Thanks. |
||||
mikeb![]() Senior Member ![]() Joined: 10/04/2016 Location: AustraliaPosts: 174 |
Looks to me like it got to -3.25°C. Note that the following is for negative temps as indicated by the 'sign' bits being set. Combine your HT & LT variables to make a 16 bit value. b11111111 11001100 (remove the space). As you want to flip ALL bits XOR with b11111111 11111111 (remove space). You will end up with b110011 (51 decimal). Add 1 to allow for the rollover from +0°C to -0°C. 52 x 0.00625°C (12 bit resolution) = 3.25°C Return minus sign back to value = -3.25°C. Try it with the examples in the datasheet I gave you. Works for me. There are 10 kinds of people in the world. Those that understand binary and those that don't. |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
If all you want is the answer without too much understanding, try this code HTemp=&b11111111 LTemp=&b11001100 tempRaw = HTemp*256 + LTemp print "&H"hex$(tempRaw) if tempRaw > 64512 then temperature = (tempRaw- 65536 )/16 else temperature = tempRaw/16 endif print temperature Test it with all the examples for the data sheet Jim VK7JH MMedit |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
I am not sure if HTemp=&b11111100
LTemp=&b00000000 is a valid responce but just in case, we need to test for it and return zero (or -64?) HTemp=&b11111100 LTemp=&b00000000 tempRaw = HTemp*256 + LTemp print "&H"+hex$(tempRaw)+" "+str$( tempRaw) if tempRaw > 64512 then temperature = (tempRaw- 65536 )/16 elseif tempRaw = 64512 then temperature = 0 else temperature = tempRaw/16 endif print temperature Jim VK7JH MMedit |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
My last attempt was rather clunky so I will try and do better this time. two's compliment is a pain to follow but is the only way to go if you are a small processor with limited maths capability. It is easy to add in binary but not so easy to subtract. Instead if 5 minus 3, little processors find it easier to do 5 plus (-3) etc. As we have a higher level language to use (MMBasic) we can do things in a more human way. Most little devices we want to talk to use two's complement so we need a simple way to convert the numbers to signed integers. In the case of the DS18B20, we retrieve two 8 bit numbers and combine them to end up with a 16 bit number. The data sheet talks about 5 sign bits but in reality there is only one and the other bits will be high for negative values because we never measure down low enough. We then convert that 16 bit number into a signed number and then multiply it by the scaling factor to get the temperature. Rather than multiply by 0.0625, I have divided by 16 (1/16 = 0.0625) The signed16 function is generic and can be used anywhere, not just for the DS18B20 code. HTemp=&b00000111 : LTemp=&b11010000 ' +125 temperature = signed16(HTemp*256 + LTemp)/16 PRINT temperature HTemp=&b00000000 : LTemp=&b10100010 ' +10.125 temperature = signed16(HTemp*256 + LTemp)/16 PRINT temperature HTemp=&b00000000 : LTemp=&b00001000 ' +0.5 temperature = signed16(HTemp*256 + LTemp)/16 PRINT temperature HTemp=&b00000000 : LTemp=&b00000000 ' 0 temperature = signed16(HTemp*256 + LTemp)/16 PRINT temperature HTemp=&b11111111 : LTemp=&b11111000 ' -0.5 temperature = signed16(HTemp*256 + LTemp)/16 PRINT temperature HTemp=&b11111111 : LTemp=&b01011110 ' -10.125 temperature = signed16(HTemp*256 + LTemp)/16 PRINT temperature HTemp=&b11111100 : LTemp=&b10010000 ' -55 temperature = signed16(HTemp*256 + LTemp)/16 PRINT temperature HTemp=&b11111111 : LTemp=&b11001100 ' -3.25 temperature = signed16(HTemp*256 + LTemp)/16 PRINT temperature END FUNCTION signed16(x) IF x > 32767 THEN signed16 = x - 65536 ELSE signed16 = x ENDIF END FUNCTION XORing etc is the 'correct' way to look at it but the above function does the same maths in a more readable form (for Basic programmers) Jim VK7JH MMedit |
||||
mikeb![]() Senior Member ![]() Joined: 10/04/2016 Location: AustraliaPosts: 174 |
@Jim Nice explanation Jim. Thanks for that. I've only ever dealt with 8 bit micro's, in the past, using PICbasicPro. The lack of native floating point maths, and its inability to support the PIC32 range, convinced me to 'bite the bullet'. So glad I did. @Phill Hope you're exercising the appropriate amount of caution on that frosty roof. Did you get any joy out of the 3483's I sent you ? It's a shame that Microchip didn't check the available market RS-485 transceiver datasheets before they locked the logic sense of the DE pin into silicon. A register, to select the sense, would have been nice. I suppose a digital transistor inverter, and pullup resistor, isn't that much of a hassle at the end of the day. 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 |
Thanks all, Finally looking at this again. Does this look right for flipping the bits? Haven't tried the other suggestion yet. [Code] 'Calculate the Temp in C ReadTemp=((HTemp And &b111) * 256 + LTemp) / 16 If HTemp And &b1000 Then ReadTemp=-ReadTemp 'Adjust if negative - Returns wrong values 'If HTemp And &b1000 Then ReadTemp=(ReadTemp-128) 'Adjust if negative - Works Basically If HTemp And &b1000 Then 'Could also be &b11111000 or either &h8 of &F8 ReadTemp=((HTemp Xor &b11111111)*256+((&b11111111 Xor LTemp)+1))/-16 'Or use *hFF or 255 [/code] Phil. |
||||
![]() ![]() |
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |