Article View: alt.flame.pascal
Article #4Re: How to count time of signals in real Time with PC-Timer ?
From: Jens-Erich Lange
Date: Thu, 13 Jan 2000 00:00
Date: Thu, 13 Jan 2000 00:00
301 lines
7193 bytes
7193 bytes
Endlich mal einer den das hier �berhaupt interessiert...
(Echtzeit-Programmierer, wo seid ihr???) Mein Beitrag
weiter unten war ja nicht sehr fruchtbar.
Hier nun die L�sung, bestehend aus den beiden Units
"TimerTic" und "Micro"
Have Fun,
Jens
{ (C) Jens-Erich Lange. Alle Rechte vorbehalten! }
{ http://home.t-online.de/home/dynamo }
unit TimerTic;
interface
function TimerTicks : longint;
function NewTimer(Msek : longint) : longint;
function TimeOut(Time : longint) : boolean;
procedure WaitTimeOut(Time : longint);
procedure WaitDelay(Msek : longint);
implementation
uses
Micro;
const
MaxGap = 60000;
{ Mindestens alle 60 Sek nachschauen! }
MaxTimer = 86400000;
{ �berlauf des Z�hlers }
function TimerTicks;
begin
TimerTicks := CurrentMilli;
end;
function NewTimer;
begin
NewTimer := (CurrentMilli + MSek) mod MaxTimer;
end;
function TimeOut;
var
t : longint;
x : longint;
begin
TimeOut := false;
t := CurrentMilli;
x := t - Time;
if (x >= 0) and (x < MaxGap) then TimeOut := true;
x := MaxTimer - Time + t;
if (x >= 0) and (x < MaxGap) then TimeOut := true;
end;
procedure WaitTimeOut;
begin
repeat
until TimeOut(Time);
end;
procedure WaitDelay;
var Time : longint;
begin
Time := NewTimer(Msek);
WaitTimeOut(Time);
end;
end.
{ (C) Jens-Erich Lange. Alle Rechte vorbehalten! }
{ http://home.t-online.de/home/dynamo }
unit Micro;
interface
type
MicroTime = record
TicValue : longint;
MicValue : word;
end;
MicroClock = record
Hour : byte; { 0..23 }
Minute : byte; { 0..59 }
Second : byte; { 0..59 }
MilliSec : word; { 0..999 }
MicroSec : word; { 0..999 }
end;
function MTimeToMilli(T : MicroTime) : longint;
function MTimeToMicro(T : MicroTime) : real;
function ClockToMilli(C : MicroClock) : longint;
function ClockToMicro(C : MicroClock) : real;
procedure MicroToClock(M : real; var C : MicroClock);
procedure MTimeToClock(T : MicroTime; var C : MicroClock);
procedure CurrentMTime(var T : MicroTime);
procedure CurrentClock(var C : MicroClock);
function CurrentMilli : longint;
function CurrentMicro : real;
procedure MilliDelay(MilliSec : longint);
procedure MicroDelay(MilliSec : real);
implementation
const
TimerC = $43;
Timer0 = $40;
Modus2 = $34;
Modus3 = $36;
Faktor = 1193.182;
var
Ticker : longint absolute $0040:$006C;
ExitSave : pointer;
function MTimeToMilli(T : MicroTime) : longint;
begin
MTimeToMilli := round(((T.TicValue * 65536.0) + T.MicValue) / Faktor);
end;
function MTimeToMicro(T : MicroTime) : real;
begin
MTimeToMicro := ((T.TicValue * 65536.0) + T.MicValue) / Faktor;
end;
function ClockToMilli(C : MicroClock) : longint;
begin
with C do ClockToMilli := (longint(Hour) * 3600000) +
(longint(Minute) * 60000) +
(longint(Second) * 1000) +
MilliSec;
end;
function ClockToMicro(C : MicroClock) : real;
begin
with C do ClockToMicro := (longint(Hour) * 3600000) +
(longint(Minute) * 60000) +
(longint(Second) * 1000) +
MilliSec +
(MicroSec / 1000);
end;
procedure MicroToClock(M : real; var C : MicroClock);
begin
with C do
begin
if (M > 86400000.0) or (M < 0.0) then M := 0.0;
Hour := byte(trunc(M / 3600000.0));
M := M - (Hour * 3600000.0);
Minute := byte(trunc(M / 60000.0));
M := M - (Minute * 60000.0);
Second := byte(trunc(M / 1000.0));
M := M - (Second * 1000.0);
MilliSec := word(trunc(M));
MicroSec := word(trunc(frac(M) * 1000.0));
end;
end;
procedure MTimeToClock(T : MicroTime; var C : MicroClock);
var M : real;
begin
M := MTimeToMicro(T);
MicroToClock(M,C);
end;
procedure CurrentMTime(var T : MicroTime);
var
HIB : byte;
LOB : byte;
begin
T.TicValue := Ticker;
port[TimerC] := 0;
LOB := port[Timer0];
HIB := port[Timer0];
T.MicValue := not (word(HIB) shl 8 + LOB);
if (Ticker > T.TicValue) and (T.MicValue < 32000) then inc(T.TicValue);
end;
procedure CurrentClock(var C : MicroClock);
var T : MicroTime;
begin
CurrentMTime(T);
MTimeToClock(T,C);
end;
function CurrentMilli : longint;
var T : MicroTime;
begin
CurrentMTime(T);
CurrentMilli := MTimeToMilli(T);
end;
function CurrentMicro : real;
var T : MicroTime;
begin
CurrentMTime(T);
CurrentMicro := MTimeToMicro(T);
end;
procedure MilliDelay(MilliSec : longint);
var
T0 : MicroTime;
T1 : longint;
T2 : longint;
begin
CurrentMTime(T0);
T1 := MTimeToMilli(T0);
repeat
CurrentMTime(T0);
T2 := MTimeToMilli(T0);
until (T2-MilliSec) > T1;
end;
procedure MicroDelay(MilliSec : real);
var
T0 : MicroTime;
T1 : real;
T2 : real;
begin
CurrentMTime(T0);
T1 := MTimeToMicro(T0);
repeat
CurrentMTime(T0);
T2 := MTimeToMilli(T0);
until (T2-MilliSec) > T1;
end;
{$F+}
procedure ExitCode;
begin
{$IFDEF ShowExit}
writeln('Micro');
{$ENDIF}
ExitProc := ExitSave;
port[TimerC] := Modus3;
port[Timer0] := 0;
port[Timer0] := 0;
end;
{$F-}
begin
{$IFDEF ShowInit}
writeln('Micro');
{$ENDIF}
ExitSave := ExitProc;
ExitProc := @ExitCode;
port[TimerC] := Modus2;
port[Timer0] := 0;
port[Timer0] := 0;
end.
Technical Details
Message-ID:
<85kbs0$7pc$1@news00.btx.dtag.de>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<387CD946.7C08132B@swissonline.ch>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII X-Google-Thread: fd1d1,13e283332aa96bc0 X-Google-Attributes: gidfd1d1,public X-Google-Thread: 110467,13e283332aa96bc0 X-Google-Attributes: gid110467,public X-Google-Thread: 108ebd,13e283332aa96bc0 X-Google-Attributes: gid108ebd,public X-Google-Thread: 1043cf,13e283332aa96bc0 X-Google-Attributes: gid1043cf,public X-Google-Thread: 10eb32,13e283332aa96bc0 X-Google-Attributes: gid10eb32,public X-Google-Thread: 10c950,13e283332aa96bc0 X-Google-Attributes: gid10c950,public From: "Jens-Erich Lange" <dynamo@t-online.de> Subject: Re: How to count time of signals in real Time with PC-Timer ? Date: 2000/01/13 Message-ID: <85kbs0$7pc$1@news00.btx.dtag.de> X-Deja-AN: 573294994 Content-Transfer-Encoding: 8bit References: <387CD946.7C08132B@swissonline.ch> X-Sender: 340072856479-0001@t-dialin.net Content-Type: text/plain; charset=ISO-8859-1 X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3 X-Complaints-To: abuse@t-online.de X-Trace: news00.btx.dtag.de 947761856 7980 340072856479-0001 000113 11:10:56 Organization: T-Online Mime-Version: 1.0 Newsgroups: alt.flame.pascal,borland.public.delphi.objectpascal,borland.public.turbopascal,comp.lang.pascal.ansi-iso,de.comp.lang.pascal,fido.ger.pascal