0

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.

this is the code in 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 :

enter image description here

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.

What I'm facing here, is that the interruption from RBIE works only once, the second time I press the button, nothing happens.

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.

I Hope that you could see something i didn't notice, and help me.

shizhen
  • 12,251
  • 9
  • 52
  • 88
  • 1
    _the second time I press the button, nothing happens_ doesn't mean that _the interruption from RBIE works only once_. Your globals aren't `volatile`, so your main loop doesn't bother to reload them. – user58697 Jan 20 '19 at 22:57
  • @user58697 made the variables volatile, nothing happens :/ – Steven May Jan 20 '19 at 23:13
  • what means 'nothing happens'? Have you checked with the debugger if the interrupt is 'fired'? – Mike Jan 22 '19 at 08:03

2 Answers2

0

The code as posted compiles without warnings or errors with 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.

Your circuit diagram looks like it was created using Proteus, perhaps there are issues with how that simulator works.

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.

I would suggest setting bit 8 of the OPTION_REG to one to turn off the PORTB pull-ups.

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.

Dan1138
  • 1,150
  • 8
  • 11
0

You have enabled internal weak pull up resistor and also connected pull down resistor on pin RB0, external resistor is not needed, also you need to provide some amount of delay (about 300ms) after the button is pressed.