100 kHz timer frequency in Windows XP

There is a popular CNC machine tool that works in Windows XP, and it has a timer that works with a user selection frequency of 25 kHz to 100 kHz.

It so happened that I also create CNC machine controllers, but I used the DSP controller's motion control panel from Galil Motion Control Inc. DMC1800, which I control with my top-level program. There are potential buyers who cannot afford such a configuration, so I wrote another application that I plan to offer as a software-based management package. The application is completed except that I do not have a timer. (The MS multimedia timer is NOT sufficient.)

I pursued this for more than two years with no results, so I decided to ask for HELP. To avoid confusion, I mean a periodic timer that can operate at 100 kHz, not a stopwatch timer.

I would really appreciate help on this,

C4c

+6
windows winapi
source share
16 answers

Impossible. The fastest timer available on Windows is 1 ms (1 kHz). You will need a real-time operating system or a dedicated controller. Even in real-time, usually around 20 kHz, this is your maximum.

+10
source share

What exactly should this timer do? If he just needs to output a square wave between 25 kHz to 100 kHz, then you will need additional equipment, but not too expensive equipment. For example, look at the DT-340 from Data Translation. It can go even faster than you need, and I believe that it can even cause an interrupt. Or is it PCI 8255/8254 48 I / O.

If you can afford $ 500 or so on your budget, there should be many hardware options. The actual programmable timer chip should be much cheaper than this, so if you are ready to flip your own circuit and connect it to a computer, you can probably do a lot better than $ 500.

I once built a circuit around an Intel 8253 Programmable Interval Timer chip, which was used in the original IBM PC to generate audio signals from an internal speaker. It was connected to a 1.1393182 MHz chip and has a 16-bit divider. This chip has been around for decades. If you need more, I'm sure there have been improvements since then.

When I say that the 8253 (or 8254) chip inside the PC is used to drive the internal speaker, I'm not ready. Wikipedia has a lot more about Intel 8253 . It explains that output 0 is used to generate a clock interrupt, output 1 is used to synchronize DRAM time, and output 2 is used to control the internal speaker.

The first two synchronization outputs generate the interrupts you want, but the above descriptions of what these outputs are currently doing should give you some pause to reflect that changing their behavior may cause Windows XP to notice. The third output for the speaker is basically harmless for communication, but you will need to somehow redirect this signal to what it generates and interrupts if you need it.

Mach 3 probably uses the High Performance Event Timer (HPET), which according to here is part of the latest Intel chipsets. Timers 0 and 1 are used to emulate 8254.

"The ICH HPET Timer 2 interrupt can be routed to any of the following IRQs: 11, 20, 21, 22, and 23, through the TIM2_CONF Register. (Note: this register is called TIM3_CONF in Intel® ICH5.)"

+9
source share

You can get close to polling 100 kHz using the QueryPerformanceCounter in the current processor cycle.

For example:

typedef void ( *CallbackProc )(); void PollAtKhz( int khz, CallbackProc const &Callback ) { int hz = khz * 1000; LARGE_INTEGER li; QueryPerformanceFrequency( &li ); if( li.QuadPart < hz ) return; __int64 diff = li.QuadPart / hz; LARGE_INTEGER NextStop; LARGE_INTEGER CurStop; QueryPerformanceCounter( &NextStop ); NextStop.QuadPart += diff; while( true ) { QueryPerformanceCounter( &CurStop ); if( CurStop.QuadPart >= NextStop.QuadPart ) { Callback(); QueryPerformanceCounter( &NextStop ); NextStop.QuadPart += diff; } } } 

In my test, I got a frequency of 92-98 kHz, using this code to test it:

 void TestCallBack() { static int i = 0; static time_t LastTime = time( NULL ) + 1; ++i; if( time( NULL ) >= LastTime ) { printf( "%d / sec\r\n", i ); LastTime = time( NULL ) + 1; i = 0; } } 

Obviously, this code requires maximum CPU utilization, so SMP is required to get these results. In addition, real-time process / thread prioritization also helps.

+7
source share

Windows XP is not a real-time system, and I don’t think there is a way to guarantee timers in the 10 microsecond range. You can look in the Microsoft.Ccr.Core library, I think it will be closest to you.

Edit: I found additional information from Microsoft. According to How to use the QueryPerformanceCounter to Time Code article ,

 Function Units Resolution --------------------------------------------------------------------------- Now, Time, Timer seconds 1 second GetTickCount milliseconds approx. 10 ms TimeGetTime milliseconds approx. 10 ms QueryPerformanceCounter QueryPerformanceFrequency same 

and that in the context of an article that begins with a statement:

When the time code to determine the performance, you want to use the timer with the highest resolution the system can offer.

Robert Harvey is right, QueryPerformanceFrequency is in tens of microseconds, but I don’t think there is a way to use it to trigger events accurately. I think this makes it a complete bust for software-only solutions. Have you looked at any chips like Arduino that are pretty cheap? From the Arduino forums , it seems like 10 microsecond accuracy is possible.

