0

I want to test a code which is designed for running on a Microcontroller and it is platform independent.

Problem is I currently don't have any Embedded simulator (due to some damn reason!) and I want to test it... I am using Visual Studio 2008 (What a Pity!!).... Code is entirely in C but as VS 2008 doesn't support C, I have stored files as .cpp.... But No C++ concept is used!!

What all I require is a Timer which would generate Interrupt after every 1 millisecond!

And One more thing.... Can I configure something so that an Interrupt will be generated if I press certain key on Keyboard....

I don't want to make GUI. I searched for Timers and all were for MFC application.... If there isn't any option, I will make a rough MFC...

Regards!!

Swanand
  • 4,027
  • 10
  • 41
  • 69
  • What do you base your assertion on, that Visual Studio doesn't support C? – Moo-Juice Nov 30 '10 at 13:55
  • @Moo-Juice: That's what all the kids at the WHC (windows haterz club) say. – John Dibling Nov 30 '10 at 14:02
  • possible duplicate of [How to create timer in WinApi (C++)?](http://stackoverflow.com/questions/2128620/how-to-create-timer-in-winapi-c) – John Dibling Nov 30 '10 at 14:02
  • Visual studio 2008 (and 2010) does support c-only programs ! It just don't support a wizard for .c sources ,but that is easely solved by supply a .c extension (also main.cpp needs to be renamed to main.c) – engf-010 Nov 30 '10 at 14:26
  • You most certainly won't get a resolution of 1ms - the best I achived using TimerQueue API mentioned by Dean on a reasonably potent machine was ~5ms. Also since Windows is not nearly real-time capable expect some serious jitter in your timing. Don't forget to use timeBeginPeriod() and timeEndPeriod() otherwise even the "modern" timer queues will be very inaccurate. – Arne Nov 30 '10 at 14:38

2 Answers2

0

For high precision timers the your best bet is probably CreateTimerQueueTimer (not used personally).

Personally I used to use timeSetEvent but that was way back years and years ago.

Dean Taylor
  • 40,514
  • 3
  • 31
  • 50
  • @Dean: "This function is obsolete. New applications should use CreateTimerQueueTimer to create a timer-queue timer." – John Dibling Nov 30 '10 at 14:04
  • 1
    And for "high precision" timers, what you *really* want is QueryPerformanceCounter() – John Dibling Nov 30 '10 at 14:06
  • @John It was a long time ago when I used high-precision timers, used to have to write for Win95, so John you may well be right. – Dean Taylor Nov 30 '10 at 14:11
  • @Dean: Things have evolved more than a little in the Windows world in the last 15 years. :) – John Dibling Nov 30 '10 at 14:14
  • @John Yeah, well with the times - unfortunately no need to play with low level API's anymore, gone are days of writing windows Assembly calls and kernel level drivers. Ahh the things I used to enjoy. :-) – Dean Taylor Nov 30 '10 at 14:24
  • Seems timeSetTime is still used by the Media SDK team, or at least it was 2009/07: http://blogs.msdn.com/b/mediasdkstuff/archive/2009/07/02/why-are-the-multimedia-timer-apis-timesetevent-not-as-accurate-as-i-would-expect.aspx – Dean Taylor Nov 30 '10 at 14:31
  • @John: QueryPerformanceCounter is indeed very accurate but I have not yet found a way to let it generate a callback as requested by the original poster. I my understanding it's a mere measuring device. – Arne Nov 30 '10 at 14:40
  • 1
    @Arne: QPC is indeed just a measuring device. If you want to use it to generate interrupts, you would need to write that functionality. – John Dibling Nov 30 '10 at 15:12
  • 1
    @Dean: Indeed, but timeSetTime has very poor accuracy. You want an interrupt every millisecond, but you'll only get an average of one interrupt per millisecond at best. If you need accuracy and precision in the millisecond or below range, you need QPC – John Dibling Nov 30 '10 at 15:14
  • @John Oh yeah and QPC is actually considered buggy (documentation vs. real world functionality) http://www.virtualdub.org/blog/pivot/entry.php?id=106 and http://blogs.msdn.com/b/oldnewthing/archive/2008/09/08/8931563.aspx – Dean Taylor Nov 30 '10 at 15:38
  • @John A few +1's for your worth while comments. :) – Dean Taylor Nov 30 '10 at 15:50
  • @Dean: Didn't follow the links, but they probably refer to problems that can arise on multi-cpu machines. Whether or not this can be considered a "bug" is debatable, but you're right in that it is something to be aware of. FWIW, I have never seen this problem come up in the real world. I write high-perf servers that run on 32-way boxes. My finding is that if your box is running the same chip (model + stepping) you wont have a problem. – John Dibling Nov 30 '10 at 15:52
0

You can try the Boost Asio Timers too. If you don't have it setup, please refer here: Getting Started on MS Windows.

yasouser
  • 5,113
  • 2
  • 27
  • 41