CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

FAST F2C C2F temperature converts - no floats - no division

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

FAST F2C C2F temperature converts - no floats - no division
PostPosted: Fri Apr 26, 2013 2:00 pm     Reply with quote

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   
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group