| 发表于:2007-07-19 10:16:353楼 得分:10 |
有个例子: //利用中断实现每500毫秒接收一次数据 //调用dos下的中断。 //dos的时钟中断 int 21h ah=0x1c 每秒产生18.2次中断 //该程序时间间隔为550毫秒 可以由count的值算出。 #include <stdio.h> #include <dos.h> #include <conio.h> #define intr 0x1c //0x1c为时钟中断 #ifdef __cplusplus #define __cppargs ... #else #define __cppargs #endif void interrupt ( *oldhandler)(__cppargs); int count=0; int a=0,b=0; struct time t; void interrupt handler(__cppargs) // 执行dos中断时调用的程序 { count++; if(count==10) { gettime(&t); b=t.ti_hund; printf( "(2) %d\n ",b); if(b <a)printf( "delay %d ms ",((100-a)+b)*10); else printf( "delay %d ms ",(b-a)*10);} } int main(void) { oldhandler = getvect(intr); //取得原来的中断向量 setvect(intr, handler); //设置现在的中断向量 gettime(&t);a=t.ti_hund; printf( "(1) %d\n ",a); while (count < 11); //循环等待。执行dos的时钟中断 setvect(intr, oldhandler); //执行完毕,恢复原来的中断向量 return 0; } | | |
|