引用 9 楼 wuliang11 的回复:
你会rtx吗?你看懂了上面代码吗?
你懂 c 语言么,你懂计算机么,你懂编程么?
用RTX库编写的定时程序,计时误差不超过10us。
//
// RTEX1.H
//
#include
#include
#include
// defines for interrput handler code
// TO DO: set the bus interrput vector
#define ISA_BUS_VECTOR 7
// defines for port I/O calls
// For this example, port set to speaker
// TO DO: set your own values
#define CT_PORT_BASE ((PUCHAR) 0x61)
#define CT_PORT_RANGE 1
unsigned long counts;
// function prototype for periodic timer function
void
RTFCNDCL
TimerHandler(
void * nContext
);
// function prototype for interrupt handler
void
RTFCNDCL
InterruptHandler(
void * nContext
);
//
// RTEX1.C
//
#include "RTEX1.h"
#include
#include
void
_cdecl
wmain(
int argc,
wchar_t **argv,
wchar_t **envp
)
{
// for periodic timer code
LARGE_INTEGER liPeriod; // timer period
HANDLE hTimer; // timer handle
unsigned char key;
// for interrupt handler code
HANDLE hInterrupt; // interrupt vector handle
int nContext; // context
unsigned long intcounts;
// for port I/O code
UCHAR cX; // character read from port
counts=0;
intcounts=0;
// RTX periodic timer code:
// TO DO: Set default timer period to your desired time.
// The period needs to be an even multiple of the HAL
// period found in the control panel.
// This example uses a period of 5000 micro seconds.
liPeriod.QuadPart = 50000;
// Create a periodic timer
if (! (hTimer = RtCreateTimer(
NULL, // security
0, // stack size - 0 uses default
TimerHandler, // timer handler
NULL, // NULL context (argument to handler)
RT_PRIORITY_MAX, // priority
CLOCK_2) )) // RTX HAL timer
{
//
// TO DO: exception code here
// RtWprintf(L"RtCreateTimer error = %d\n",GetLastError());
ExitProcess(1);
}
if (! RtSetTimerRelative( hTimer,
&liPeriod,
&liPeriod) )
{
//
// TO DO: exception code here
//
// RtWprintf(L"RtSetTimerRelative error = %d\n",GetLastError());
ExitProcess(1);
}
// RTX interrupt handler code
// RtAttachInterruptVector associates a handler routine with
// a hardware interrupt.
// Note: level triggered interrupts are not supported in the
// Win32 environment
hInterrupt = RtAttachInterruptVector(
NULL, // thread attributes
0, // stack size - 0 uses default
InterruptHandler, // handler routine
(void *) &nContext,// context
1, // priority
Isa, // interface type
0, // bus number
ISA_BUS_VECTOR, // bus interrupt level
ISA_BUS_VECTOR ); // bus interrupt vector
if (! hInterrupt)
{
//
// TO DO: exception code here
// RtWprintf(L"RtAttachInterruptVector error = %d\n",GetLastError());
ExitProcess(1);
}
// RTX port I/O code
// Enable direct I/O access of CT ports from user context
if (! RtEnablePortIo(CT_PORT_BASE,
CT_PORT_RANGE) )
{
//
// TO DO: your exception code here
// RtWprintf(L"RtEnablePortIo error = %d\n",GetLastError());
}
// Use RTX port I/O functions to read and write to port.
// See User's Guide chapter 3, "Using RTX Functionality" for
// description of functions.
//
// Below is an example of a call to read/write to a port.
RtWprintf(L"Turning on the speaker for 3 seconds.\n");
cX = RtReadPortUchar(CT_PORT_BASE) | 0x03;
RtWritePortUchar(CT_PORT_BASE, cX);
cX ^= 0x03;
while(1)
{
if(counts>=100*intcounts)
{
if(kbhit())
{
if((key=getch())==VK_ESCAPE)
break;
}
printf("%d\n",counts);
intcounts++;
}
}
RtWritePortUchar(CT_PORT_BASE, cX);
//
// TO DO: your program code here
//
ExitProcess(0);
}
//
// Functions called from RTEX1.c
//
#include "RTEX1.h"
//
// RTX periodic timer handler function
// Refer to the RTX Samples directory for examples
// of periodic timers
//
void
RTFCNDCL
TimerHandler(
PVOID context
)
{
// TO DO: your timer handler code here
counts++;
}
//
// RTX interrupt handler function
// Refer to the RTX Samples directory for examples
// of interrupt handlers
//
void
RTFCNDCL
InterruptHandler(
void * nContext
)
{
// TO DO: your interrupt handler code here
}
复制代码