Skip to main content
deleted 6 characters in body
Source Link
Bouzaid
  • 3
  • 1
  • 1
  • 4

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 the following :

enter image description here 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 IRB0 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.

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 the following :

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 IRB0 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.

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 the following : 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 IRB0 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.

Source Link
Bouzaid
  • 3
  • 1
  • 1
  • 4

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.

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 the following :

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 IRB0 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.