A Sample 68HC05 Program
The sample function that follows finds the cosine of an angle between 0 and 180 degrees inclusive by interpolating the result from a look up table.
The table consists of 46 elements representing the cosine of every fourth degree, again, from 0 to 180 degrees inclusive, scaled by 127.
A simple linear interpolation is performed using these standardized equations:
Notes:
To simplify the interpolation math, the look up table includes the cosine of every fourth degree (0°, 4°, 8°, and so on) rather than, say, every fifth degree (0°, 5°, 10°, and so on).
This makes the difference between the known upper and lower angles four, and allows the division of the difference between the cosines of the known lower and upper angles to be accomplished with two right shift instructions.
Similarly, because all the known angles are multiples of four, the difference between the given angle and the known lower angle is found by logically ANDing the given angle with three.
These simplifications allow the interpolation to be taken with only three mathematical operations:
1. the difference between the cosine of the known angle and delta2. the difference in the numerator of the delta fraction3. the product of the two delta terms