+2
source share

The best thing you can do for Windows synchronization is QueryPerformanceFrequency / QueryPerformanceCounter . The lowest frequency with which I saw these products is 1.1 MHz, so it will provide a sufficient frequency.

But even with this, as the other answers pointed out, this will not work. A window is not a real-time operating system; it does not provide any time guarantees. It is not possible to verify that your code runs every 1 us (for 100 kHz), since Windows will change to other processes or kernel code.

You will need to find some kind of external hardware solution to get that kind of time.

+2
source share

When you say a periodic timer, I assume you mean a pulse generator?

Do you offer a timer that will generate an event every 10 microseconds? I doubt you should do this.

You can try to program the PC audio output and the interface that the instrument will use. You will have to mute / tune PC sounds while your instrument is running to prevent frequencies that would be false to the pulse flow.

Did you read the USB pulse generator? Or a cheap " USB sound card " dedicated to this operation?

+2
source share

Perhaps you could use some of these:

http://www.geisswerks.com/ryan/FAQS/timing.html

Look for QueryPerformanceFrequency, but maybe other solutions might be helpful.

+1
source share

So far, I suspect that the meomaxy solution is the best ...

If you need a high-resolution phase difference, Intel has a code here that claims accuracy down to the nanosecond. (There is also a good discussion of the general possibilities of differential time).

+1
source share

Well, after reading the Mach 3 documentation (well written, informative, and even funny), I think I see what is happening. First, understand that there is a difference between having a high-frequency user space event trigger and a kernel interrupt routine. The kernel interruption procedure should be fast and therefore should not contain a lot of logic, therefore we usually say that it is impossible to have a generalized callback in your program that can respond to events at frequencies> 1 kHz. In addition, operating systems use local timers for scheduling, which decide when user programs can start, and as a rule, the scheduler speed does not exceed 1 kHz, so it is impossible to write a user space program that responds faster than this.

However, in this case, you do not need to have complex user-space logic that responds at this speed, since the goal is only to modulate the output at high frequency to obtain a PWM signal. It would be possible to create a kernel interrupt handler for APIC , which has a high priority and runs at a high frequency, if it just does very simply such as switching the output to the parallel port.

So, if you want to do this, I suggest you read about kernel programming and use APIC, and you can achieve something useful for you. However, a few tips:

  • Distributing a driver with your software is a pain in the ass for both the developer and your users who need to install a driver that may or may not work. (The hardware is comprehensive, and if your decision depends on very specific computer configurations, your mileage may vary.)
  • The presence of such a high-frequency interrupt will cause the computer to start up and make everything very hot. You can get stalls or blue screens. Be prepared for these troubles.
  • If you manage to switch the LPT contact in response to the APIC timer, I suggest connecting a acquisition card or digital oscilloscope to measure not only frequency but also jitter. Any time-programmed signal tends to have more jitter, so keep an eye out for that. Jitter effectively increases the noise in your signal. In addition, jitter tends to remain constant, so at low frequencies this may not be a problem, but at high frequencies jitter can be a large percentage of your total period, significantly reducing the signal-to-noise ratio.
  • If your goal is simply to create a PWM or pulse signal to control stepper motors, having a bit of onboard equipment is really the best way. I will return these few points:
    • First, if you are reading Mach 3 documents, they contain a section on how to electrically isolate your parallel computer port in case of electrical problems. In other words, even if you use the built-in LPT port, some external equipment is recommended in any case for safety. If you intend to do this, you can also turn on the $ 2 timer chip.
    • Since you generate a high-frequency signal, but only control this signal at a lower speed, there is no need to start the computer with the task of actually generating the signal itself - this is what timers are for.
    • A $ 5 low-income AVR microcontroller can talk to a computer via a USB port and generate a PWM signal. This is a much less expensive solution than buying a fee for $ 100, but it depends on your needs. (For example, how many signals, etc.) For example, buy a Teensy and simply plug it into the USB port of your computer and write a 5-line program to control the 7 preset PWM outputs. Done and done!
    • If you do an Internet search for "PWM parallel", you will find many tutorials on how to generate PWM signals using the parallel port, but note that 99% of them are actually tutorials on how to manage the built-in timer for the parallel port rather than how to directly generate the actual PWM signal.

Finally, here are some documents you might find useful:

+1
source share

Windows multimedia timers can give you 1 ms.

0
source share

The main problem with using a PC to control traffic without dedicated equipment is the data transfer speed of the various outputs built into most PCs. If your customers do not have a budget for the top motion controller that you are talking about, then they simply will not get access to 100 kHz signals.

The I / O controllers for each port provide some buffering, so you get smooth results from them. This largely serves the purpose of making the software easier to “beat” these signals, and also makes the I / O frequency more stable.

The disadvantage is that buffering is likely to lead to some latency in the system, which can affect the closed loop control circuitry through control in software. Another disadvantage is that it limits your frequency parameters. One of the fastest PC I / O options that does not provide high-level flow control, such as USB or SATA, is actually an audio port, but on most systems it is limited to 64 kHz or 44 kHz.

If the audio port is your option, you don’t have to worry about timing at all, just make sure you set the frequency correctly and produce your signal like otherwise.

If you need to use a digital signal, the parallel and serial ports work similarly , but at a lower maximum speed.

EDIT Hmm ... It seems that parallel ports are a little faster than before. Advanced Parallel Port (EPP) is able to provide you with up to 2 bits of binary band, which should be within reach of your target. However, due to the way the parallel interface works, the data rate depends on the periphery, and not on a specific predetermined buffer speed. In fact, the peripheral device recognizes that it has received data or that it is ready to transmit data for each byte transmitted.

This is not so bad, because a simple peripheral device with clock synchronization is easy to configure.

In addition, data cannot flow in both directions at the same time, data coming from a peripheral device will block the data flow from the host PC. A possible way to get around this is to simply arrange access to two parallel ports or organize a peripheral device to limit its data transfers to each other cycle.

I / O on such a port is usually interrupted. In addition to several error conditions, the most useful interrupts are generated when the input or output FIFO reaches a certain level. You can use this interrupt to write (or read) new data to the port. The threshold can be set anywhere from 1 to 16 bytes. You must carefully test (using a logic analyzer / oscilloscope) that the data is indeed flowing at the expected speed, at the expected speed. You can do this almost the same in software using the QueryPerformanceCounter .

0
source share

I would say that this is impossible without writing your own driver.

If you wrote your own driver, you could grab the HPET timers (not available on all machines, but come with most newer ones) and maybe the APIC timer (not sure if this can give you 100 kHz) and get 100 kHz from there. This is quite complicated, so you will have to do a lot more research.

In addition, setting up your application in real time should help, even if it engulfs your CPU usage.

0
source share

To all respondents

I tried to make it clear that I was not interested in measuring the elapsed time, but most of the answers were just that.

Apparently, I could not adequately convey my thought, so I will try again. I want to create events that happen 100,000 times in 1 second with a processor price of about 12%.

The program I referenced, Mach 3, generates a frequency of 108,000 when the user selects 100,000, but the frequency is very stable. I mention that to illustrate that the accuracy of the actual time interval is not critical, as long as the error in the time interval is consistent.

I will say again that Mach 3 works with a processor price of about 12%. A frequency of 108,000 is emitted on the parallel port pin, where it can be checked with the scope I made.

Other respondents somewhat understood what I wanted to do, but their example was to measure the number of cycles that could occur over a period of 1 second, recognizing that the cost of the processor would be unacceptable.

The general consensus was that what I want to do is impossible. For those who agree with this opinion, I would advise you to Google Mach 3 CNC , download a free copy of the program and then install it. Run the program, use the tabs to go to the diagnostics page, which you can see for yourself that this is really possible. First, the program will start operating at a default frequency of 25 kHz. You can change the frequency by opening the "Config" / "Ports and Pins" / menu, then select a new frequency and run the program again.

Forums like this have a large number of posts related to this subject, and the answers, at best, match what I received. I gave an example that clearly shows that it is POSSIBLE, so let's find out how Mach 3 performs this task.

C4c

0
source share

Recently, I have been working with an CNC router using the real-time Linux kernel; the problem you see is the lack of real-time proactive use in Windows XP. I can't view Mach3 stuff from here, but the only way it should work is to use the kernel level driver in windows. (sorry, I'm not really a person on Windows, so I don't know the specifics). Writing some kind of driver to bypass the default Windows preemption model is your answer, although I would start by looking at a bit about Windows drivers and maybe look at the real-time fixes for the linux kernel for reference.

http://www.kernel.org/pub/linux/kernel/projects/rt/

0
source share

Could you use something like IntervalZero RTX. It is not free, it is a commercial product. It is written for real-time presentation on Windows (XP, etc.). It has very fast timers. according to this, paper RTX has timers with a value of 1 microsecond (whereas Windows is 1000 microseconds)

Not sure what price \ licensing is. I tried the eval version.

0
source share

It looks like you need a 555 timer (or crystal), an AND gate, and a parallel port (or a USB digital I / O device). You could fake a DSP sound card to do something like that, but I wouldn't bet on it.

Any timer that you can build on windows that will start at the required speed will be associated with the CPU. This does not mean that the computing processor cannot handle this, but you should think about the rest of the moving parts on your computer. To get a reliable 100 kHz signal from software emulation to hardware I / O signaling on standard equipment is a rather difficult task. You have connected the core I / O to get the correct signal pattern.

Several options may be to create a feedback loop for the serial port, and then play with bit patterns and signal types to get a fairly stable signal. But it will be much better for you to service either the assembly or the purchase of a digital control board, which is designed to control the CNC machine. If you look, most of the hardware associated with this CNC Mach 3 refers to control cards that accept USB, RS-232, or LPT ports.

(Oh, and telling people that they’re wrong, instead of having a constructive comment ... also don’t win your fans)

0
source share

All Articles