Arduino Mega 2560 two interrupt issue

Thread Starter

Vindhyachal Takniki

Joined Nov 3, 2014
598
1. I am using arduino mega 2560. Connected are 20 segments together, refreshed every 500us. After every 500us, one segment is enable & written.

2. Also a PS2 keyboard is connected to read keys & display on segment. Library used is PS2Keyboard.cpp - PS2Keyboard library Written by Christian Weichel<Snip>.

3. Ps2 & timer refreshing code of 20 segments works independently correctly. Problem arises when I merge both codes together. In that case what happens is I am not able to read correctly from keyboard. Probably due to when timer interrupt comes, it blocks rest of interrupt until it is processed.

4. Timer 2 is used and for PS2 data line is pin 19 & clock pin is 18.

5. Now how to resolve this issues. Refreshing leds is also important other flickering is observed.

6. I read "http://www.gammon.com.au/interrupts", & read that inside interrupt function, below function can be used to enable interrupt again.

interrupts (); // allow more interrupts

7. this way code is working, but i am not sure how safe it is use.
in top of timer2 isr I have written:

void timer2_isr_500us()
{
interrupt();

refresh_led()

}


Mods Note:
Please don't show the Email on forum, it may bring the Spambot to the forum and to the Email.
 

MrSoftware

Joined Oct 29, 2013
2,273
What is inside your refresh_led()? It sounds like you might have a priority issue. Perhaps the keyboard needs to be a higher priority. Instead of doing any actual work inside your timer2_isr_500us() function, set a flag there and make the call to do the actual work inside the main loop. The goal is to minimize the time inside your timer2_isr_500us() function, leaving more time to handle your keyboard interrupts. Maybe something like:

Code:
volatile bool bRefreshLED = false;  /* Be sure to use volatile if set inside an interrupt! */

/* Make this function very light weight, minimize time inside an interrupt routine */
void timer2_isr_500us()
{
  bRefreshLED = true;
}

loop()
{
    if(bRefreshLED)
    {
        bRefreshLED = false;
        refresh_led();
    }
}
 
Top