Having Trouble with Fahrenheit Conversion

neiltechspec

Senior Member
Using a HT21D i2c Humidity & Temp sensor, no probs with Humidity & Temp in degrees C.

But I can't seen to get my head around converting negative degrees C to degrees F.

Any help appreciated, relevant part of code is below (with below 0 remmed out).

Neil.

Code:
symbol temp12 = w7
symbol adjtemp = w8

HTUtemp:
	hi2cin $e3,(b15,b14) ;temperature measurement
	temp12 = temp12 & $FFFC ;mask out lower 2 bits of LSB (status bits)
	temp12 = 17572 ** temp12 - 4685
	tempf = temp12       ;for Fahrenheit conversion
	sign = 43	 ;"+"   

	if temp12 > 13000 then	;check for below 0                       
	 sign = 45  ;"-" 
	 temp12 = - temp12
	endif
    
	bintoascii temp12,b23,b24,b25,b26,b27

  	if b24 = 48 then 
	 serout ip_out,baud,("S Panel Temp:  ",sign,b25,".",b26,$b0,"C",cr,lf)
  	endif
  	
	if b24 > 48 then
	 serout ip_out,baud,("S Panel Temp: ",sign,b24,b25,".",b26,$b0,"C",cr,lf)
	endif

	;Fahrenheit Conversion
	
	if tempf <= 13000 then
	 adjtemp = tempf * 9 / 5 + 3200
	endif

	;if tempf > 13000 then
	 ;tempf = tempf - 13000
	 ;adjtemp = tempf * 9 / 5 - 3200
	;endif
	
	;bintoascii adjtemp,b23,b24,b25,b26,b27
	
	;serout ip_out,baud,("  ",b24,b25,".",b26,$b0,"F",cr,lf)
 

marks

Senior Member
Hi neiltechspec ,
I.ve order one of these to try out seeing there so cheap about $7.50 already on a small pcb
i rewrote this slightly to include Fahrenheit so youll just have to see if it goes
i tried posting in colour but even this little bit of code is to many characters for the forum lol.

Code:
;  HTU21D   Digital Relative Humidity sensor with Temperature output
;           Recommended supplyvoltage is 3VDC (regulated).
'#picaxe 18m2
SYMBOL SensorRH     = W0  SYMBOL RHmsb     = b1  SYMBOL RHlsb    = b0            
SYMBOL Temperature  = W1  SYMBOL TEMPmsb   = b3  SYMBOL TEMPLsb  = b2
SYMBOL FTemperature  = W2
SYMBOL Sign         = b6
SYMBOL D0           = b7
SYMBOL D1           = b8 
SYMBOL D2           = b9 
SYMBOL D3           = b10 
SYMBOL D4           = b11

Main:

Pause 20
hi2csetup i2cmaster,0x80,i2cslow_8,i2cbyte                            ' %10000000 (hex 0x80) is device i2c bus address 
hi2cin 0xE3, (TEMPmsb,TEMPLsb) : TEMPLsb = TEMPLsb AND 0xFC           ' Last bits of LSBdata ,
hi2cin 0xE5, (RHmsb  ,RHlsb  ) : RHlsb   = RHlsb   AND 0xFC           '  must beset to ‘0’ before calculating 
pause 20
  
Calculate_FTemp:                                                      
               FTemperature = Temperature **31630 -5233               ' -40.00 to 257°F 14bit                                
               Sign = " "                                             ' Display +   
      IF FTemperature > 26000 THEN                       
        Sign = "-"                                                    ' Display - 
        FTemperature = -FTemperature                                    
      ENDIF 
           BinTOASCII FTemperature,D4,D3,D2,D1,D0 
                   IF  D4 = "0" THEN : D4 = " "                       ' leading zero blanking 
                    IF D3 = "0" THEN : D3 = " " : ENDIF               ' leading zero blanking
                   ENDIF                                                                                                             
           sertxd (CR, LF, "  Temperature  ",Sign,D4,D3,D2,".",D1,D0,"'F  ")           		 
		 		 
 Calculate_Temp:                                                      ; TempActual = TempSensor /65536 x175.72 -46.85
               Temperature = Temperature **17572 -4685                ' -40.00 to 125°C 14bit                                
               Sign = " "                                             ' Display +   
      IF Temperature > 13000 THEN                       
        Sign = "-"                                                    ' Display - 
        Temperature = -Temperature                                    
      ENDIF 
           BinTOASCII Temperature,D4,D3,D2,D1,D0 
                   IF  D4 = "0" THEN : D4 = " "                       ' leading zero blanking 
                    IF D3 = "0" THEN : D3 = " " : ENDIF               ' leading zero blanking
                   ENDIF                                                                                                             
           sertxd (CR, LF, "  Temperature  ",Sign,D4,D3,D2,".",D1,D0,"'C")           
            
 RH_Compensated:          
               Temperature =Temperature /10                           ; RHcompensated = RHactual + (25 -TempActual) x -0.1
           IF Sign = "-" THEN : Temperature = -Temperature : ENDIF    ' improves RHaccuracy over 0.00 to 80.00°C
                                                                      
 Calculate_RH:                                                        ; RHactual = RHsensor /65536 x125 -6
            SensorRH = SensorRH **12500 -600 + Temperature -250                         ' -6.00 to 118 Rh% 12bit
            Sign = " "                                                ' Display +
      IF SensorRH > 13000 THEN                       
        Sign = "-"                                                    ' Display - 
        SensorRH = -SensorRH                                    
      ENDIF              
                  
          BinTOASCII SensorRH,D4,D3,D2,D1,D0 
                  IF  D4 = "0" THEN : D4 = " "                       ' leading zero blanking 
                   IF D3 = "0" THEN : D3 = " " : ENDIF               ' leading zero blanking  
                  ENDIF
            sertxd (CR, LF, "    RHcompensated    ",sign,D4,D3,D2,".",D1,D0,"%")   
                              
Pause 2000
goto main
 

neiltechspec

Senior Member
Extracted the relevant part from your code & placed into mine.

Now reads correctly - thanks.

Neil.

edit: only checked on +ve C temperature.

hmmm, not so sure now I have thought about it.
From your code -40 to +257 deg F ???? don't think -40 F is possible !

I'll have to wait until next time it's freezin and see what happens.

Neil.
 
Last edited:
Top