asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
FAST F2C C2F temperature converts - no floats - no division |
Posted: Fri Apr 26, 2013 2:00 pm |
|
|
Code: |
fast temperature conversion no float no division
/*
fast computed , "good enough accuracy" temperature conversion:
Temperatures in the range of 32.0 deg F , Upwards to 6553.5 deg F
ie: 0 deg C to 3623.0 C
the temperature in this system is represented as
10X the value of degrees and tenths so 50.0 deg becomes 500 etc
You will need to adjust the print formatting for the high temp end of
this range
much faster than floating point
*/
// conversion of 10x temps
// IN: deg F10x OUT: deg C10X
// residual output error at 986 input +.03%
unsigned int16 DegFtoC(unsigned int16 degf_in){
unsigned int32 t1;
t1= (unsigned int32 ) (degf_in-320);
t1 *=569;
return (t1>>10); // effective /1024
}
// IN: deg C10x OUT: deg F10X
// residual output error at 370 input -.01%
unsigned int16 DegCtoF(unsigned int16 degC_in){
unsigned int32 t1=1843;
t1 *= degC_in;
t1 >>=10; // divide 1024
return ( t1+320); // add C-F offset in our units
}
// typical useage
unsigned int16 tempf=986; // 98.6 degreres F
printf("Deg C %3.1w Deg F %3.1w \r\n", DegFtoC(tempf), tempf);
// outputs Deg C 37.0 Deg F 98.6
|
|
|