[comp.sys.ibm.pc] millisecond timing

martin@epistemi.UUCP (Martin Shepherd) (07/09/87)

Does anyone out there know anything about using an XT-compatible PC
(probably Elonex PC 88 Turbo) for applications involving millisecond
timing?  I ask because I would like to be able to use a machine like
this for psychological experiments which involve presenting simple
stimuli (single words, symbols, etc.) on the monitor and timing
subjects' keypress responses to the nearest one or two milliseconds.
The keys could be on the keyboard, but being able to hook up a couple
of morse keys would be useful.  I need to be able to "blank" and "unblank"
the monitor screen so that the onset of a display is more or less
instantaneous.

This might seem like a strange thing to be doing with a PC but my
resources are very limited and I want to be able to do word processing
and reaction time experiments on the same machine.

Any comments/ideas?

Martin Shepherd.

perkins@bnrmtv.UUCP (Henry Perkins) (07/14/87)

In article <1@epistemi.UUCP>, martin@epistemi.UUCP (Martin Shepherd) writes:
> I ask because I would like to be able to use a [PC Clone]
> for psychological experiments which involve presenting simple
> stimuli (single words, symbols, etc.) on the monitor and timing
> subjects' keypress responses to the nearest one or two milliseconds.
> I need to be able to "blank" and "unblank"
> the monitor screen so that the onset of a display is more or less
> instantaneous.

Your accuracy can't be better than 1/30th second (color monitor)
or 1/25th second (monochrome monitor), because that's how long it
takes to refresh a screen image.  You've got further problems with
standard monochrome monitors because they use slow-fade phosphors
-- it can take most of a second for an image to fade away.  Color
monitors will let you change the image completely in the refresh
period, so they're much better suited to your application.

You probably could re-program the timer chip to interrupt more
frequently than the standard 18.2 times per second if you wanted;
however, you'd have to replace the normal timer interrupt service
routine with one of your own creation -- you'd want less overhead,
and your system clock wouldn't keep time if the interrupt rate
were changed.
-- 
{hplabs,amdahl,3comvax}!bnrmtv!perkins        --Henry Perkins

It is better never to have been born.  But who among us has such luck?
One in a million, perhaps.

izumi@violet.berkeley.edu.UUCP (07/14/87)

[eat]
Someone recently asked about ways to do timings with 1 millisecond
accuracy on an XT or compatibles.  Here's what I think is possible.

If you haven't bought a computer yet, and AT or compatibles is an option,
you can probably do the timing at near 1 msec accuracy without adding
any hardware.  Otherwise, on XT, you will have to do something quite a bit
more involved.

First, on AT, there is already a piece of hardware that can do the timing
with the accuracy and resolution of about 1 msec (1/1024 sec).  Because
this hardware can generate interrupts at the preprogrammed rate of 1024Hz,
and this rate is not dependent on the CPU clock frequency, the same program
will give you the same results on all versions of the AT and compatibles.
Interrupt at 1024Hz can be generated on ATs by non-volatile (w/ battery b.u.)
Real-time clock/ CMOS RAM chip.
The information on this is covered on the AT reference around page 1-46,
system board section.  (I/O address of that chip is 70 and 71 hex).

To activate interrupt from this chip (RTC), you have to first enable
"Periodic Interrupt Enable" bit of Status Register B of the clock chip.
Done by,

	out 70h,0bh		;address Status Reg B
	in  al,71h		;read register
	or  al,40h		;bit-6 is the PIE
	out 70h,0bh
	out 71h,al		;write back

etc.

This will activate Hardware interrupt INT 70h (IRQ8) to the
second PIC (interrupt controller chip) in the AT.
As with regular interrupt you have to un-mask the bit-0 (IRQ8) of the
IMR in the second PIC 8259 whose I/O address begins at 0a0h.

There is an interrupt service routine to handle this interrupt (RTC_INT)
in the  BIOS listing. see pages BIOS2 5-161 on.  You will see there
what has to be done during interrut servicing.  Note that you have to
issue EOI (end of interrupt) to both PICs (8259s).
Since this ISR routine is short, doesn't do anything particularly important
, you can entirely take over the interrupt vector INT 70h (By the way, BIOS's
INT 50 is a typo,  I suppose).
    ^^
You can replace this interrupt service routine with your own to do the
timing at near 1 msec accuracy.

Unfortunately, I have not programmed this chip to do the interrupt at 1.024
kHz, so there may be some more details that have to be worked out.  But, I am
pretty sure it can be done.   Indeed, I remember seeing an article on the
use of this interrupt in PC Tech Journal about a year (?) ago. (I forgot
which issue).

