timerembeddedpicmikroc

Unable to produce 1s delay using timer module


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;
    }
  }
} 

Solution

  • The declaration of the ISR is wrong

    Switch:

    void Interrupt(){  
    

    to:

    void interrupt(){  
    

    And don't forget the volatile

    volatile char value;