[fpc-pascal] Microsecond Delay Suggestions?
Dmitry Boyarintsev
skalogryz.lists at gmail.com
Tue Jul 26 19:05:13 CEST 2016
On Tue, Jul 26, 2016 at 12:38 PM, James Richters <
james at productionautomation.net> wrote:
> What I need is a timer that I can specify in microseconds, a millisecond
> is too long. I am using it for timing to read in a string on a serial
> connection. My fastest baudrate is 250000, so at that rate I would need to
> delay only 36 microseconds. So I can’t use sleep () or delay () because
> they only go down to 1mS and that’s too slow.
>
Here's an example of Sleep with microseconds that I came up with
procedure SleepMcs(mcs: Int64);
var
ct : TLargeInteger;
fr : TLargeInteger;
af : TLargeInteger;
trg : TLargeInteger;
const
NanoInMicro = 1000;
begin
QueryPerformanceCounter(ct);
QueryPerformanceFrequency(fr);
trg:=round(mcs * NanoInMicro / (NSInSec/fr));
repeat
QueryPerformanceCounter(af);
until trg<=(af-ct);
end;
I'm hoping that there's a better solution out there, because this procedure
will end up with 100% CPU load.
I doubt it's possible to unload the CPU, since the windows scheduler is in
milliseconds accuracy.
May be a lower (kernel/driver) level allows that.
thanks,
Dmitry
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20160726/8489a5c2/attachment.html>
More information about the fpc-pascal
mailing list