Now, if you MUST use XT or compatibles, and want to do it without timing
loops which is affected by the CPU clock frequency, you will have to 
add an plug in adapter card which has a timer or hardware interrupt 
circuitry on it.  One example is Scientific Solutions (spawned from
Tecmar) LabMaster (This has the whole analog I/O stuff on it and probably
an overkill for your purpose.).

If you want to do it without buying these things, you can probably
use your spare printer adapter to do the job.
IBM PC printer adapters can generate interrupts IRQ7 (or IRQ5 ?).
So, if you have a printer port that you don't need, you can
connect an 1 kHz oscillator (TTL level) to ACK line of the printer port.
Then, put proper interrupt service routine to int vector for IRQ7,
enable interrupt on the printer adapter, and on the 8259 PIC (interrupt
controller).  Since there are a few more input lines on the printer
adapter, you can probably use them for buttons pressed by subject. 
 
Hope this helps.

Izumi Ohzawa
izumi@violet.berkeley.edu
(415) 642-6440

ralf@b.gp.cs.cmu.edu (Ralf Brown) (07/15/87)

In article <4343@jade.BERKELEY.EDU> izumi@violet.berkeley.edu () writes:
>Someone recently asked about ways to do timings with 1 millisecond
>accuracy on an XT or compatibles.  Here's what I think is possible.
...
>Now, if you MUST use XT or compatibles, and want to do it without timing
>loops which is affected by the CPU clock frequency, you will have to 
>add an plug in adapter card which has a timer or hardware interrupt 
>circuitry on it. [...]

