ICEINSPACE
Moon Phase
CURRENT MOON
Waning Crescent 30.7%
|
|

21-10-2010, 10:13 PM
|
 |
amateur
|
|
Join Date: Jul 2006
Location: Mt Waverley, VIC
Posts: 7,105
|
|
Delay function & Delphi
The windows app for focus control via stepper motor is almost ready for posting.. apart from little problem: controlled delay around couple of milliseconds.
I have found a number of delay functions on the web (they are all polling RTC), but instead of milliseconds, they are exiting after seconds, sometimes longer..
The obvious alternative solution would be just a program loop, and it is good for me personally since I can adjust the delay to suit my machine..., but then it will not be good for anyone else... Does anyone know how to solve this properly?
All comments are most welcome and appreciated
Last edited by bojan; 22-10-2010 at 11:11 AM.
Reason: question answered
|

21-10-2010, 10:32 PM
|
Watch me post!
|
|
Join Date: Mar 2006
Location: Melbourne
Posts: 1,905
|
|
Gday Bojan
If you want a synchronous function, just use sleep().
This works in milliseconds and is synched to the CPU clock automagically.
If you want an asynchronous method, use a TTimer.
You can program this in millisecs as well, and then just set it running as reqd
and create an event for when it stops, or you can test if its running and prevent a specific call going out till it stops.
Andrew
|

21-10-2010, 10:37 PM
|
 |
amateur
|
|
Join Date: Jul 2006
Location: Mt Waverley, VIC
Posts: 7,105
|
|
Thanks mate, I will try.
I was also thinkig about this:
procedure TForm1.Delay(msecs:integer);
var
FirstTickCount:longint;
begin
FirstTickCount:=GetTickCount;
repeat
Application.ProcessMessages; {allowing access to other
controls, etc.}
until ((GetTickCount-FirstTickCount) >= Longint(msecs));
end;
Would omitting 'Application.ProcessMessages' help?
I don't mind if machine does nothing else while waiting, this app is for focus, therefore it will be used very occasionally.
|

21-10-2010, 10:49 PM
|
 |
Registered User
|
|
Join Date: Jan 2009
Location: Glenhaven
Posts: 4,161
|
|
The traditional C method of doing a sleep with granularity less than 1 second is to use select() with the three fd_sets all being empty and the struct timeval being the required seconds and microseconds.
Code:
int select(int nfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timeval *timeout);
Note that sleep(1) is only guaranteed to sleep to the top of the next second, so not more than one second.
|

21-10-2010, 10:51 PM
|
Watch me post!
|
|
Join Date: Mar 2006
Location: Melbourne
Posts: 1,905
|
|
Gday Bojan
Quote:
I don't mind if machine does nothing else while waiting,
|
Just use the std sleep procedure then.
It automatically converts cpuTicks to millisec, hence is clock independent.
Much simpler than trying to roll yr own timer.
Andrew
PS the "process messages" wouldnt hurt in yr scenario, but will not do anything unless you have an event programmed for the message.
In the case you show, using a timer is probably a neater solution.
|

22-10-2010, 06:46 AM
|
 |
amateur
|
|
Join Date: Jul 2006
Location: Mt Waverley, VIC
Posts: 7,105
|
|
Thanks guys,
sleep() actually works as expected (introducing delay couple of milliseconds long..
so I am sticking with this.
The app will be ready for distribution soon, after some more testing :-)
|

22-10-2010, 08:05 AM
|
 |
Meteor & fossil collector
|
|
Join Date: Jul 2005
Location: Bentleigh
Posts: 1,386
|
|
Always nice to see people using Delphi!
I had to do more than 5 years of c/c++ when I was in Telstra, but did all of my independent utilities in Delphi. Some VB, bit of Basic, Paradox and lots of Turbo Pascal. It always seemed to me that C can programs can be summarised in one word "obfiscation". My current job only uses Delphi (7) and the code base is at 360,000+ lines, it was 60,000 when I started 10 years ago and only a couple of thousand of those lines still exist.
|

22-10-2010, 08:48 AM
|
 |
amateur
|
|
Join Date: Jul 2006
Location: Mt Waverley, VIC
Posts: 7,105
|
|
BTW, in case you didn't know (I didn't), Borland has released turbo Pascal 5.5 as freeware.
http://www.programmersheaven.com/dow.../Download.aspx
Last edited by bojan; 22-10-2010 at 08:59 AM.
|

22-10-2010, 09:19 AM
|
Registered User
|
|
Join Date: Feb 2007
Location: Beaumont Hills NSW
Posts: 2,900
|
|
Hi
I am interested in the methods of using "delay loops". I only program in basic (I am too old to learn something new) and the delay loops of course depend on computer speeds. Programs I wrote for the old PC's and XT's went haywire on 386's and 486's etc. So I needed a way to make delays constant.
When I needed to have a constant delay time I have always had to resort to reading the computer "ticks" over a set time period to produce a modifier to use in the delay loops. It works for me but is a bit messy because it takes a couple of seconds out of the start up time so I am always interested in other methods.
Barry
Last edited by Barrykgerdes; 22-10-2010 at 10:12 AM.
|

22-10-2010, 09:26 AM
|
 |
amateur
|
|
Join Date: Jul 2006
Location: Mt Waverley, VIC
Posts: 7,105
|
|
I am also using program loops for delays/timing, when doing something in assembler (this is what I used for stepper driver for my EQ3 - very accurate, crystal controlled timing, as the execution lime for each instructions is known.. and processor (old one, Motorola KJ705) I am using does not have adequate timer).
However, those above-mentioned Delphi delays are funny... instead of milliseconds (what they are supposed to be), they are delaying things by seconds (or more.. I didn't figure out what is happening there). Even sleep(ms) is not quite accurate and it's longer (something to with multitasking in Windows?) but at least it is consistent and short enough to produce acceptable delays for stepper motor pulses duration.
Last edited by bojan; 22-10-2010 at 03:54 PM.
|

25-10-2010, 08:35 AM
|
 |
Meteor & fossil collector
|
|
Join Date: Jul 2005
Location: Bentleigh
Posts: 1,386
|
|
Quote:
Originally Posted by bojan
|
Yes, we had to use this for a project last year, it had to run super fast under DOS on an old 486 laptop. It took me a while to remember some of the differences, but it fixed the problem, and much quicker to develop than machine code!
|
Thread Tools |
|
Rate This Thread |
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +10. The time is now 03:42 AM.
|
|