Real-Time Programing Features of Ada:
The facilities that Ada offers as a system-level programming language are very suitable for real-time programming. With Ada you can
specify word formats,
control the internal representation of data,
control where data are placed,
specify register locations and formats, and
specify the underlying representation of user-defined types.
Ada gives you access to machine features:
machine code,
interrupts,
interfaces to other languages,
unscheduled access to shared variables, and
unchecked type conversion.
1. Define types and subtypes required:
type STATUS_TYPE is (OFF, LOW, NORMAL, HIGH);
subtype TIME_TYPE is INTEGER range 0 .. 2**13-1;
subtype TEMPERATURE_TYPE is INTEGER range 0 .. 2**10-1;
subtype PRESSURE_TYPE is INTEGER range 0 .. 2**6-1;
2. Define record for word formats:
type SENSOR_WORD_FORMAT is
record
STATUS : STATUS_TYPE;
TIME : TIME_TYPE;
TEMPERATURE : TEMPERATURE_TYPE;
PRESSURE : PRESSURE_TYPE;
end record;
3. Use representation specification to map the values and bits: Machine dependent
for STATUS_TYPE use ( OFF => 2#000#,
LOW => 2#001#,
NORMAL => 2#010#,
HIGH => 2#100# );
for SENSOR_WORD_FORMAT use
record
STATUS at 0 range 1 .. 3;
TIME at 0 range 4 .. 16;
TEMPERATURE at 1 range 1 .. 10;
PRESSURE at 1 range 11 .. 16;
end record;
4. Use representation specification to map the values and bits: Machine independent
The other way of handling this word format is to use a pragma. Pragma's
in Ada give directions to the compiler, but do not generate code.
pragma PACK (SENSOR_WORD_FORMAT);
tells the compiler to pack the words in the computer as tightly as possible with
the fields specified in the machine-independent version of the sensor record.
5. Convert temperature into degree Celsius:
The designer of the equipment might have stated that each step in the temperature input represents 0.25 degree Celsius and that a zero input represents -50 degrees. Thus, to convert the temperature information, we need to multiply the input value by 0.25 and to subtract 50 degrees. This can be done by converting the input information to a floating point type and then performing the calculations in an assignment statement. For example,
INPUT : SENSOR_WORD_FORMAT;
TEMPERATURE_IN_CELSIUS : = 0. 25 * FLOAT (INPUT. TEMPERATURE) - 50. 0;
Problem Statement:
The interface in below figure is an eight-bit interface. ASCII data come into this computer at a specified address A040 (in base 16).
type BINARY_STRING is array (POSITIVE range <>) of BOOLEAN;
INPUT_DATA: BINARY_STRING (1 .. 8);
pragma PACK (BINARY_STRING);
for INPUT_DATA use at 16#A040#;
Package specification:
with UNCHECKED_CONVERSION;
package DATA_CONVERSION is
procedure GET_DATA (C: out CHARACTER);
end DATA_CONVERSION;
Package body:
with UNCHECKED_CONVERSION;
package body DATA_CONVERSION is
procedure GET_DATA (C : out CHARACTER) is
type BINARY_STRING is array (POSITIVE range <>) of BOOLEAN;
INPUT_DATA: BINARY_STRING (1 .. 8);
TEMPORARY : BINARY-STRING (1 .. 8);
pragma PACK (BINARY_STRING);
for INPUT_DATA use at 16#A040#;
function UNCHECKED_CONVERSION is new UNCHECKED_CONVERSION (BINARY_STRING, CHARACTER);
begin
while INPUT_DATA (8) loop -- busy loop on bit 8
null;
end loop;
INPUT_DATA : = not INPUT_DATA; -- changes all bits and bit 8 is set
TEMPORARY : = INPUT_DATA and (FALSE, others => TRUE);
C : = UNCHECKED_CONVERSION (TEMPORARY);
end GET_DATA;
end DATA-CONVERSION;