4

Does anyone know if there is a way to cause a interrupt via software? Right now I am setting up a timer at a very low timeout 1ns and then using that to cause the interrupt.

Sorry if this is a dumb question... I think there has to be a better way to do this...

Thanks,

Riley

ril3y
  • 571
  • 1
  • 6
  • 14

2 Answers2

3

The best way I have found to cause a software interrupt (without using an RTOS) is to use one of the pinchange interrupts. Activate the pullup resistor on the pin. Then when you change the value, it will trigger the interrupt.

Scott Murphy
  • 925
  • 6
  • 10
  • That is an interesting approach, but does require external hardware, always a bad thing to deal with. – Kortuk Jun 06 '10 at 19:02
  • What external hardware? I meant the internal pullup resistor. – Scott Murphy Jun 06 '10 at 19:30
  • The pin does need external hardware to pull it low by default? Or do the pins by definition float to a low voltage? To me you need a pull down of a value significantly greater than your internal pull up to make sure this pin floats low. – Kortuk Jun 06 '10 at 19:52
  • I do not mean to say it is not a good answer, I was just adding my input. There is rarely a perfect method, I just mean to make sure any disadvantages are fleshed out. The advantage is a simple way to create a software generated interrupt. It does seem you need to enable that pull-up, then quickly disable it and hope it has floated low before you use it again. – Kortuk Jun 06 '10 at 19:55
  • Hmm... This is an interesting point. The way I use it isn't by enabling and disabling the pull up... I enable the pull up and then write 1 or 0 to the pin. Leaving the pullup active in both cases. I see what you are saying now though about it possibly having some indeterminancy. This would be dependant on the way your MCU was structured. Good talk! – Scott Murphy Jun 06 '10 at 20:55
  • 2
    The pin change interrupt works even when you configure the pin as an output - and I think that's what Scott's doing. Must also be using an open-drain output, and hence the need to enable the pull-up. – JustJeff Jun 07 '10 at 02:28
  • 1
    this is the correct answer. the datasheet expressly states that the interrupt will fire even if the port pin is configured to an output. that's the whole point in fact :) – vicatcu Jun 09 '10 at 17:15
  • interesting. I guess that is a way to do it, you do still lose an I/O over it, but 1 I/O pin is not that bad. – Kortuk Jun 13 '10 at 14:53
1

I know this may seem odd, but this is true on most microcontrollers.

Try just setting the flag that is normally set by an interrupt condition yourself. if the flag that normally triggers from a timer interrupt at ox7857.6, then just try setting this bit yourself, in most microcontrollers you will trigger the interrupt yourself quite easily.

Sometimes you need to disable global interrupts, then set it, and then enable global interrupts.

Forgive me for not having using an X-Mega, but I know I have seen this work on other controllers.

Kortuk
  • 13,362
  • 8
  • 60
  • 85