/* TIMER.H */ /* Routines for reprogramming of timer and Ctrl-Break */ /* User-defined routines */ void user_init(); /* Initialization program. Sets the frequency */ void user_interface(); /* Interface task. Executed in the foreground */ void user_task(); /* Periodic task. Executed in the background */ void user_terminate(); /* Termination task. Executed after terminate() */ void user_abort(); /* Abort task. Executed after ctrl-break */ void newbreak_start(), newbreak_stop(),newtask_start(); /* Declaration of program variables */ /* freq is the sampling frequency (Hz) and must be set by the user */ float freq; int countdown,lobyte,hibyte,count,doscount,done; /* Routines to change the interrupt rate of the timer */ /* and to redirect the timer processing */ void (interrupt *oldtask) (); void interrupt newtask() { user_task(); if ( count==0 ){(*oldtask)();count=doscount;} else{--count;outp(0x20,0x20); } } void newtask_init() { countdown=(int)(1193180./freq); hibyte=(int)(countdown/256); lobyte=countdown-hibyte*256; count=done=0; doscount=(int)(freq/18.2); } void newtask_start() { disable(); oldtask=getvect(0x08); outp(0x43,0x36); outp(0x40,lobyte); outp(0x40,hibyte); setvect(0x08,newtask); enable(); } void newtask_stop() { disable(); setvect(0x08,oldtask); outp(0x43,0x36); outp(0x40,0); outp(0x40,0); enable(); } /* Routines to redirect the Ctrl-Break processing */ void (interrupt *oldbreak) (); void interrupt newbreak() { newtask_stop(); newbreak_stop(); outp(0x20,0x20); user_abort(); exit(1); } void newbreak_start () { disable(); oldbreak=getvect(0x23); setvect(0x23,newbreak); enable(); } void newbreak_stop() { disable(); setvect(0x23,oldbreak); enable(); } void terminate() { newtask_stop(); newbreak_stop(); outp(0x20,0x20); user_terminate(); exit(1); } /* Main program */ main() { user_init(); newtask_init(); newbreak_start(); newtask_start(); while(!done){user_interface();} }