Question: Please implement this code, will upvote! int set _ temp _ from _ ports ( temp _ t * temp ) ; / / Uses
Please implement this code, will upvote!
int settempfromportstempt temp;
Uses the two global variables ports THERMOSENSORPORT and
THERMOSTATUSPORT to set the fields of temp If
THERMOSENSORPORT is negative or above its maximum trusted value
associated with deg C this function sets the
tenthsdegrees to and the tempmode to for temp before
returning Otherwise, converts the sensor value to deg C using
shift operations. Further converts to deg F if indicated from
THERMOSTATUSPORT. Sets the fields of temp to appropriate
values. tempmode is for Celsius, and for Fahrenheit. Returns
on success. This function DOES NOT modify any global variables
but may access them.
CONSTRAINTS: Uses only integer operations. No floating point
operations are used as the target machine does not have a FPU. Does
not use any math functions such as abs
This function works with the struct tempt defined in thermo.h which has the following layout.
Breaks temperature down into constituent parts
typedef struct
short tenthsdegrees; actual temp in tenths of degrees
char tempmode; for celsius, for fahrenheit, for error
tempt;
The function settempfromports will read the global variables mentioned above and fill in values for the struct fields of the parameter temp. To convert the temperature sensor value to a displayable temperature, one must perform some division and modulo operations.
"Divide" the sensor value by to get the number of tenths degrees Celsius above deg C Note the documentation indicates that this can be done using a shift operation as we are dividing by a power of two.
Round up if the remainder is high enough. Note that when using shifting bits to emulate division by a power of bits which are shifted off would be the remainder and these can be isolated with appropriate bitwise logical operations to check them.
Account for the offset from deg C by subtracting.
Check THERMOSTATUSPORT to see if conversion to Fahrenheit is needed.
If conversion is needed, use the formula
farenheit celsius ;
but note that we are working in tenths degrees so adjustments may be needed. No rounding is done when converting from Celsius to Fahrenheit. Use of this conversion and lack of rounding means that, despite the highresolution temperature, degrees in Fahrenheit only appear in increments of about deg F giving it less resolution than the Celsius. This is the price of a simpler implementation. Continue to abide by the constraint: do not use floating point operations. Simple microprocessors cannot perform floating point operations as they lack a Floating Point Unit FPU
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
