Yes, I could see that in the original code, the first debug was there to display one more digit for temp>99.The first IF condition in the original displays 1 more digit prior to the decimal point.
That is why I thought that I could remove the second "TempF = TempF + 3200".
But it appears that I need to keep both of them in the code to get the correct temperature conversion??
Or am I missing something?
My goal is to have a section of code that will return degrees F for at least a range of say -20 F to +120 F (-30 to +50 C) I dont care about fractional digits. I am also trying to keep it small, as I am running out of room in my PIC. I don't need extreme accuracy +- 1 deg or so is ok.
Here is my(Bruce's) complete subroutine which seems to work correctly:
But It seems that I still need both instances of... TempF = TempF + 3200Code:'===========subroutine========================== GetTemp: OWOUT comm_pin, 1, [$CC, $4E, 0, 0, DS18B20_9bit] 'set resolution 9 bit Start_Convert: OWOUT Comm_Pin, 1, [$CC, $44] 'Skip ROM search & do temp conversion Wait_Up: OWIN Comm_Pin, 4, [Busy] ' Read busy-bit IF Busy = 0 THEN Wait_Up ' Still busy..?, Wait_Up..! OWOUT Comm_Pin, 1, [$CC, $BE] OWIN Comm_Pin, 2, [R_Temp.Lowbyte, R_Temp.Highbyte] ' Read two bytes GOSUB Convert_Temp RETURN '================================================================================ Convert_Temp: ' +32.0 to +257 F IF Cold_Bit = Real_Cold THEN Yikes ' If Cold_Bit = 1, it's below "0" deg C Sign = "+" Dummy = 1125 * R_Temp TempF = DIV32 100 IF TempF >6795 THEN ' Over 99.5 deg F..? TempF = TempF + 3200 ELSE TempF = TempF + 3200 ENDIF tempf=tempf/100 'keep only whole digits RETURN '----------------------------------------------------------------------------- Yikes: ' Display full range -C to -F conversion Sign = "-" ' Display - symbol for negative temp TempF = ~R_Temp / 16 ' Begin conversion from -C to deg +/-F IF TempF >=18 THEN ' Check for -degrees F "-18 C = -0.4 F" TempF = ((((TempF + 50) * 9) /5) -122) ' -C to -F below -17 deg C ELSE ' Else result = +deg F TempF = ((((-TempF + 50) * 9) /5) -58) ENDIF RETURN '==================================================
and that is what is puzzling me.
Isn't it the case that only one or the other will be executed? Yet if I remove one of them then I get the wrong temperature??
Possibly(likely) I need to go back and do some more testing.
Thanks




Bookmarks