Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

micro seconds delay in C++

Status
Not open for further replies.

hameeds01

Advanced Member level 2
Joined
Aug 23, 2005
Messages
599
Helped
75
Reputation
156
Reaction score
41
Trophy points
1,308
Location
Islamabad Pakistan
Activity points
4,540
c++ microseconds

i m doing programming in C++ & i m using Parallel Port for my project ,
what is the command for giving a delay in micro seconds

sleep(x) ; x in seconds
delay(x) ; x in milli seconds

what about micro seconds??????????
 

windows timers micro secounds

Hi, One trick that I always used in the past was the following:

Perform dummy read operations on the parallelport register (like reading input state of the port) this will give you about 0.5 us - 2us delay. depending on the platform and age of your platform.

so if you need 100us delay do 200 dummy reads !.

regards,

Paul.

PS: I am talking about real access to the printer port NOT through a windows driver !!.
 

c++ sleep microseconds

Hi

the problem of this method the processor spend time without any operation.
if there counter i can make it count and interrupt me at end of the periode?

thanks
 

delay c++

Hi,

for this small periods it isn't wothy to use interruts. the most you can use is a multithreaded application for timings larger than a few milliseconds. For smaller timings try to use the QueryPerformanceFreqency and QueryPerformanceCounter functions.
 

microseconds delay c++

Hi,

I think the best way to search the net for a timer code with micro second accuracy, and use it in your code with a busy wait algorithm.

Regards,
Shohdy
 

c++ code microsecond time delay

Hi Here some code that works on any PC platform. The software was written by me some years ago and uses an internal generated timing signal found on every PC The code is in inline asm for C. It only works on windows if you have direct access to this hardware. The timing signal is 15us pulses.

regards,

Paul.
 

delay in c microsecond

Hellom,
I used these to get microsecond delays

/***************************** delay.h *****************/
#ifndef __DELAY_H__
#define __DELAY_H__

class Delay
{
public:
Delay();
~Delay();
static void delaySeconds( float seconds );
static void delayMiliSeconds( float miliseconds );
static void delayMicroSeconds( float microseconds );
};

#endif
/***********************************************************/

/************************************ delay.cpp ****************/

#include "stdafx.h"
#include "delay.h"

Delay::Delay()
{

}
Delay::~Delay()
{

}

void Delay::delaySeconds( float seconds )
{
__int64 timeEllapsed;
__int64 timeStart;
__int64 timeDelta;

QueryPerformanceFrequency( (LARGE_INTEGER*)(&timeDelta ) );
QueryPerformanceCounter ( (LARGE_INTEGER*)(&timeStart ) );

__int64 timeToWait = (double)timeDelta * (double)seconds;

timeEllapsed = timeStart;

while( ( timeEllapsed - timeStart ) < timeToWait )
{
QueryPerformanceCounter( (LARGE_INTEGER*)(&timeEllapsed ) );

};
}

void Delay::delayMiliSeconds( float miliseconds )
{
__int64 timeEllapsed;
__int64 timeStart;
__int64 timeDelta;

QueryPerformanceFrequency( (LARGE_INTEGER*)(&timeDelta ) );

__int64 timeToWait = (double)timeDelta * (double)miliseconds/1000.0f;

QueryPerformanceCounter ( (LARGE_INTEGER*)(&timeStart ) );

timeEllapsed = timeStart;

while( ( timeEllapsed - timeStart ) < timeToWait )
{
QueryPerformanceCounter( (LARGE_INTEGER*)(&timeEllapsed ) );

};
}

void Delay::delayMicroSeconds( float microseconds )
{
__int64 timeEllapsed;
__int64 timeStart;
__int64 timeDelta;

QueryPerformanceFrequency( (LARGE_INTEGER*)(&timeDelta ) );

__int64 timeToWait = (double)timeDelta * (double)microseconds / 1000000.0f;

QueryPerformanceCounter ( (LARGE_INTEGER*)(&timeStart ) );

timeEllapsed = timeStart;

while( ( timeEllapsed - timeStart ) < timeToWait )
{
QueryPerformanceCounter( (LARGE_INTEGER*)(&timeEllapsed ) );

};
}
 

c++ delay microsec

The last post by bilgekaan is the proper way to do it on a Windows platform. It uses the internal 64 counter on the pentium that increments by one each clock cycle. If you use the Win Api calls in VB you have to use the decimal data type since it is a 64 bit integer type. On a 266 Mhz platform I could get a 3 us resolution making two successive calls to the QueryPerformanceCounter() routine. You have to remember that your program can be gone for several milliseconds in a multitasking environment, and heavy HD access can make your program disappear for 10's of milliseconds, so be aware of this.
 

time difference microseconds c++

Hi,
Never expect delays to be perfect. Delays as fine as microseconds is difficault to achieve. May be you can use gettimeofday(). It alleast returns time in microseconds. Using assembly code (like polling some port) will give smaller delays. So may be you should combine assebly code and a gettimeofday() call to check if the necessary sleep time is over or not. I am able to do it in Linux.
 

queryperformancecounter delay

hey lousinells

i am not able to find the gettimeofday() function

could u pls tell me which header file to include ?
 

micro second delay c++

Hi,
Be sure whether these headers are available in your platform. I have done this in Linux platform (Debian Sarge 3.1r2).

Code:
       #include <sys/time.h>
       #include <time.h>

       int gettimeofday(struct timeval *tv, struct timezone *tz);
       int settimeofday(const struct timeval *tv , const struct timezone *tz);
alvarorahul said:
hey lousinells

i am not able to find the gettimeofday() function

could u pls tell me which header file to include ?
 

delay microsecond c++

what about CPU speeds , wont it make a lot of difference in microseconds, and is it good enough for real time application.
 

delays in c++

Actually we are not making a precise delay. we are just making a smaller delay with some assembly instructions and keep-on checking whether the required delay period is over or not in a loop. In a normal CPU with fair speed this will work because assembly instructions can make delays of the order of few micro seconds. So this should work.
mehboob_iiui said:
what about CPU speeds , wont it make a lot of difference in microseconds, and is it good enough for real time application.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top