Arduino Mega 2560 two interrupt issue

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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 <info@32leaves.net>

 

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()

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

you've not given us much information to give you a clear answer.

 

Firstly, I would suggest you measure the time it takes is each isr. One method is to set a port bit at the beginning of the isr and clear it at the end. Use a logic analyser and/or oscilloscope to measure. The 'scope is preferred as it give a visual indication of the min and max time. You want to answer the question "How often?" and "How long?"

 

Once you're armed with some numbers, you can then determine if there is a possibility of running out of time - eg, if your 500us isr takes 200us and the ps2 isr takes 400us sometimes - then there is a potential problem. 

The concern with re-enabling interrupts within the isr is 'nesting'. If another interrupt occurs, then another processor state is put on the stack. If this continues to happen without the isrs completing, then the stack grows and eventually weird shit happens(c).

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Interrupt routines should be fast.  Try to find the minimal thing

to do in the interrupt and return to the main program.  Setting

a single bit flag in a timer interrupt is very common.  The main

program waits for the flag to be set and then starts the potentially

long routine to update the LEDs or whatever else it needs to do.

(The first thing it does is clear the flag again of course.)

 

--Mike

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Vindhyachal Takniki wrote:
in top of timer2 isr I have written:

void timer2_isr_500us()
{
    interrupt();
    
    refresh_led()

Can I assume the use of ".cpp" means this is avr-gcc ? If so then it's very unwise to call functions from an ISR unless they are actually "static inline". If you call functions then the compiler will PUSH/POP to preserve almost the entire register set around each CALL. It can make ISRs very inefficient which is the last thing you want if speed is of the essence.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It sounds like your ISR()'s are taking too long to service, thus blocking other tasks from running when they should.

Always strive to keep your ISR() short and sweet, doing just enough, like setting a flag and returning, then in main loop, service the flag and clear it as needed.

We often see beginners here trying to do too much in the ISR(), when it should be done in the main loop.

 

 

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

1. led refresh function  refresh_led()   is for iluustation. In actual code its all in the functiob and refresh 20 segments, where each segment has its turn line wise.

 

2. Ps2 is library for arduino

 

3. if i set flag in isr & refresh led in main, then i see some flicker in segments.  might be due to fact, in isr, it is definative that every 500us it will be refeshed. while in main there may be time variatoin in each.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So what do you want us to do? Without specific information, we can only advise. You've got the advice, so its up to you.