TongY, the following is the program I mentioned and used, which is provided in the attached paper titled "Triggering with NI-VXI".
Appendix B
Example Using the TIC Counter to Implement a Program
Delay
On a PC platform, most languages include a delay function to temporarily halt the execution of
the program for a specified amount of time. These functions rely on the PC system clock which
runs at 18 Hz, resulting in a 55 ms resolution timer. In some situations, it is necessary to have a
delay function with a higher resolution. The following program creates a delay function with a
resolution of 1 ms for use in a standalone DOS executable. It takes advantage of the TIC counter
to interrupt the program every 1 ms. Of course, there are several ways in which to have the TIC
chip provide this feature, but this one requires only the TIC counter and no trigger lines.
Another advantage of this function is that it does not require any special setup. All you need to
do is call TIC_Delay() as you would normally call the Delay() function.
#include "nivxi.h"
#include "stdio.h"
#include "process.h"
#define CONTROLLER -1
#define SET_TRIG_HNDR 1U<<14
#define GET_TRIG_HNDR 50
#define SYNC 4
#define CLK10 70
#define ONE_MS_COUNT 10000U /* number of CLK10 cycles that = 1 ms */
#define NO_ERROR 0
static int16 ret;
/* Interrupt Flag and Function */
static int delay_flag=0;
NIVXI_HQUAL void NIVXI_HSPEC delay_intr_handler (int16, uint16, uint16);
NIVXI_HQUAL void NIVXI_HSPEC (*old_handler) (int16, uint16, uint16);
/**************/
/* TIC_Delay -- 1 ms res. delay function using interrupts
/**************/
int TIC_Delay (uint16 count)
{
uint16 loop;
/**/
/* Enable the Interrupts. Notice the return codes have an
/* extra number subtracted to more easily identify the
/* function that returned the error code.
/* Also notice that the old handler is retrieved first so that
/* it may be reinstalled at the end of the delay function.
/**/
old_handler = GetTrigHandler (GET_TRIG_HNDR);
ret = SetTrigHandler (SET_TRIG_HNDR, delay_intr_handler);
if (ret<0) return ret;
ret = TrigCntrConfig (CONTROLLER, 0, CLK10, ONE_MS_COUNT);
if (ret<0) return ret - 200;
ret = EnableTrigSense (CONTROLLER, 50, SYNC);
if (ret<0) return ret - 400;
/**/
/* Respond to count interrupts.
/**/
for (loop=0; loop
while (!delay_flag); /* Waiting for interrupt */
delay_flag = 0;
}
ret = DisableTrigSense (CONTROLLER, 50);
if (ret<0) return ret - 800;
ret = SetTrigHandler (SET_TRIG_HNDR, old_handler);
if (ret<0) return ret - 1000;
return 0;
} /* END TIC_Delay () */
NIVXI_HQUAL void NIVXI_HSPEC delay_intr_handler (int16 controller, uint16 line, uint16 type)
{
delay_flag = 1;
/* Reset the 16-bit Counter */
TrigCntrConfig (CONTROLLER, 2, CLK10, ONE_MS_COUNT);
} /* END delay_intr_handler */
void main(void)
{
/* It is not recommended to call InitVXIlibrary() in the */
/* Delay function because the overhead on InitVXIlibrary() */
/* can be fairly large. */
ret = InitVXIlibrary();
if (ret<0) {
printf ("Init Failed\n");
exit (-1);
}
/* Delay for 1 ms */
printf("First for 1 ms...\n");
TIC_Delay (1);
/* Delay for 100 ms */
printf("Done, now for 100 ms...\n");
TIC_Delay (100);
printf("Done!\n");
ret = CloseVXIlibrary();
}帖子被Yanghuan在03-23-2005 01:46 AM时编辑过了