I used the formula in that doc to generate a lookup table:
//////////////////////////////////////////////////////////////////////
//
// rangeTable is a 128-entry table that converts the 8-bit A/D value
// to the corresponding range in cm.
byte const rangeTable [128] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 80, 76, 72, 68, 65, 62, 59, 57, 54, 52, 50, 48, 47,
45, 44, 42, 41, 40, 38, 37, 36, 35, 34, 33, 33, 32, 31, 30, 30,
29, 28, 28, 27, 26, 26, 25, 25, 24, 24, 23, 23, 23, 22, 22, 21,
21, 21, 20, 20, 20, 19, 19, 19, 18, 18, 18, 18, 17, 17, 17, 17,
16, 16, 16, 16, 16, 15, 15, 15, 15, 15, 14, 14, 14, 14, 14, 14,
13, 13, 13, 13, 13, 13, 13, 12, 12, 12, 12, 12, 12, 12, 12, 11,
11, 11, 11, 11, 11, 11, 11, 11, 11, 10, 10, 10, 10, 10, 10, 10};
//////////////////////////////////////////////////////////////////////
//
// rangeFinderGetChannel
//
// Return a converted range from the given A/D channel.
// The conversion is done via a lookup table.
byte rangeFinderGetChannel (byte channel)
{
byte voltage;
set_adc_channel (channel);
delay_us (10);
voltage = read_adc ();
if (voltage > 127)
return (10);
return (rangeTable [voltage]);
}
- Jon