From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.1 required=5.0 tests=BAYES_00,FREEMAIL_FROM, PP_MIME_FAKE_ASCII_TEXT autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,f822ae7b0f7433c1 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII Path: g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local01.nntp.dca.giganews.com!nntp.comcast.com!news.comcast.com.POSTED!not-for-mail NNTP-Posting-Date: Thu, 18 Jan 2007 22:05:13 -0600 From: "Steve" Newsgroups: comp.lang.ada References: <1168871816.263502.212100@11g2000cwr.googlegroups.com> Subject: Re: Translating an embedded C algorithm Date: Thu, 18 Jan 2007 20:08:12 -0800 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.3028 X-RFC2646: Format=Flowed; Original X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3028 Message-ID: NNTP-Posting-Host: 24.20.111.245 X-Trace: sv3-WysNDEvIIA4pKjFbvnHgRSOYD04wQt7rTib7Tff3aIXs+nEEDJ9NkczE0wtMfq542r+gJf72n/tsCYE!Mh6ZcDiVpiwz5MOaOOXh8BYGX8FaGfLu/9bKFfzLZEu+gTwdT+BkWTcgDb1z9YdysWfwtSjw2uUR!pf7ezk2KYP5lNg== X-Complaints-To: abuse@comcast.net X-DMCA-Complaints-To: dmca@comcast.net X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.32 Xref: g2news2.google.com comp.lang.ada:8303 Date: 2007-01-18T20:08:12-08:00 List-Id: "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 >