Counter Interrupt not working

Discussion on projects that are created by users and posted on mikroElektronika website.
Post Reply
Author
Message
anilson002
Posts: 8
Joined: 16 Jan 2020 06:07

Counter Interrupt not working

#1 Post by anilson002 » 11 Apr 2020 00:18

Hello,

I am trying a simple project to count how many times a PushButton has been pressed.
I searched a couple of post but could not figure out how to make it work.

version 1

Code: Select all

/*
 * Project name:
     MyProject_3a (Using Timer0 to obtain count interrupts)

 * Description:
     - This code demonstrates how to use Timer0 and it's interrupt.
     - Program counts how many times PushButton on PORTA.RA4 has been pressed
     - Each count increment turns ON a LED (PORTB.RB1, PORTB.RB2, PORTB.RB3)
     - The count resets to Zero after 3 presses and turns OFF all LEDs
*/


unsigned cnt_pb;

void initialize()
{
   ANSEL = 0;

   TRISA = 0xFF;
   TRISB = 0x00;

   PORTB = 0x01;             //0b00000001

   cnt_pb = 0;
   
   OPTION_REG = 0x20;        //0b00100000
   TMR0 = 155;
   INTCON = 0xA0;            //0b10100000
}


void interrupt()
{
    cnt_pb++;
    TMR0 = 155;
    INTCON.TMR0IF = 0;
    
    switch(cnt_pb)
    {
       case 1:
            PORTB.RB1 = 1; break;
       case 2:
            PORTB.RB2 = 1; break;
       case 3:
            PORTB.RB3 = 1; break;
       default:
            PORTB.RB1 = 0;
            PORTB.RB2 = 0;
            PORTB.RB3 = 0;
            cnt_pb = 0;
    }
}


void main()
{
   initialize();
   while(1){}
}

version 2

Code: Select all

/*
 * Project name:
     MyProject_3a (Using Timer0 to obtain count interrupts)

 * Description:
     - This code demonstrates how to use Timer0 and it's interrupt.
     - Program counts how many times PushButton on PORTA.RA4 has been pressed
     - Each count increment turns ON a LED (PORTB.RB1, PORTB.RB2, PORTB.RB3)
     - The count resets to Zero after 3 presses and turns OFF all LEDs
*/


char cnt, old_cnt;


void initialize()
{
   ANSEL = 0;

   TRISA = 0xFF;
   TRISB = 0x00;

   PORTB = 0x01;             //0b00000001

   cnt = 0;

   OPTION_REG = 0x20;        //0b00100000
   TMR0 = 155;
   INTCON = 0xA0;            //0b10100000
}


void interrupt()
{
   if(INTCON.TMR0IF)
   {
      cnt++;
      TMR0 = 155;
      INTCON.TMR0IF = 0;
   }
}


void display()
{
    switch(cnt)
    {
       case 1:
            PORTB.RB1 = 1; break;
       case 2:
            PORTB.RB2 = 1; break;
       case 3:
            PORTB.RB3 = 1; break;
       default:
            PORTB.RB1 = 0;
            PORTB.RB2 = 0;
            PORTB.RB3 = 0;
            cnt = 0;
    }
}


void main()
{
   initialize();

   while(1)
   {
      if (old_cnt != cnt)
      {
         display();
         old_cnt = cnt;
      }
   }
}
Circuit_MyProject_3.JPG
Circuit_MyProject_3.JPG (53.29 KiB) Viewed 2012 times

hexreader
Posts: 1786
Joined: 27 Jun 2010 12:07
Location: England

Re: Counter Interrupt not working

#2 Post by hexreader » 11 Apr 2020 01:16

Your code works, but 1:2 prescaler is enabled, so timer 0 rolls over every 200 button presses.
Set PSA bit to bypass the prescaler. Now you only need 100 button presses to light an LED :)

Set TMR0 register to 255, not 155, so that only one button push is required to roll Timer 0 over to 0 and light the next LED

Code: Select all

/*
 * Project name:
     MyProject_3a (Using Timer0 to obtain count interrupts)

 * Description:
     - This code demonstrates how to use Timer0 and it's interrupt.
     - Program counts how many times PushButton on PORTA.RA4 has been pressed
     - Each count increment turns ON a LED (PORTB.RB1, PORTB.RB2, PORTB.RB3)
     - The count resets to Zero after 3 presses and turns OFF all LEDs
*/

char cnt, old_cnt;

void initialize()
{
   ANSEL = 0;                // disable analogue

   TRISA = 0xFF;             // all input
   TRISB = 0x00;             // all output

   PORTB = 0x01;             // 0b00000001

   cnt = 0;

   OPTION_REG = 0x28;        // 0b00100000 no pull-up, T0 clk RA4, rising edge, 1:2, PSA set (no prescaler)
   TMR0 = 255;               // a single button press will cause rollover
   INTCON = 0xA0;            // 0b10100000 GIE, TMR0IE,
}

void interrupt()
{
   if(INTCON.TMR0IF)
   {
      cnt++;
      TMR0 = 255;            // a single button press will cause rollover
      INTCON.TMR0IF = 0;
   }
}


void display()
{
    switch(cnt)
    {
       case 1:
            PORTB.RB1 = 1; break;
       case 2:
            PORTB.RB2 = 1; break;
       case 3:
            PORTB.RB3 = 1; break;
       default:
            PORTB.RB1 = 0;
            PORTB.RB2 = 0;
            PORTB.RB3 = 0;
            cnt = 0;
    }
}


void main()
{
   initialize();

   while(1)
   {
      if (old_cnt != cnt)
      {
         display();
         old_cnt = cnt;
      }
   }
}
Start every day with a smile...... (get it over with) :)

Post Reply

Return to “User Projects”