comp.lang.ada
 help / color / mirror / Atom feed
From: "Vo, Anh \(US SSA\)" <anh.vo@baesystems.com>
To: "Talulah" <paul.hills@uk.landisgyr.com>, <comp.lang.ada@ada-france.org>
Subject: RE: Translating an embedded C algorithm
Date: Mon, 15 Jan 2007 19:50:27 -0800
Date: 2007-01-16T04:55:02+01:00	[thread overview]
Message-ID: <mailman.22.1168919455.18371.comp.lang.ada@ada-france.org> (raw)
In-Reply-To: <1168871816.263502.212100@11g2000cwr.googlegroups.com>

-----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 º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.
--| 
--| Note: GNAT-GPL-2006 compiler on Windows was used
-------------------------------------------------------------------------
function Measure_Temperature (Adc_Count : in Integer) return Integer is

   Index : Integer := 0;
   InterpolationDistance : Integer := 0;
   TempDiff : Integer := 0;
   TempCalOffset : Integer := 0;
   Temperature : Integer := 0;
   
   TEMPERATURE_TABLE_NUM_ENTRIES : constant := 32;
   Table : constant array 
      (0 .. TEMPERATURE_TABLE_NUM_ENTRIES - 1) of Integer := (
               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: 
   -- The following constants and function need to be defined.
   -- They are arbitrary set so compilation is successful.
   MINIMUM_ADC_COUNT : constant := Integer'Last;
   MAXIMUM_ADC_COUNT : constant := Integer'Last;
   SPACING : constant := 1; 
   TEMPERATURE_CAL_VALUE : constant := 0;

   function ReadEEPROM (Temp : in Integer) return Integer is
   begin
      return Temp; -- for now
   end ReadEEPROM;
   -----------------------------------------------------------
   
begin
   
   -- 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 := 0;
   elsif (Adc_Count > MAXIMUM_ADC_COUNT) then
      -- Overflow of ADC - saturate at maximum value
      Index := TEMPERATURE_TABLE_NUM_ENTRIES - 1;
   else
      -- Find the index of the table entry just below the ADC value
      Index := (Adc_Count - MINIMUM_ADC_COUNT) / SPACING;
   end if;
   
   -- 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 := (Adc_Count - MINIMUM_ADC_COUNT) + (SPACING - 1);
   TempDiff := (Table(Index) - Table (Index+1));
   
   -- 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 := 
            Table(Index) - ((TempDiff * InterpolationDistance) / SPACING);
   
   -- To this is then added the calibration offset to the temperature table.
   TempCalOffset := ReadEeprom (TEMPERATURE_CAL_VALUE);
   Temperature := Temperature + TempCalOffset;
   
   return Temperature;
        
end Measure_Temperature;
         



  parent reply	other threads:[~2007-01-16  3:50 UTC|newest]

Thread overview: 102+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-01-15 14:36 Translating an embedded C algorithm Talulah
2007-01-16  0:06 ` Jeffrey Carter
2007-01-16  1:10   ` Marc A. Criley
2007-01-16  2:21   ` Cesar Rabak
2007-01-16 10:40     ` Markus E Leypold
2007-01-16 14:48       ` Larry Kilgallen
2007-01-16 17:32     ` Jeffrey Carter
2007-01-16 18:04       ` Cesar Rabak
2007-01-17  0:09         ` Jeffrey Carter
2007-01-17  1:07           ` Cesar Rabak
2007-01-16  4:37   ` Alexander E. Kopilovich
2007-01-16 13:37     ` Cesar Rabak
2007-01-16 23:47     ` Simon Wright
2007-01-17  1:02       ` Cesar Rabak
2007-01-16  3:50 ` Vo, Anh (US SSA) [this message]
2007-01-16 12:15   ` Niklas Holsti
2007-01-16 23:50     ` Simon Wright
2007-01-18 16:17     ` Niklas Holsti
2007-01-18 16:41       ` Ludovic Brenta
2007-01-18 20:02         ` Niklas Holsti
2007-01-18 22:25         ` Cesar Rabak
2007-01-19  8:32           ` Niklas Holsti
2007-01-19 19:15             ` Cesar Rabak
2007-01-19 20:49             ` Simon Wright
2007-01-18 16:55       ` Robert A Duff
2007-01-18 18:54         ` Jeffrey Carter
2007-01-19  0:45           ` Robert A Duff
2007-01-18 21:25         ` Niklas Holsti
2007-01-19  0:50           ` Robert A Duff
2007-01-19  4:43           ` Jeffrey Carter
2007-01-18 18:43       ` Jeffrey Carter
2007-01-18 20:19         ` Niklas Holsti
2007-01-18 20:30       ` Niklas Holsti
2007-01-18 23:34       ` Cesar Rabak
2007-01-19  8:57         ` Niklas Holsti
2007-01-19  2:11       ` Steve Whalen
2007-01-19 10:27         ` Niklas Holsti
2007-01-16 13:32   ` Cesar Rabak
2007-01-16 14:47   ` Gautier
2007-01-16 15:15     ` Cesar Rabak
2007-01-16 15:16     ` Jean-Pierre Rosen
2007-01-16 16:12       ` Ludovic Brenta
2007-01-16 17:10         ` Georg Bauhaus
2007-01-16 22:32           ` Ludovic Brenta
2007-01-17 20:22             ` Georg Bauhaus
2007-01-18  9:23               ` Ludovic Brenta
2007-01-16 17:12         ` Cesar Rabak
2007-01-16 17:20           ` Frank J. Lhota
2007-01-16 18:09             ` Cesar Rabak
2007-01-16 17:36           ` Dmitry A. Kazakov
2007-01-16 18:08             ` Cesar Rabak
2007-01-16 18:48               ` Dmitry A. Kazakov
2007-01-16 20:03                 ` Cesar Rabak
2007-01-18 19:33                   ` Björn Persson
2007-01-18 22:32                     ` Cesar Rabak
2007-01-19 20:26                       ` Björn Persson
2007-01-19 23:25                         ` Cesar Rabak
2007-01-19  7:15                     ` Maciej Sobczak
2007-01-19 20:27                       ` Björn Persson
2007-01-19 20:34                         ` Robert A Duff
2007-01-17 13:48           ` Maciej Sobczak
2007-01-17 23:32             ` Translating an embedded C algorithm -- OT Cesar Rabak
2007-01-18  8:56               ` Talulah
2007-01-18 22:05                 ` Cesar Rabak
2007-01-18  9:03               ` Maciej Sobczak
2007-01-18 10:22                 ` Alex R. Mosteo
2007-01-18 18:34                 ` Jeffrey Carter
2007-01-18 22:26                   ` Cesar Rabak
2007-01-19  4:45                     ` Jeffrey Carter
2007-01-18 22:18                 ` Cesar Rabak
2007-01-19 20:53                   ` Simon Wright
2007-01-16 15:55   ` Translating an embedded C algorithm Cesar Rabak
2007-01-17  3:00     ` Vo, Anh (US SSA)
2007-01-17 10:48       ` Cesar Rabak
2007-01-17 11:44       ` Niklas Holsti
2007-01-17 13:31         ` Talulah
2007-01-17 19:20           ` Jeffrey Carter
2007-01-18 14:19             ` Talulah
2007-01-18 15:28               ` Jean-Pierre Rosen
2007-01-18 23:27                 ` Cesar Rabak
2007-01-18 18:51               ` Jeffrey Carter
2007-01-18 22:30                 ` Cesar Rabak
2007-01-19  4:48                   ` Jeffrey Carter
2007-01-19 19:13                     ` Cesar Rabak
2007-01-20 20:56                       ` Jeffrey Carter
2007-01-19  2:21                 ` Alexander E. Kopilovich
2007-01-19  3:25                   ` Larry Kilgallen
2007-01-20  0:46                     ` Alexander E. Kopilovich
2007-01-20 13:03                       ` Larry Kilgallen
2007-01-20 16:54                         ` Alexander E. Kopilovich
2007-01-20 23:53                           ` Larry Kilgallen
2007-01-20 21:02                         ` Jeffrey Carter
2007-01-25 21:59                     ` Markus E Leypold
2007-01-26  4:06                       ` Larry Kilgallen
2007-01-26 11:26                         ` Markus E Leypold
2007-01-26 12:25                           ` Cesar Rabak
2007-01-19  4:52                   ` Jeffrey Carter
2007-01-19 10:13                   ` Warner BRUNS
2007-01-19 14:54                   ` Robert A Duff
2007-01-19  4:08 ` Steve
2007-01-19 20:41   ` Simon Wright
  -- strict thread matches above, loose matches on Subject: below --
2007-01-17  7:07 AW: " Grein, Christoph (Fa. ESG)
2007-01-17 10:26 ` Ludovic Brenta
2007-01-17 16:44   ` Markus E Leypold
2007-01-18  8:49     ` Ludovic Brenta
2007-01-19  9:33       ` Stephen Leake
2007-01-19 19:23         ` Cesar Rabak
2007-01-19 20:27           ` Robert A Duff
2007-01-20  9:54             ` Dmitry A. Kazakov
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox