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=-2.9 required=5.0 tests=BAYES_00,MAILING_LIST_MULTI autolearn=unavailable autolearn_force=no version=3.4.4 X-Google-Thread: 103376,f822ae7b0f7433c1 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII Return-Path: x-mimeole: Produced By Microsoft Exchange V6.5 Content-class: urn:content-classes:message MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Subject: RE: Translating an embedded C algorithm Date: Mon, 15 Jan 2007 19:50:27 -0800 In-Reply-To: <1168871816.263502.212100@11g2000cwr.googlegroups.com> X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: Translating an embedded C algorithm Thread-Index: Acc4s9P5PtCOIp+nTWGc33wfMv0K2AAbGL/Q References: <1168871816.263502.212100@11g2000cwr.googlegroups.com> From: "Vo, Anh \(US SSA\)" To: "Talulah" , X-OriginalArrivalTime: 16 Jan 2007 03:50:27.0447 (UTC) FILETIME=[75AF3870:01C73921] X-Virus-Scanned: amavisd-new at ada-france.org X-BeenThere: comp.lang.ada@ada-france.org X-Mailman-Version: 2.1.9rc1 Precedence: list List-Id: "Gateway to the comp.lang.ada Usenet newsgroup" List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.ada Message-ID: X-Leafnode-NNTP-Posting-Host: 88.191.17.134 Organization: Guest of ProXad - France NNTP-Posting-Date: 16 Jan 2007 04:55:02 MET NNTP-Posting-Host: 88.191.14.223 X-Trace: 1168919702 news-2.free.fr 21547 88.191.14.223:42874 X-Complaints-To: abuse@proxad.net Path: g2news2.google.com!news4.google.com!news.glorb.com!news.cs.univ-paris8.fr!univ-lyon1.fr!news.in2p3.fr!in2p3.fr!feed.ac-versailles.fr!nerim.net!proxad.net!cleanfeed2-a.proxad.net!nnrp14-2.free.fr!not-for-mail Xref: g2news2.google.com comp.lang.ada:8153 Date: 2007-01-16T04:55:02+01:00 -----Original Message----- From: comp.lang.ada-bounces@ada-france.org = [mailto:comp.lang.ada-bounces@ada-france.org] On Behalf Of Talulah Sent: Monday, January 15, 2007 6:37 AM To: comp.lang.ada@ada-france.org Subject: Translating an embedded C algorithm << [..] 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. 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. >>> Here I just translated directory to Ada from C code in the table. Please = pay attention to the Important Note below the Table. Note also that I = have slightly modified some variables for readability. Let me know if = more information is desired. AV ------------------------------------------------------------------------ --| MeasureTemperature --| --| Calculates the temperature in =BAC given the ADC count by lookup --| table and linear interpolation of the diode circuit characteristic. --| --| Parameters: --| AdcCount Raw ADC count. --| --| Returns: --| Temperature in =BAC x 10. INVALID_TEMPERATURE if out of range. --|=20 --| Note: GNAT-GPL-2006 compiler on Windows was used -------------------------------------------------------------------------= function Measure_Temperature (Adc_Count : in Integer) return Integer is Index : Integer :=3D 0; InterpolationDistance : Integer :=3D 0; TempDiff : Integer :=3D 0; TempCalOffset : Integer :=3D 0; Temperature : Integer :=3D 0; =20 TEMPERATURE_TABLE_NUM_ENTRIES : constant :=3D 32; Table : constant array=20 (0 .. TEMPERATURE_TABLE_NUM_ENTRIES - 1) of Integer :=3D ( 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); -- *** Important Note:=20 -- The following constants and function need to be defined. -- They are arbitrary set so compilation is successful. MINIMUM_ADC_COUNT : constant :=3D Integer'Last; MAXIMUM_ADC_COUNT : constant :=3D Integer'Last; SPACING : constant :=3D 1;=20 TEMPERATURE_CAL_VALUE : constant :=3D 0; function ReadEEPROM (Temp : in Integer) return Integer is begin return Temp; -- for now end ReadEEPROM; ----------------------------------------------------------- =20 begin =20 -- Index is the index into the table. Each table entry is 8 ADC -- counts higher than the last one, so subtract the offset and -- divide by 8 to find the table index. InterpolationDistance -- is the linear distance between the the table entry ADC cou nt -- and the actual ADC count. -- Check range of Adc_Count and saturate index if out of range. if (Adc_Count < MINIMUM_ADC_COUNT) then -- Underflow of ADC - saturate at minimum value Index :=3D 0; elsif (Adc_Count > MAXIMUM_ADC_COUNT) then -- Overflow of ADC - saturate at maximum value Index :=3D TEMPERATURE_TABLE_NUM_ENTRIES - 1; else -- Find the index of the table entry just below the ADC value Index :=3D (Adc_Count - MINIMUM_ADC_COUNT) / SPACING; end if; =20 -- Calculate the interpolation between the table entries either side = of -- the ADC value. This is the remainder of the difference between the -- ADC count and the minimum temperature ADC count divided by the -- spacing between table entries. Since the spacing is a power of 2, -- this can be achieved by simply masking all but the bottom 3 bits. InterpolationDistance :=3D (Adc_Count - MINIMUM_ADC_COUNT) + (SPACING = - 1); TempDiff :=3D (Table(Index) - Table (Index+1)); =20 -- The temperature is then the base temperature minus the amount -- calculated by linear interpolation. The compiler is clever enough -- to know that dividing by 8 is a right shift of three bits. Temperature :=3D=20 Table(Index) - ((TempDiff * InterpolationDistance) / = SPACING); =20 -- To this is then added the calibration offset to the temperature = table. TempCalOffset :=3D ReadEeprom (TEMPERATURE_CAL_VALUE); Temperature :=3D Temperature + TempCalOffset; =20 return Temperature; =20 end Measure_Temperature; =20