RTX 简单例子 点击:2180 | 回复:0



TellTruth

    
  • 精华:1帖
  • 求助:0帖
  • 帖子:10帖 | 451回
  • 年度积分:0
  • 历史总积分:1302
  • 注册:2003年10月29日
发表于:2005-06-14 14:23:00
楼主
用RTX库编写的定时程序,计时误差不超过10us。 // // RTEX1.H // #include <windows.h> #include <wchar.h> #include <rtapi.h> // 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 <stdio.h> #include <conio.h> 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 c


热门招聘
相关主题

官方公众号

智造工程师