简体   繁体   English

替换键盘中断(Interrupt 9)进行两次调用而不是一次调用

[英]Replace keyboard interrupt (Interrupt 9) makes two calls rather than one

Each keyboard press will enter to the function, which replaced the interrupt, twice.每按一次键盘将进入两次替换中断的函数。 Why is that happing?为什么会这样?

Maybe the "enter" after scanf is interfering?也许 scanf 之后的“输入”会干扰? But it goes to the function twice each press and not only after first try但是每次按下它都会进入该功能两次,而不仅仅是在第一次尝试之后

What am I doing wrong?我究竟做错了什么? How can I make it enter only once to the function for one press each time?我怎样才能让它每次按一次只进入一次功能? In the pic you can see I press 5 only twice instead of 5 times在图片中你可以看到我只按了两次 5 而不是 5 次

在此处输入图片说明

#include<stdio.h>
#include<dos.h>

volatile int ctrl_break_flag;   //counter
void interrupt(*Int9Save)(void);   


void interrupt my_func8(void)
{
  ctrl_break_flag++;
  printf("%d\n",ctrl_break_flag);
  Int9Save();
  }
void main()
{
 int N=0,i;
 Int9Save=getvect(9); //Save pointer to original interrupt.
 printf("Please enter number: ");
 scanf("%d",&N);
 setvect(9,my_func8);//Set interrupt pointer to our function.
 ctrl_break_flag=0;

 while(ctrl_break_flag<N);
   printf("End");
 setvect(9,Int9Save);//Return to original interrupt.
 return;
}

In order to avoid it you can simply set integer to count the times there was an interrupt and then ignore all the odd for example.为了避免它,您可以简单地设置整数来计算中断的次数,然后忽略所有奇数。

code example:代码示例:

void interrupt Keyboard(void){
    check++;
    Keyboard_Flag=1;
    if(check%2==0) //print only button pressed(will print only 
                                               odd interrupts)
    printf(" KeyPressed");
    Int9Save(); // return state
    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM