Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Delay using interrupt in mikroc PIC

Status
Not open for further replies.

sonar_abhi

New Member
Hello Guys,

I am trying to implement a delay using timer0 interrupt. I'll just post an abstraction of the code in mikroc
C:
void inittimer()
{
//Timer initialization for a 32ms interrupt
}

void interrupt()
   {
   //interrupt code;
   delayvariable++;
   }

void main()
   {
   TRISB = 0;          //PORTB is output
   TRISC.F0 = 1;   //PORTC Pin 0 is input and connected to a switch
   if (PORTC.F0 ==0) //check is the button is pressed
     {
      if (delayvariable==3)   //~100ms second delay to check buttton debounce
      }
   if (PORTC.F0==0) //check if the button is still pressed
        {
         PORTB = ~PORTB; //toggle PORTB
         delayvariable = 0;  //reset the delay variable
         }
      }
   }
}

The above code does not work as I expected. Can somebody please point out the mistake I am making or suggest a better way for achieving a 100 ms delay using interrupt?
 
Last edited by a moderator:
My Bad...This is the complete code. Ideally, the PORTB should toggle if the button is pressed. But the problem lies in the button debounce part. The PORTB is toggled only if the button is pressed for more than 8 seconds. Also if the button is kept pressed, the PORTB keeps on toggling continuously every 100mS. I was expecting the button to be read and if after 100mS if the button is still pressed, the PORTB is toggled. Normally, delay is used to check the button debounce but I do not want to use the delay or the BUTTON library of mikroc. I want to do it via interrrupt


Code:
#define SW1 PORTC.RC0

char latdelay;

void InitTimer0(){
  OPTION_REG     = 0x87;
  TMR0           = 6;
  INTCON         = 0xA0;
}

void Interrupt(){
  if (TMR0IF_bit){
   TMR0IF_bit   = 0;
   TMR0         = 6;
   latdelay++;
  }
}

void main(){
 InitTimer0();
 TRISC.RC0 = 1;
 TRISB     = 0;
 PORTB     = 0x00;

 while (1){
  if (SW1 == 0){
   if (latdelay == 3)
    {
     if (SW1 == 0){
     PORTB = ~PORTB;
     latdelay=0;
     }
    }
   }
 }
}
 
Last edited:
Hi Ian,

Thanks. Solved the problem. Actually I am using PIC16F877A and it does not have the Latch resistors. Initialized a flag and checked the condition of the flag in the main loop.
Here is the code in case anyone needs it

Code:
#define SW1 PORTC.RC0


char latdelay, portdelay;

void InitTimer0(){
  OPTION_REG     = 0x87;
  TMR0           = 6;
  INTCON         = 0xA0;
}

void Interrupt(){
  if (TMR0IF_bit){
   TMR0IF_bit   = 0;
   TMR0         = 6;
   latdelay++;
  if (latdelay>3){
    portdelay=1;
    latdelay=0;
   }
  }
}

void main(){
 InitTimer0();
 TRISC.RC0 = 1;
 TRISB     = 0;
 PORTB     = 0x00;

 while (1){ 
     if (SW1 == 0)
     {
      latdelay=0;
     if (SW1 == 0  &&  portdelay == 1){
     PORTB = ~PORTB;
     portdelay=0;
     }
   }
 }
}
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top