"Talulah" wrote in message news:1168871816.263502.212100@11g2000cwr.googlegroups.com... > Hi, > > I am in the process of writing a book about real-time embedded > development (hardware and software). In it there is a section on > different programming languages that are available for embedded > development. I'm using a small C function that does temperature > measurement by linear interpolation of a lookup table as an example. I > have C code to do this, and would like to have the same algorithm > implemented in other languages. > > Unfortunately I'm really a C programmer, with very little experience > of other languages, so I would like to ask if anyone on this newsgroup > is interested in translating the algorithm into Ada. Of course you > would be mentioned in thanks in the book once it is finished. > Here is a bit more Ada-ish version: with Interfaces; package Temperature_Evaluation is type Kelvin_Tenths is new Interfaces.Integer_16; type Adc_Reading is range 0 .. 2**10-1; ------------------------------------------------------------------------ -- MeasureTemperature -- -- Calculates the temperature in �C given the ADC count by lookup -- table and linear interpolation of the diode circuit characteristic. -- -- Parameters: -- AdcCount Raw ADC count. -- -- Returns: -- Temperature in �C x 10. INVALID_TEMPERATURE if out of range. ------------------------------------------------------------------------ function MeasureTemperature( AdcCount : Adc_Reading ) return Kelvin_Tenths; end Temperature_Evaluation; with Interfaces; package body Temperature_Evaluation is ---------------------------------------------------------------------- -- The Temperature_Lookup table is used to map ADC counts to a -- temperature in kelvins. -- Consecutive values in the table represent an increase of 8 ADC -- counts. -- The ADC value of the first entry in the table is given by: -- Base_Adc_Value; ---------------------------------------------------------------------- Interp_Interval : constant := 8; Base_Adc_Value : constant := 220; Temperature_Lookup : constant array( Positive range <>) of Kelvin_Tenths := ( 1171, 1116, 1061, 1006, 952, 899, 846, 793, 741, 689, 638, 588, 537, 488, 438, 389, 341, 293, 246, 199, 152, 106, 61, 16, -29, -73, -116, -160, -202, -244, -286, -327 ); ------------------------------------------------------------------------ -- MeasureTemperature -- -- Calculates the temperature in �C given the ADC count by lookup -- table and linear interpolation of the diode circuit characteristic. -- -- Parameters: -- AdcCount Raw ADC count. -- -- Returns: -- Temperature in �C x 10. INVALID_TEMPERATURE if out of range. ------------------------------------------------------------------------ function MeasureTemperature( AdcCount : Adc_Reading ) return Kelvin_Tenths is Table_Index : Integer; Table_Remainder : Integer; Temperature : Kelvin_Tenths; Temp_Diff : Integer; begin -------------------------------------------------------------------- -- Evaluate the index into Temperature_Lookup as well as remaining -- Adc counts that are not covered by the lookup. -------------------------------------------------------------------- Table_Index := Temperature_Lookup'First + (Integer(AdcCount) - Base_Adc_Value) / Interp_Interval; Table_Remainder := (Integer(AdcCount) - Base_Adc_Value) rem Interp_Interval; -------------------------------------------------------------------- -- Check the index against the bounds of the lookup table -------------------------------------------------------------------- if Table_Index < Temperature_Lookup'First then ---------------------------------------------------------------- -- For an index before the first index of the table, use -- the first index. ---------------------------------------------------------------- Table_Index := Temperature_Lookup'First; elsif Table_Index >= Temperature_Lookup'Last then ---------------------------------------------------------------- -- For an index that at or beyond the last index of the table, -- use the last index. ---------------------------------------------------------------- Table_Index := Temperature_Lookup'Last; end if; -------------------------------------------------------------------- -- Evaluate the base temperature value -------------------------------------------------------------------- Temperature := Temperature_Lookup( Table_Index ); if Table_Remainder /= 0 then Temp_Diff := Integer(Temperature_Lookup( Table_Index + 1 ) - Temperature_Lookup( Table_Index )); Temperature := Temperature + Kelvin_Tenths( ( Temp_Diff * Table_Remainder ) / Interp_Interval); end if; return Temperature + ReadEeprom(TEMPERATURE_CAL_VALUE); end MeasureTemperature; end Temperature_Evaluation; I took the liberty of making minor changes in the logic which I believe simplifies things a bit. Regards, Steve (The Duck) > If you are interested, I have placed the section of the book here: > > http://homepages.which.net/~paul.hills/Temporary/Temperature.pdf > > so you can see the code and description. Please email me if you are > interested to paul (dot) hills (who is at) uk (dot) landisgyr (dot) > com. > > Thanks very much > Paul Hills >