comp.lang.ada
 help / color / mirror / Atom feed
From: "Steve" <nospam_steved94@comcast.net>
Subject: Re: Translating an embedded C algorithm
Date: Thu, 18 Jan 2007 20:08:12 -0800
Date: 2007-01-18T20:08:12-08:00	[thread overview]
Message-ID: <V-Cdncurtp_k3i3YnZ2dnUVZ_s-rnZ2d@comcast.com> (raw)
In-Reply-To: 1168871816.263502.212100@11g2000cwr.googlegroups.com

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 5631 bytes --]

"Talulah" <paul.hills@uk.landisgyr.com> 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
> 





  parent reply	other threads:[~2007-01-19  4:08 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)
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 [this message]
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