Page 335 - Programming Microcontrollers in C
P. 335

320    Chapter 6  Large Microcontrollers


                              Result =15 +  50 – 15)(   (67 – 40 )  = 38.625
                                                     80 – 40
                              Notice that there is a product and a division that is the same for
                          all input values between 40 and 80 in this case. This calculation is
                          the slope of the line between the two points being interpolated. Usu­
                          ally table look-up operations are used where time constraints on the
                          program are great so that these operations, multiply and divide (es­
                          pecially divide), are unwelcome in these programs. There is a way to
                          avoid the divide operation. Note that between each set of points the
                          slope is a constant. Therefore, if the slope of the line is built into the
                          table, the calculation would involve one subtraction, one multiply,
                          and one addition. Let us modify the table above to contain the slopes
                          of the lines between each point.


                                  20        5        0.5
                                  40       15      0.875
                                  80       50       1.75
                                 120      120          2
                                 180      240
                              Table 6-4: Look-Up Table With Slopes

                              Note that the slope of the line runs from the lower point to the
                          next point. Therefore, there is no slope for the last point. When this
                          table is built in memory, it consists of a header followed by a four-
                          byte entry for each entry in the table. The header consists of a single
                          byte that indicates the length of the table. In this case, since the table
                          starts with an independent variable value of 20, there are expected to
                          be no input values less than 20. If there are, however, the value of 5
                          will be used for these output values. Also, if the value of the indepen­
                          dent variable is greater than the maximum of 180, a value of 240 will
                          be returned from the routine. With data from the above table, the
                          calculation above would reduce to

                              Result = 15 + 0.875(67 - 40) = 38.625
                          which requires a multiply, a subtract and one add. Of course, the
                          routine will truncate the fractional part of the result so that the value
                          returned will be 38.
   330   331   332   333   334   335   336   337   338   339   340