Page 337 - Programming Microcontrollers in C
P. 337
322 Chapter 6 Large Microcontrollers
80, 50, 0x01c0,
120, 120, 0x0200,
80, 240
};
Note that in the table above, the slopes are entered in the table as
two-byte integers. There is no binary point in the slope. The conver
sion of these integers to floating-point numbers is accomplished easily.
We must merely recognize that these numbers are a factor of 128 too
large, so after the slope is used as a multiplier, the result must be
divided by 128 to get the correct answer. Of course, division by 128
can be accomplished by a shift right by 8, or more practically merely
choosing the left byte of the product.
A function that will make use of this table is
char table_look_up( char x_in)
{
int i;
for(i=0;x_in>_lut.data[i].x && i<=_lut.entries; i++);
if(i>=_lut.entries)
return _lut.data[_lut.entries-1].y;
else if (i==0)
return _lut.data[0].y;
else
return _lut.data[i-1].y+(((x_in-_lut.data[i-1].x)*
_lut.data[i-1].slope)>>8);
}
Listing 6-6: Table Look-Up Routine, Version 1
The compiler optimizer will recognize the shift right by 8 in the
above function and will merely select the upper byte of the result
rather than executing the shift operation indicated.
This function was checked on an evaluation system for the single
input value of 67 and the result was the expected value of 38. Of
course, that did not check the function over its full range of opera
tion. The check over the full range was accomplished on a host
machine. It is a simple matter to include the above function and table
in the following program.
#include “tlu.h”
void main(void)