It is possible to get timing loops which are independent of CPU speed (well,
almost--they can't handle changing clock speed in the middle).  Turbo Pascal
does this to get the DELAY function to work regardless of clock speed, and
be pretty close to exact.  The trick is to run a timing loop for exactly
the interval between two clock ticks, and count the number of times through
the loop.  Divide by 55.nnn to get the number of times through the loop for a 
1-millisecond delay.

In pseudocode,
	take over clock interrupt
	at next clock tick, start timing loop
	at next clock tick, end timing loop
	restore clock interrupt
	see how many times we made it through the loop

Hope this helps!


	
-- 
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
ARPA:  RALF@B.GP.CS.CMU.EDU               USnail: Ralf Brown
AT&T:  (412) 268-3053 (school)                    Computer Science Department
                                                  Carnegie-Mellon University
DISCLAIMER?  Who ever said I claimed anything?    Pittsburgh, PA 15213
"I do not fear computers.  I fear the lack of them..." -- Isaac Asimov

todd@uhccux.UUCP (The Perplexed Wiz) (07/15/87)

In article <62@b.gp.cs.cmu.edu> ralf@b.gp.cs.cmu.edu (Ralf Brown) writes:
>It is possible to get timing loops which are independent of CPU speed (well,
>almost--they can't handle changing clock speed in the middle).  Turbo Pascal
>does this to get the DELAY function to work regardless of clock speed, and

I haven't tried it but there is an assembly language routine in the
Feb 1987 issue of "Behavior Research Methods, Instruments, & Computers"
that claims to provide 1msec timing accuracy on an IBM PC.

	Graves, Roger, & Bradley, Ron (1987).  Millisecond interval
		timer and auditory reaction time programs for the
		IBM PC.  Behavior Research Methods, Instruments,
		and Computers, 19: 30-35.

...todd

-- 
Todd Ogasawara, U. of Hawaii Center for Teaching Excellence
UUCP:		{ihnp4,seismo,ucbvax,dcdwest}!sdcsvax!nosc!uhccux!todd
ARPA:		uhccux!todd@nosc.MIL
INTERNET:	todd@uhccux.UHCC.HAWAII.EDU

herve@titn.TITN (Herve Siegrist) (07/17/87)

It looks to me that there is a much better way to control timings on
an AT, as the BIOS provides some neat functions for that. All this 
is in the interrupt 15H for the cassette I/O. Two timing functions
are provided, the function code beeing provided in AH:

-1/ Function 86H (wait):

	Set AH to 86H, (CX,DX) to the number of microseconds to wait, and
	call INT15H. The call will return when the requested number of
	microseconds elapsed.

-2/ Function 83H (event wait):

	Set AH to 86H, (ES:BX) to the address of a memory byte, (CX,DX)
	to the requested number of microseconds. Also set AL to 0 to set
	the interval or to 1 to cancel the previous request, and call INT15H.

	This function returns right away. When the delay expires, the 
	high order bit of the byte whose address was provided in (ES:BX)
	will be set by the BIOS.

This provides a nice way to get timing information with a microsecond 
resolution. I should say however that I never tried to use that myself...

FROM:	Herve Siegrist, TITN Inc.  24301 Southland Dr. Suite 200
	Hayward, CA 94545	Tel: (415) 785-5970
UUCP:	(decvax|ucbvax|ihnp4)!decwrl!sun!dlb!plx!titn!herve

ralf@b.gp.cs.cmu.edu (Ralf Brown) (07/21/87)

In article <182@titn.TITN> herve@titn.TITN (Herve Siegrist) writes:
>
>It looks to me that there is a much better way to control timings on
>an AT, as the BIOS provides some neat functions for that. All this 
>is in the interrupt 15H for the cassette I/O. Two timing functions
>are provided, the function code beeing provided in AH:
[details omitted]
>This provides a nice way to get timing information with a microsecond 
>resolution. I should say however that I never tried to use that myself...
>
>FROM:	Herve Siegrist, TITN Inc.  24301 Southland Dr. Suite 200
>	Hayward, CA 94545	Tel: (415) 785-5970
>UUCP:	(decvax|ucbvax|ihnp4)!decwrl!sun!dlb!plx!titn!herve

Unfortunately, though the docs say microseconds, the actual resolution is only
977 microseconds, as these functions use the 1024/sec interrupts from the
real-time clock.  Thus the actual delays get rounded up to the next highest
multiple of 977 (plus some overhead in the BIOS code).


-- 
-=-=-=-=-=-=-=-=-=-= {harvard,seismo}!b.gp.cs.cmu.edu!ralf =-=-=-=-=-=-=-=-=-=-
ARPA: RALF@B.GP.CS.CMU.EDU                  USnail: Ralf Brown
BIT:  RALF%B.GP.CS.CMU.EDU@CMUCCVMA                 Computer Science Department
AT&T: (412) 268-3053 (school)                       Carnegie-Mellon University
DISCLAIMER?  Who ever said I claimed anything?      Pittsburgh, PA 15213
"I do not fear computers.  I fear the lack of them..." -- Isaac Asimov

ins_bjjb@jhunix.UUCP (Jared J Brennan) (07/22/87)

In article <2169@bnrmtv.UUCP> perkins@bnrmtv.UUCP (Henry Perkins) writes:
>In article <1@epistemi.UUCP>, martin@epistemi.UUCP (Martin Shepherd) writes:
>> I ask because I would like to be able to use a [PC Clone]
>> for psychological experiments which involve presenting simple
>> stimuli (single words, symbols, etc.) on the monitor and timing
>> subjects' keypress responses to the nearest one or two milliseconds.
>> I need to be able to "blank" and "unblank"
>> the monitor screen so that the onset of a display is more or less
>> instantaneous.
>
>Your accuracy can't be better than 1/30th second (color monitor)
>or 1/25th second (monochrome monitor), because that's how long it
>takes to refresh a screen image.  You've got further problems with
>standard monochrome monitors because they use slow-fade phosphors
>-- it can take most of a second for an image to fade away.  Color
>monitors will let you change the image completely in the refresh
>period, so they're much better suited to your application.
>
>You probably could re-program the timer chip to interrupt more
>frequently than the standard 18.2 times per second if you wanted;
>however, you'd have to replace the normal timer interrupt service
>routine with one of your own creation -- you'd want less overhead,
>and your system clock wouldn't keep time if the interrupt rate
>were changed.

   Well, since I've been programming in the psychology department here at
Hopkins doing just that, I thought perhaps I'd contribute a word or two.

   The software I'm using is a General Graphics Package published at
Stanford.  Since I don't have the thing right next to me, I can't give you
the address, but I can tell you about the thing.

   Most of the package is for graphics, of course, but there are a few
routines of note, particularly those which reset the timer chip to a
frequency of your own choosing.  These allow millisecond timing.

   As far as blanking the display goes, well, you'll just have to settle for
rewriting the strings with blanks (works much better than cls'ing), whether
by writing to the display memory or puts()'ing or whatever.

--
Jared J. Brennan
BITNET: INS_BJJB@JHUVMS, INS_BJJB@JHUNIX
ARPA: ins_bjjb%jhunix@hopkins.ARPA
UUCP: {allegra!hopkins, seismo!umcp-cs, ihnp4!whuxcc} !jhunix!ins_bjjb
(If Email fails (as it no doubt will), send via USnail . . .
   Box 193 Gilman Hall
   Johns Hopkins Univ.
   Baltimore, MD  21218   )

perkins@bnrmtv.UUCP (Henry Perkins) (07/24/87)

In article <1@epistemi.UUCP>, martin@epistemi.UUCP (Martin Shepherd) wrote:
> >> I need to be able to "blank" and "unblank"
> >> the monitor screen so that the onset of a display is more or less
> >> instantaneous.

In article <2169@bnrmtv.UUCP> perkins@bnrmtv.UUCP (Henry Perkins) responded:
> >Your accuracy can't be better than 1/30th second (color monitor)
> >or 1/25th second (monochrome monitor), because that's how long it
> >takes to refresh a screen image.

In article <5068@jhunix.UUCP>, ins_bjjb@jhunix.UUCP (Jared J Brennan) responded:
>    As far as blanking the display goes, well, you'll just have to settle for
> rewriting the strings with blanks (works much better than cls'ing), whether
> by writing to the display memory or puts()'ing or whatever.

The mechanism doesn't matter; you can rewrite just part of screen
memory or blank it all.  The important consideration is that what
you change in screen memory only affects what is actually sent to
the monitor 30 times a second.  You can "display" something and
"erase" it 106 times with NOTHING appearing on the screen if you
do it all in less than one video frame interval.

Yes, you CAN change the timer chip to allow millisecond accuracy.
IT DOESN'T MATTER.  All your timings are + up to 1/30 second (33
milliseconds) since your subjects are responding to screen images,
and you don't know when the images actually get to the screen any
more accurately than that.

"More or less instantaneous" is NOT possible.  "Within 33 milli-
seconds" is what you can get.
-- 
{hplabs,amdahl,3comvax}!bnrmtv!perkins        --Henry Perkins

It is better never to have been born.  But who among us has such luck?
One in a million, perhaps.

brianc@cognos.uucp (Brian Campbell) (07/30/87)

In article <1@epistemi.UUCP> martin@epistemi.UUCP (Martin Shepherd) writes:
> Does anyone out there know anything about using an XT-compatible PC
> (probably Elonex PC 88 Turbo) for applications involving millisecond
> timing?  I ask because I would like to be able to use a machine like

  Other problems aside, there was an article in Byte a few months ago
describing a program that provided microsecond timing resolution.  I
think the program was called "profile.asm" or something like that.  Hope
this is more helpful than some of the responses I've seen.

-- 
Brian Campbell          uucp: decvax!utzoo!dciem!nrcaer!cognos!brianc
Cognos Incorporated     mail: 3755 Riverside Drive, Ottawa, Ontario, K1G 3N3
(613) 738-1440          fido: sysop@163/8

bee@watkin1.waterloo.edu (Bill E. Eickmeier) (07/14/88)

Has anyone wrote any functions (preferable in MSC, but I'm not picky)
which provide millisecond accuracy under MS-DOS on an IBM PC/AT?  The
time functions in MSC - fstime()... - uses the system clock which is
updated only 18.2 times per second (about once every 55 ms).

Any help is greatly appreciated...

Bill E. Eickmeier, Kinesiology, U. Waterloo, Waterloo, Ontario, Canada N2L 3G1
UUCP: ...!{allegra|clyde|linus|utzoo|decvax|uunet}!watmath!watkin1!bee
Canadian: bee@watkin1.uwaterloo.ca
Most others: bee@watkin1.waterloo.{edu|cdn}	BELL:1-519-885-1211(x2972)

davidsen@steinmetz.ge.com (William E. Davidsen Jr) (07/15/88)

In article <19870@watmath.waterloo.edu> bee@watkin1.waterloo.edu (Bill E. Eickmeier) writes:
| Has anyone wrote any functions (preferable in MSC, but I'm not picky)
| which provide millisecond accuracy under MS-DOS on an IBM PC/AT?  The

  Sure, an I think it was even posted. I use a combination of hardware
and software timeing loops... on the first call I go thru a loop and
see how many times it takes to get a ms (checked by the hardware int).
This takes care of clock speeds, etc. I wrote it for a friend who has a
music program.

-- 
	bill davidsen		(wedu@ge-crd.arpa)
  {uunet | philabs | seismo}!steinmetz!crdos1!davidsen
"Stupidity, like virtue, is its own reward" -me

bee@watkin1.waterloo.edu (Bill E. Eickmeier) (07/18/88)

In article <11539@steinmetz.ge.com> davidsen@crdos1.UUCP (bill davidsen) writes:
>In article <19870@watmath.waterloo.edu> bee@watkin1.waterloo.edu (Bill E. Eickmeier) writes:
>| Has anyone wrote any functions (preferable in MSC, but I'm not picky)
>| which provide millisecond accuracy under MS-DOS on an IBM PC/AT?  The
>
>  Sure, an I think it was even posted. I use a combination of hardware
>and software timeing loops... 

Hmmm, I can't find it here, oh well. I did find something in an old Dr.
Dobb's that should do the job. If anyone wants it, just ask and I'll
mail or post...

--
Bill E. Eickmeier, Kinesiology, U. Waterloo, Waterloo, Ontario, Canada N2L 3G1
UUCP: ...!{allegra|clyde|linus|utzoo|decvax|uunet}!watmath!watkin1!bee
Canadian: bee@watkin1.uwaterloo.ca
Most others: bee@watkin1.waterloo.{edu|cdn}	BELL:1-519-885-1211(x2972)