Thread: custom pause

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    85

    custom pause

    Well, I bet this has been asked a million times before, but I haven't been able to find it.

    I'm looking for a delay. A pretty special delay, since it will generate pwm cycles on the parallel port.

    Sleep() tends to be bad, since windows doesn't manage timeshares pretty good. And it's pretty slow, too.

    I've got a dual core at 3Ghz, so I'm sure I can definitely pull out something more than 1ms (1khz). I'm aiming for 20khz, so it will be outside the audible spectra, since it will control stepper motors and they will make sounds

    I was thinking - a loop, but it's definitely going to be a big one. How can I 'calibrate' this loop, so that in a second it will give out x pulses on the parallel port? I know how to interface the port and generate the frequency and pwm, but I'm not able to find a method for computing the delay.

    Any ideas? Thanks.

    Edit: Oh, and I don't really mind if it will go to 100% cpu usage.

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    look up gettimeofday in <sys/time.h>

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Since you're using Windows, you can use the QueryPerformanceFrequency and QueryPerformanceCounter methods for more accurate timing (if the system supports a high resolution timer)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    85
    ... I don't seem to have it. This is really odd. I've reinstalled devc++ on a separate machine, and it's not in the includes of that, either.

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    85
    Grrr. I hate native windows functions. Lack of support, lack of documentation, the performance counter varies by system, but you have to guess its maximum value, they use their freaking custom types, which don't support any conversion or typecasting. How wonderful.

    Is there a way to obtain a counter more precise than 1 s in windows without using the dreaded windows headers?

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    If you write for an O/S, you really shouldn't run from its system calls. Learn how to get around MSDN (by way of google ).

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    hmm it's weird that gettimeofday is not defined in mingw. It is defined in GCC on Linux since it is part of the POSIX specification (http://www.opengroup.org/onlinepubs/...timeofday.html)

    a workaround is available here (replaces sys/time.h) -
    http://bioinformatics.org/molekel/wiki/Main/OpenBabel

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    gettimeofday is really a POSIX function. Getting it to work on Windows might be more trouble than it's worth.

    Functions that might be of interest too are the time* family, in particular timeGetDevCaps, timeBeginPeriod, timeEndPeriod, timeSetEvent and timeKillEvent. However, this timer is meant for MIDI, and very clearly so: you are not allowed to call anything except timeSetEvent, timeKillEvent, timeGetTime, timeGetSystemTime, PostMessage, OutputDebugString, midiOutShortMsg and midiOutLongMsg. Everything else is completely undefined and can even completely crash your system (if it's a 9x kernel).
    Besides, the resolution may still not be high enough for you. You'll have to test that.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    gettimeofday is really a POSIX function. Getting it to work on Windows might be more trouble than it's worth.
    That I don't quite understand. According to Wikipedia, Win32 is POSIX compliant.

    I personally try to avoid non-portable APIs, too, as I develop in both Windows and Linux, and it's hard to develop something that doesn't work =)

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It's only compliant if you install the Microsoft Services for Unix.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User
    Join Date
    Aug 2007
    Posts
    85
    I finally did it with the performance counter. It reaches about 100 khz (but phases are far from equal), and at this frequency, the output ICs from the motherboard start squeaking. i think it's a harmonic or something of that value.

    thing is, on my 3ghz intel, it gets the maximum frequency correctly. on my 800mhz amd, it gets something lower. quite odd.. i wonder if it's a specific case or not..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fork(), pause(), and storing PID's in a linked list
    By vital101 in forum C Programming
    Replies: 10
    Last Post: 09-28-2007, 02:16 AM
  2. Custom Allocation
    By SevenThunders in forum C Programming
    Replies: 17
    Last Post: 04-09-2007, 04:10 PM
  3. Stl sets and custom classes.
    By cunnus88 in forum C++ Programming
    Replies: 3
    Last Post: 05-12-2006, 11:58 PM
  4. Replies: 7
    Last Post: 04-13-2003, 10:53 PM
  5. Pause function ???
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 09-02-2001, 08:22 PM