0

I am trying to generate a 1 second delay, using a timer module. The value in the timer register is 61 DEC, which will generate an interrupt every ~50ms, which will increment the value of a variable named value.

After 20 interrupts ~1 sec will have passed, which will alter the state of an LED. But this doesn't seem to work as intended. The oscillator frequency is 4MHz, the prescaler value of the timer module is 256 and the targeted microcontroller is PIC16F72. Here is the code:

char value;
bit state;

void Interrupt(){     
  if(TMR0IF_bit){
      value++;
      TMR0 = 61;    
      TMR0IF_bit = 0;    
    }
}

void main(){      
  value = 0;
  state = 0;  
  TRISB.RB0 = 0;
  OPTION_REG = 0x87;
  TMR0 = 61;
  INTCON = 0xA0;
  
  while(1){
    if(value == 20){
      value=0;
      state=~state;
      PORTB.RB0=state;
    }
  }
} 
Mike
  • 4,041
  • 6
  • 20
  • 37
  • What means: doesn't seem to work? Try to use the debugger. – Mike Jun 22 '21 at 10:57
  • I tried to use debugger but it is not entering the interrupt routine. I tried to run debugger tool along with stop watch, even after 2 seconds it didn't enter the interrupt routine at once. – Mufaddal Darbar Jun 22 '21 at 12:08
  • Confirm that the timer is running by checking the counter register using the debugger. – Tagli Jun 22 '21 at 13:47
  • 1
    If it is not entering the interrupt handler, then you have a different problem perhaps, but not declaring `value` as `volatile` is still a problem. – Clifford Jun 22 '21 at 17:40
  • Have you switched off the watchdog in the configuration? – Mike Jun 23 '21 at 05:21
  • Pretty sure the bug is related to missing `volatile`. Though how does the compiler know that this function is an interrupt? You usually need some non-standard C keyword or pragma. – Lundin Jun 23 '21 at 06:43
  • in MikroC the ISR should be declared `void interrupt()` and not `void Interrupt()`. With a lowercase `i`. Maybe thats an error. – Mike Jun 23 '21 at 10:58
  • Thankyou everyone. making the 'I' in Interrupt() lowercase worked and now it's running perfectly. – Mufaddal Darbar Jun 24 '21 at 06:42
  • 1
    If your problem has been resolved, please consider checking your thread as SOLVED for the other people to know. – Kozmotronik Jun 24 '21 at 19:26

1 Answers1

1

The declaration of the ISR is wrong

Switch:

void Interrupt(){  

to:

void interrupt(){  

And don't forget the volatile

volatile char value;
Mike
  • 4,041
  • 6
  • 20
  • 37