Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

URGENT please help!! 3.2 thermo_update.c: Updating the Display with User Code Periodically the microcontroller will run code to adjust the thermometer display to show the

URGENT please help!!

3.2 thermo_update.c: Updating the Display with User Code

Periodically the microcontroller will run code to adjust the thermometer display to show the current temperature. This function is

int thermo_update(); 

and it will be your job to write this function

Rather than write everything that needs to be done within thermo_update(), several helper functions will be used to divide this task into several more manageable and testable chunks.

These should all be written in thermo_update.c and are as follows.

Converting time of Day in Seconds to a Struct

int set_temp_from_ports(temp_t *temp); // Uses the two global variables (ports) THERMO_SENSOR_PORT and // THERMO_STATUS_PORT to set the temp structure. If THERMO_SENSOR_PORT // is above its maximum trusted value, associated with +50.0 deg C, // does not alter temp and returns 1. Otherwise, sets fields of temp // based on converting sensor value to degrees and checking whether // Celsius or Fahrenheit display is in effect. Returns 0 on successful // set. This function DOES NOT modify any global variables but may // access global variables. // // CONSTRAINT: Uses only integer operations. No floating point // operations are used as the target machine does not have a FPU. 

This function works with the struct temp_t defined in thermo.h which has the following layout.

// Breaks time down into 12-hour format // Breaks temperature down into constituent parts typedef struct{ short tenths_degrees; // actual temp in tenths of degrees char is_fahrenheit; // 0 for celsius, 1 for fahrenheit } temp_t; 

The function set_temp_from_ports() 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.

  1. Divide the sensor value by 64 to get the number of tenths degrees Celsius above -50.0 deg C. Note the constraint: make use only of integer operations. Do not use float or double variables. This is emulates the somewhat common situation where simple microprocessors cannot perform floating point operations as they lack a Floating Point Unit (FPU).
  2. Round up if the remainder is high enough.
  3. Account for the offset from -50.0 deg C by subtracting.
  4. Check THERMO_STATUS_PORT to see if conversion to Fahrenheit is needed.
  5. If conversion is needed, use the formula

    farenheit = (celsius * 9) / 5 + 32; 

    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 high-resolution temperature, degrees in Fahrenheit only appear in increments of about 0.2 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.

Setting Display from a temp_t

int set_display_from_temp(temp_t temp, int *display); // Alters the bits of integer pointed to by display to reflect the // temperature in struct arg temp. If temp has a temperature value // that is below minimum or above maximum temperature allowable or if // an improper indication of celsius/fahrenheit is given, does nothing // and returns 1. Otherwise, calculates each digit of the temperature // and changes bits at display to show the temperature according to // the pattern for each digit. This function DOES NOT modify any // global variables but may access global variables. 

Importantly, this function sets the bits in the integer pointed to by display which may or may not be the global display variable.

To properly set the display bits, set_dsiplay_from_temp() will need to do bit shifting along with bitwise operations to construct the correct bit pattern for the thermometer display.

A good trick to use is to create a series of bit patterns that correspond to the various digits. For example, according to the diagrams above, the bit patter for 9 is 0b1101111. If a 9 should appear on the display somewhere, this bit pattern should be shifted and combined with the existing bits in display so that a 9 will show. Creating similar constant mask patterns for each digit, the minus sign, and the position of the Celsius/Fahrenheit indicator is a good way to make this problem manageable.

A detailed explanation of one approach to the problem follows.

  • Create an array of bit masks for each of the digits 0-9. The 0th element of the array contains a bit mask like 0b0111111 which represents the bits that should be set for a 0 digit, the 1th element of this array has a mask like 0b0000110 which are the bits to be set for a 1. There should be ten entries in this array in indices 0-9.
  • Use modulo to determine the integer value for the tens, ones, and tenths digits for the temperature. Call these variables something like temp_hundreds, temp_tens and so on. Each variable should be in the range 0-9.
  • Start with an integer variable of 0 (all 0 bits).
  • Use temp_tens to index into your array of masks to determine the bits that should be set for it. Combine the state variable with temp_ones mask.
  • Combining bits here is a logical operation of setting all bits that are one in the mask to 1 in the display variable.
  • Use temp_hundred to index into your array of masks for the right mask for that digit. The bits corresponding to the hundreds place of the temperature is shifted to the left by 21 bits so shift the mask to the left and combine it with the state variable.
  • Repeat this process for the tens digit (shifted by 14) ones, and tenths digits.
  • There are several special cases to consider: leading 0's should be blanks so nothing should be drawn. Also, the negative sign should be positioned immediately to the left of the first non-blank digit. A couple examples of how this looks are below

    RIGHT WRONG 22.4 deg C 022.4 deg C -1.5 deg C -01.5 deg C - 1.5 deg C 

Overall Update Function

int thermo_update(); // Called to update the thermometer display. Makes use of // set_temp_from_ports() and set_display_from_temp() to access // temperature sensor then set the display. Checks these functions and // if they indicate an error, makes not changes to the display. // Otherwise modifies THERMO_DISPLAY_PORT to set the display. // // CONSTRAINT: Does not allocate any heap memory as malloc() is NOT // available on the target microcontroller. Uses stack and global // memory only. 

This function makes use of the previous two functions and the global variables that correspond to the hardware to alter the display. It should be relatively short by making use of the previous functions.

3.3 Thermometer Simulator

While we do not have actual hardware with the features mentioned, a simulator for the system is in the provided file thermo_main.c. You do not need to modify or understand code in either file to complete the assignment though it will certainly expand you C skills to spend some time examining them.

The main() function in thermo_main.c accepts a command line argument which is a number of seconds since the beginning of the day and will call your functions for this problem and show results for it. You are encouraged to use this function to test your code incrementally

  • Examine whether set_temp_from_ports() is correct based on the first part of output in thermo_main.c
  • Once set_temp_from_ports() is complete, examine whether the output of set_display_from_temp() is correct based on the latter part of the output.
  • Once both these functions are correct, examine whether thermo_update() is correct based on the final part of the output of the main() function.

Note that there are a variety of functions in the thermo_main.c which are used to simulate how the thermometer will display. This is also where the global variables like THERMO_DISPLAY_PORT are defined. However, you do not need to modify or even understand the code; it is only used to show how the display would look when the THERMO_DISPLAY_PORT bits are set.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Security

Authors: Alfred Basta, Melissa Zgola

1st Edition

1435453905, 978-1435453906

More Books

Students also viewed these Databases questions