Help!: problem with fast timing for real-time application on PC 
Author Message
 Help!: problem with fast timing for real-time application on PC

Hello there,

A friend of mine has a programming problem, which he hopes someone can
suggest a solution for. Since he does not have Internet access, I am posting
this message on for him. Here is the problem:

He wants to make on his PC a real-time application for
which quite accurate timing is needed. The normal speed of the system timer
(= 1/18.2 sec) is not fast enough so he decided to speed it up. This works
fine but it has some unwanted side effects, for instance the screen blanker
is activated every 18 seconds.

The solution he thought of is to redirect the interrupt to his own handler,
which forwards the interrupt only once every x times that the interrupt is
called. The effect would be that the rest of the handlers think the timer
operates at normal speed. The problem, however, is that if you don't forward
the interrupt the computer crashes. The code works well on interrupt 1Ch
(= the software timer interrupt) so that can't cause the trouble. In sum,
my friend has no clue as to what goes wrong.

Enclosed below is the program he made. It is written in Borland C++.

Any solution to this problem would be greatly appreciated by my friend. If
someone knows another way to get a more accurate timing he'll be happy too,
as long as it doesn't include any hardware modifications. Please e-mail
any suggestions to me.

Greetings,
Stefan Brands

----------------------------TIMER.HPP-----------------------------------

/*
        TIMER :
                This library speeds up the timer by a factor of 'TIME_ACC'.
                You initialize the timer through 'set_timer()'. Use 'clear_timer()'
                to reset it to its original value. ALWAYS do this because if you
                don't the computer will crash. 'get_timer()' gives you the number
                of clock ticks since you started the timer and with 'ticks_2_sec()'
                you can convert these ticks to seconds.
                WARNING: The program does not register an overflow of the clock
                                 ticks. So after (2^32)/(18.2 * TIME_ACC) seconds the clock
                                 returns to zero.
*/

#ifndef __TIMER__
#define __TIMER__

#include <dos.h>

/*  This defines the multiplyer of the timer.
        The value must be a divisor of 65536 */
#define TIME_ACC 16

void set_timer(void);
void clear_timer(void);
long int get_timer(void);
float ticks_2_sec(long int clicks);

#endif

--------------------------TIMER.CPP-----------------------------------

#include "timer.hpp"
/* The clock tick interrupt */
#define INTR 0x08

#ifdef __cplusplus
        #define __CPPARGS ...
#else
        #define __CPPARGS
#endif

long int no_ticks;
int counter;

void interrupt ( *oldhandler)(__CPPARGS);

void interrupt handler(__CPPARGS)
{
        no_ticks++;
        counter--;
        if (counter == 0)
        {
                oldhandler();
                counter = TIME_ACC;
        }

Quote:
}

void set_timer(void)
{
        unsigned int counter_div = 65536l / TIME_ACC;

        no_ticks = 0;
        counter = TIME_ACC;

/* get the address of the current clock tick interrupt */
        oldhandler = getvect(INTR);

/* install the new interrupt handler */
        setvect(INTR, handler);

/* set counter 0 (= system timer) to read/write */
        outp(0x43, 0x36);

/* write the counter divisor */
        outp(0x40,(unsigned char)(counter_div & 255));
        outp(0x40,(unsigned char)(counter_div >> 8));

Quote:
}

void clear_timer(void)
{

/* set counter 0 (= system timer) to read/write */
        outp(0x43, 0x36);

/* set counter divisor to the normal value*/
        outp(0x40,0);
        outp(0x40,0);

/* restore interrupt vector */
        setvect(INTR, oldhandler);

Quote:
}

long int get_timer(void)
{
        return no_ticks;

Quote:
}

float ticks_2_sec(long int ticks)
{
        float div = 18.2;

        return ((float)ticks)/(div * TIME_ACC);

Quote:
}

--
Stefan Brands,
------------------------------------------------------
CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands



Tue, 29 Apr 1997 06:48:11 GMT  
 Help!: problem with fast timing for real-time application on PC
S> He wants to make on his PC a real-time application for
S> which quite accurate timing is needed. The normal speed of the system timer

If you need read time, one microsecond accuracy is available.
The method was discussed many times here. Lastly few weeks ago.



Mon, 05 May 1997 20:18:01 GMT  
 Help!: problem with fast timing for real-time application on PC

