简体   繁体   English

RBIE 中断仅工作一次 - PIC16F877A

[英]RBIE interrupt works only once - PIC16F877A

In my code, I have two interruptions, one is coming from the overflow of the TMR0, and the other one is when a button is pressed.在我的代码中,我有两个中断,一个来自 TMR0 的溢出,另一个来自按下按钮时。

this is the code in MikroC :这是 MikroC 中的代码:

int compt = 0;
int seconds = 10 ;
int enable = 0;

void interrupt(){

     if (INTCON.INTF) {
        PORTD = 9;
        enable = 1;
        seconds = 10;
        INTCON.INTF = 0;
     }

     if (INTCON.TMR0IF) {
        compt++;
        INTCON.TMR0IF  = 0;
        TMR0 = 0x06;
     }
}


void main() {

     TRISB = 0x01;
     PORTB = 0;

     PORTD = 0;
     TRISD = 0x00;


     INTCON = 0xB0;
     OPTION_REG = 0x44;
     TMR0 = 0x06;

     while(1){

        if (compt == 625){
           if (enable) seconds--;
           compt = 0;
        }

        if (seconds > 0 && enable == 1) {
           PORTD = seconds;
           PORTB.RB1 = 1;
        }  else {
            enable = 0;
            PORTB.RB1 = 0;
            PORTD = 0;
        }

     }

}

what I am trying to achieve with my code is as shown in the following picture :我试图用我的代码实现的目标如下图所示:

在此处输入图片说明

When I press one of the push buttons, the countdown starts and the LED illuminates until the countdown ends, and if the user pressed the button while the countdown still didn't hit 0, it starts over, until the countdown hits 0 again, then the LED should turn off.当我按下其中一个按钮时,倒计时开始并且 LED 亮起直到倒计时结束,如果用户在倒计时仍未达到 0 时按下按钮,则重新开始,直到倒计时再次达到 0,然后LED 应该关闭。

What I'm facing here, is that the interruption from RBIE works only once, the second time I press the button, nothing happens.我在这里面临的是,来自 RBIE 的中断仅起作用一次,第二次按下按钮时,没有任何反应。

I am not sure if the TMR0F has something to do with that or not, tried many things, but couldn't make it to work.我不确定 TMR0F 是否与此有关,尝试了很多方法,但无法使其正常工作。

I Hope that you could see something i didn't notice, and help me.我希望你能看到我没有注意到的东西,并帮助我。

The code as posted compiles without warnings or errors with MikroC. 使用MikroC,发布的代码在编译时不会发出警告或错误。

The code runs using the simulator in MLPAB v8.92 and when using the simulator stimulus to assert the INT0 interrupt it is handled correctly each time. 该代码使用MLPAB v8.92中的模拟器运行,并且当使用模拟器激励来声明INT0中断时,每次都会对其进行正确处理。

Your circuit diagram looks like it was created using Proteus, perhaps there are issues with how that simulator works. 您的电路图看起来是使用Proteus创建的,该模拟器的工作方式可能存在问题。

The only suspicious setting I can find is that the weak pull-ups are enabled for PORTB yet your circuit diagram has a 10K ohm pull-down in the INT0(RB0) pin. 我可以找到的唯一可疑设置是为PORTB启用了弱上拉功能,但是您的电路图在INT0(RB0)引脚中有一个10K欧姆的下拉电阻。

I would suggest setting bit 8 of the OPTION_REG to one to turn off the PORTB pull-ups. 我建议将OPTION_REG的第8位设置为1,以关闭PORTB上拉电阻。

Sorry my answer is not more definite but I cannot reproduce your problem from the information posted. 抱歉,我的回答还不确定,但是我无法从发布的信息中重现您的问题。

Seem like this question was asked on StackExchange too. 好像这个问题也是在StackExchange上问的。

您已启用内部弱上拉电阻并在引脚 RB0 上连接下拉电阻,不需要外部电阻,您还需要在按下按钮后提供一些延迟(约 300 毫秒)。

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

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