Hello everyone,
I know there is a build in real time clock inside the computer
and I know it's possible to use this clock when you write a program on Assembler(using an interupt that I don't remember at the moment),is there a way to use the real time clock
while programing on VC++ ?
(I use VC++ 6.0 with the 6th service pack instaled)


Andrei

Dani AI

Generated

Brief expert summary and practical next steps.

As pointed out, standard C/C++ time functions do not give direct access to the machine's RTC and a normal Windows user‑mode thread cannot guarantee hard real‑time behavior. For reliable PWM at 1–few kHz the least brittle choice is to move the timing into dedicated hardware and use the PC only for high‑level control. An inexpensive microcontroller (or a small PWM controller chip) driven over a serial/USB link gives hardware PWM with deterministic timing and lets the VC++ app only send duty/frequency updates.

If staying on the PC for experiments is required, there are three realistic routes, listed by increasing complexity and determinism:

  • User‑mode, high‑resolution measurement and busy‑wait: set thread affinity to a single core, raise priority, call timeBeginPeriod(1) and use QueryPerformanceCounter for timing loops. This can work for short tests but will show jitter and missed deadlines under normal Windows load.
  • Kernel approach: write or use a kernel driver to toggle the hardware or program an onboard timer (HPET/APIC). This reduces scheduler jitter but requires driver development and careful testing.
  • External hardware/DAQ: use a USB/PCI data acquisition or a simple MCU board for clean PWM output. This is typically the fastest path to a robust result.

Quick test snippet to measure loop timing (replace WritePort/Log with the board/driver calls used in this thread):

// measure intervals in microseconds
LARGE_INTEGER freq, t0, t1;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&t0);
for (int i=0;i<10000;++i) {
    WritePort(toggleValue);               // call into driver or MCU comms
    QueryPerformanceCounter(&t1);
    long delta = (long)((t1.QuadPart - t0.QuadPart) * 1000000 / freq.QuadPart);
    Log(delta);                           // record for min/max/stddev
    t0 = t1;
}

Checklist and cautions: test and record jitter first; avoid PROCESS_REALTIME_PRIORITY_CLASS unless on a dedicated test machine; prefer MCU or dedicated PWM hardware for deployment. The suggestions from and are useful for prototyping, but true determinism follows from moving timing out of Windows.

Recommended Answers

All 18 Replies

Yes, you get clock functionality from library <ctime> (or maybe it's still <time.h> in VC++ 6)
See for a quick reference.

Check your MSDN help information in VC++ for what's actually supported in that compiler.

Consider moving into the 21st century with newer versions. VC++ 2008 Express is a freebie.

Regrettably, all <ctime/time.h> stuff does not bear a relation to the computer Real Time Clock device. Moreover, you can't directly access RTC in standard Windows user mode.

In what modes can I acces it?

In what modes can I acces it?

I think partially on device driver level or lower (HAL).

Did you need so low level really? The Windows without (as usually third-party and commercial) special software is not a real-time system. Your programs have no direct access to ALL hardware (except CPU in user mode ;) ) on Windows installations. Didn't you know that?

Why you need RTC direct access? May be Windows API or even RTL stuff is enough for your task?..

Well, ofcourse I know that from win 2000 and upwards the acces to the ports is blocked,
but I have a driver that opens the access(and I already tested it many times while working with the parallel port),so I do have access to the hardware.
I need a real time clock to generate accurate PWM for a robot...

It's not a true RTC access (even with RTC driver) because Windows threads sheduling adds unpredictable delays for your robot control process irrespective of its priority.
That's why Windows is not RT system...

For hi-res timers, you may want to have a look at the following (100us timer period):
http://www.codeproject.com/KB/system/RealTimeModule.aspx

I need a real time clock to generate accurate PWM for a robot...

What's the frequency of the PWM? Are you interfacing to RC servos?

Ok,first of all I want to thank everyone for their help,thanks everyone! :)

Now,dougey83, thank you for the program,did you try it? How accurate is it?
How much windows distortes the timers accuracy?(oh,and this time I use an H-bridge and regular DC motors,not servos,if it's possible I want to generate PWM with frequancies of 1 to few Khz).
ArkM,you didn't pay attention,I said a driver that gives me access to ports(in other words,it allows me to access hardware),not a RTC driver...

could you not use a Hardware clock to do the timing for you?

Chris

Arctic wolf, you didn't pay attention that I said about Windows thread sheduling. You can't garantee that your processing code continued immediatly after your wonderful driver gets data from the hardware port.

Arkm,I see your point,I agree.
But I guess there is a way to give the code first priority...
(I also need to look into the code that dougey83 gave,and see how did they solve that problem there,because if they didn't then it's no good...)

Chris,I didn't understand your question,every timing inside the computer eventualy comes from hardware,it's just that the standart timing functions of C++ are no good for my application...

What i saying is, Can you not make your own oscillator to controll timing?

Chris

Why Windows? Get DOS extender, WATCOM (freeware) compiler...

Have a microcontroller generate the PWM.
You can communicate with the microcontroller using the PC.

Ok,
Chris and Colin, you're missing the point,
ofcourse I can make my own hardware,and I actually plan to use the microcontroller after I finnish some tests using the PC, BUT the point of this test is to use the PC! :)
And by the way Chris,the PC has it's own build in oscilator(the proccessor uses it for timing itself),so basically there is no need to build another one,the problem is what Arkm discribed...

Arkm,I have an old verssion of Borland C that operates in Dos mode,
it's just that I wanted to build on a modern version of C++ and also to use the API to make a good GUI...

I understand that all my "advices" are totally "light-weight" ones (I never made robot control systems)...
About good GUI: remember wonderful videogames implemented in DOS extender environments. Look at SDL game library, for example.
Apropos, Open WATCOM C++ is a cross-compiler (free now) so it's possible to implement all stuff in comfort Windows environment.

I'm aware of that arctick wolf. But i was going down the lines of what Colin said. My fault for missing the bit that it must be done on PC. Sorry.

Chris

Arkm,I never said that your advices are no good!
Don't underestimate yourself!

I will check all the leads and if I'll have questions I'll come back :) ...

Thanks again to everyone.


A.

commented: +Points for exuding professionalism and respecting the individuals who gave you advice! =) +5
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.