0
\$\begingroup\$

I am simulating a project on Proteus simulation software using this microcontroller. My issue is, I have made a 2 sec interrupt on timer0. But simulation is showing it as tooo fast (w.r.t simulation clock).

I used the Mikroelectronica Timer calculator using these settings:

The simulation schematic is as follows:

I am using MikroC pro for PIC compiler. And yes, I have double-checked the Clock frequency in the compiler, of the quartz crystal and the MCU in proteus, all are 4 MHz. I have set the crystal as XT in the compiler as well.

What could be the reason behind this inaccurate timer?

The code I'm using is as shown below:

int toggle1 = 0;
int toggle2 = 0;
int toggle3 = 0;
int toggle4 = 0;

void InitTimer0(){
  T0CON  = 0x84;
  TMR0H  = 0x0B;
  TMR0L  = 0xDC;
  TMR0IF_bit = 0;
  TMR0IE_bit     = 1;
}

void Interrupt() {
  if (INT0IF_bit && INT0IE_bit) {
     INT0IF_bit = 0;
     if(toggle1 == 0){
          LATC0_bit = 1;
          LATC2_bit = 0;
          toggle1 = 1;
          INTEDG0_bit = 1;         // Interrupt on rising edge on RB0
     }
     else{
          LATC0_bit = 0;
          toggle1 = 0;
          INTEDG0_bit = 0;         // Interrupt on falling edge on RB0
     }
   }
   if (INT2IF_bit && INT2IE_bit) {
     INT2IF_bit = 0;
     if(toggle2 == 0){
          LATC3_bit = 1;
          LATC2_bit = 0;
          toggle2 = 1;
          INTEDG2_bit = 1;         // Interrupt on rising edge on RB0
     }
     else{
          LATC3_bit = 0;
          toggle2 = 0;
          INTEDG2_bit = 0;         // Interrupt on falling edge on RB0
     }
   }
   if (INT1IF_bit && INT1IE_bit) {
     INT1IF_bit = 0;
     if(toggle3 == 0){
          LATC0_bit = 1;
          LATC2_bit = 0;
          toggle3 = 1;
          INTEDG1_bit = 1;         // Interrupt on rising edge on RB0
     }
     else{
          TMR0IE_bit     = 1;
          toggle3 = 0;
          INTEDG1_bit = 0;         // Interrupt on falling edge on RB0
          delay_ms(500);
          LATC0_bit = 0;
     }
   }
   if (TMR0IF_bit){
    TMR0H    = 0x0B;
    TMR0L    = 0xDC;
    if(toggle1 == 0 && toggle3 == 0)
          LATC2_bit = 0;
    if(toggle4 == 0){
         LATC4_bit = 1;
         toggle4 = 1;
    }
    else{
         LATC4_bit = 0;
         toggle4 = 0;
    }
   }

}

void main() {

  ADCON1 = 0x07;           // All digital I/O
  RBPU_bit = 0;

  TRISB = 0xFF;            // Set PB0 as input
  TRISC = 0x00;            // Set PortD as output
  PORTC = 0x00;            // Starting value for PortD

  INTEDG0_bit = 0;         // Interrupt on falling edge on RB0
  INTEDG1_bit = 0;         // Interrupt on falling edge on RB1
  INTEDG2_bit = 0;         // Interrupt on falling edge on RB2

  INT0IF_bit = 0;          // Clear INT0IF
  INT0IE_bit = 1;          // turn OFF interrupt on INT0

  INT1IF_bit = 0;          // Clear INT1IF
  INT1IE_bit = 1;          // turn OFF interrupt on INT1

  INT2IF_bit = 0;          // Clear INT2IF
  INT2IE_bit = 1;          // turn OFF interrupt on INT2

  InitTimer0();

  GIE_bit = 1;             // enable GIE

  while(1){}
}
\$\endgroup\$
  • \$\begingroup\$ Probably because you're not clearing the Timer0 Interrupt Flag in your interrupt handler. And on an unrelated note - why do you have a delay_ms() call inside your interrupt? That's begging for trouble... \$\endgroup\$ – brhans Aug 1 '16 at 23:31
  • \$\begingroup\$ Thank you that clears it. I know that delay shouldn't be there. My timers were not responding up till now, so that was a last resort. I will make a 500ms timer now for that delay purpose. Thank you! \$\endgroup\$ – Mohsin Anees Aug 1 '16 at 23:44
  • \$\begingroup\$ Can someone please upvote the question. I have another related to this project but can't ask it -.- \$\endgroup\$ – Mohsin Anees Aug 3 '16 at 10:08
2
\$\begingroup\$

Probably because you're not clearing the Timer0 Interrupt Flag in your interrupt handler.

You need to have a TMR0IF_bit = 0; otherwise your code will just stay stuck in the interrupt, repeating it over and over.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.