Quote:
> If you need read time, one microsecond accuracy is available.
> The method was discussed many times here. Lastly few weeks ago.

Some of us weren't here a few weeks ago. Is it in the FAQ? :)

-- Andy

+-------------------+-------------------------------------------------+
|  Andy Mortimer    |  "A mathematician is a device for converting    |

+-------------------+-------------------------------------------------+



Wed, 07 May 1997 04:06:22 GMT  
 Help!: problem with fast timing for real-time application on PC


Quote:
> > If you need read time, one microsecond accuracy is available.
> > The method was discussed many times here. Lastly few weeks ago.

> Some of us weren't here a few weeks ago. Is it in the FAQ? :)

There's a set of microsecond-resolution timing functions for PC's in
SNIPPETS, implemented as analogs of the standard clock() and related
functions. Look up Uclock.C/H.

-------------------------------------------------------------
MicroFirm: Down to the C in chips...
Home of SNIPPETS - Current release: SNIP9404.ZIP/LZH/ARJ/etc.
FidoNet 1:106/2000.6



Thu, 08 May 1997 18:35:55 GMT  
 Help!: problem with fast timing for real-time application on PC

Quote:

> He wants to make on his PC a real-time application for
> which quite accurate timing is needed. The normal speed of the system timer
> (= 1/18.2 sec) is not fast enough so he decided to speed it up. This works
> fine but it has some unwanted side effects, for instance the screen blanker
> is activated every 18 seconds.
> Any solution to this problem would be greatly appreciated by my friend. If
> someone knows another way to get a more accurate timing he'll be happy too,
> as long as it doesn't include any hardware modifications. Please e-mail
> any suggestions to me.

You could speed up the timer, but that complicates things. Most machines
have a high speed timer which runs at 1024Hz instead of the normal 18.2Hz,
and triggers Int 70h instead of Int 8. (This is used by the BIOS to
implement delays and event waits (Int 15h, Function 86h and 83h), with an
accuracy of 976 microseconds.) The only problem with this is that some
BIOSes (like mine) only activate this timer while a delay or event wait is
happening. If this is the case, you can start an event wait (Int 15h,
Function 83h) with some very long time period (up to about an hour is
possible, then you just retrigger it!). Have a look in Ralf's Interrupt
list for details about the event wait function.

-Dale

This is my .sig.
Hope you like it.



Thu, 08 May 1997 21:38:12 GMT  
 Help!: problem with fast timing for real-time application on PC

Quote:

> The solution he thought of is to redirect the interrupt to his own handler,
> which forwards the interrupt only once every x times that the interrupt is
> called. The effect would be that the rest of the handlers think the timer
> operates at normal speed. The problem, however, is that if you don't forward
> the interrupt the computer crashes. The code works well on interrupt 1Ch
> (= the software timer interrupt) so that can't cause the trouble. In sum,
> my friend has no clue as to what goes wrong.

Oh yes, I forgot to mention in my previous followup, if you speed up the
normal timer and then only forward interrupts at the appropriate rate, for
each interrupt which you *don't* forward you must send acknowledgement to
the hardware yourself (normally the interrupt handler takes care of this)
otherwise it will crash (the hardware won't allow another interrupt until
you acknowledge the first). To acknowledge the interrupt, send the value
20h to port 20h. This is probably the easiest way:

  outportb(0x20, 0x20);

-Dale

This is my .sig.
Hope you like it.



Thu, 08 May 1997 21:39:04 GMT  
 Help!: problem with fast timing for real-time application on PC

Quote:

>> If you need read time, one microsecond accuracy is available.  The
>> method was discussed many times here. Lastly few weeks ago.

A> Some of us weren't here a few weeks ago. Is it in the FAQ? :)

I don't know if there is any FAQ for this newsgroup.
If someone is preparing any, I can write the answer for it.
But I'm not interested in answering the question every week.



Tue, 13 May 1997 00:47:16 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Help!: problem with fast timing for real-time application on PC

2. Time to REAL & REAL to time

3. Real-time ST applications, any problems?

4. Bug in time.c (was Time.times problems)

5. looking for fast real-time measurement board

6. real time dispaly of time.

7. DOS compressed date/time into real date/time

8. real time, time series display

9. Fast, Precise Timing Applications (NMR, NQR)

10. Real-time sound synthesis on PC?

11. Use of APL in real-time applications

12. ST for real time applications!!

 

 
Powered by phpBB® Forum Software