1. 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.
    Dismiss Notice

Delay using interrupt in mikroc PIC

Discussion in 'Microcontrollers' started by sonar_abhi, Jun 21, 2017.

Tags:
  1. sonar_abhi

    sonar_abhi New Member

    Joined:
    Mar 15, 2016
    Messages:
    24
    Likes:
    0
    Hello Guys,

    I am trying to implement a delay using timer0 interrupt. I'll just post an abstraction of the code in mikroc
    Code (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: Jun 21, 2017
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,168
    Likes:
    910
    Location:
    Rochdale UK
    ONLINE
    What timer??? Nothing has been set!
     
  3. sonar_abhi

    sonar_abhi New Member

    Joined:
    Mar 15, 2016
    Messages:
    24
    Likes:
    0
    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 (text):

    #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: Jun 21, 2017
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,168
    Likes:
    910
    Location:
    Rochdale UK
    ONLINE

    Change to " if(Latdelay > 3)" What's the frequency of the OSC?
     
    • Like Like x 1
  6. sonar_abhi

    sonar_abhi New Member

    Joined:
    Mar 15, 2016
    Messages:
    24
    Likes:
    0
    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 (text):

    #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;
         }
       }
     }
    }
     
     
  7. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,168
    Likes:
    910
    Location:
    Rochdale UK
    ONLINE
    Glad you sorted it!!
     
  8. sonar_abhi

    sonar_abhi New Member

    Joined:
    Mar 15, 2016
    Messages:
    24
    Likes:
    0
    Yes. And that brings up the next problem I am facing. Will create a new thread as it is related to 74HC595
     

Share This Page