Pin change interrupts on ATmega8?

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

Hello all,
I'm finally developing my first real electronics projects (beyond playing with LEDs in all possible ways), consisting in a user-configurable automatic remote for my camera.

I have everything set down -- three 7-seg led displays, a relay, etc. -- but I just noticed that the ATmega8, which is what I wanted to use, doesn't seem to offer pin change interrupts.
Since I would need at least *four* digital inputs for a few buttons/switches, I had thought of using interrupts to sense the changes on those pins, but the ATmega8 only offers two external interrupts on INT0 and INT1 (the ATtiny2313, on the other hand, has several PCINT lines but I'm not sure if I can make it all within 2 KB).
Should I resort to constantly polling the pins of the mega8 directly?

Battery life would be a concern and interrupts would in theory help; but thinking of it, I could also poll the lines once in a few milliseconds and let the MCU sleep in the meantime.

I don't know how good that would be in any case, interrupt or no interrupt, as the displays are going to be on at all times (although not directly controlled by the MCU, as I'm using a 74HC595 for each display).

Anyway, sleep mode aside, what is the most elegant way to check for input lines on an MCU that doesn't offer enough pin change interrupts?

Thank you very much!

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

Why not use the ATmega88 instead?

If you must use the ATmega8, you can connect several switches to one interrupt pin using diodes.

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

I don't have any ATmega88s at the moment, unfortunately (only mega8, mega8L, tiny2313 and tiny45). I would have to order them from an online store and the shipping costs would be exaggerated for those only, as I don't need anything else at the moment.

Could you explain what you mean with using diodes? Thank you!

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

Quote:
Since I would need at least *four* digital inputs for a few buttons/switches, I had thought of using interrupts to sense the changes on those pins
Using interrupts for this is a bad idea, since every switch bouncing (and switches do bounce) will create a rapid series of pointless interrupts. Poll the pins and use a software debounce algorithm.

Stealing Proteus doesn't make you an engineer.

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

Connect each switch between GND and a port pin on the micro. Enable the internal pull-ups or include external pull-ups. Then connect a separate diode from each switch all to one of the interrupt pins. Once again, the interrupt pin should have a pull-up. The cathodes should be toward the switch.

This way, when any button is pressed, the interrupt line will be pulled low and you can still read each switch individually.

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

ArnoldB wrote:
Using interrupts for this is a bad idea, since every switch bouncing (and switches do bounce) will create a rapid series of pointless interrupts. Poll the pins and use a software debounce algorithm.

You have a point, but done correctly using the interrupts can be a big advantage in terms of power saving.

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

A capacitor across the switches will kill the debounce.
100n is probably a good value.

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

kevin123 wrote:
Connect each switch between GND and a port pin on the micro. Enable the internal pull-ups or include external pull-ups. Then connect a separate diode from each switch all to one of the interrupt pins. Once again, the interrupt pin should have a pull-up. The cathodes should be toward the switch.

This way, when any button is pressed, the interrupt line will be pulled low and you can still read each switch individually.

I'm afraid I still don't get it (hey, I'm a newbie :) ).
In that case, the line gets pulled down when any of the buttons goes down, okay. But how do I read which switch is closed, all I can see from the AVR is that one of them brought the line down? :shock:

Thank you for being patient with me. :D

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

kevin123 wrote:
You have a point, but done correctly using the interrupts can be a big advantage in terms of power saving.

Would that still be a big advantage if I have 3 led displays which are always on? The MCU might go to sleep, but lots (most of?) battery life would be "wasted" by the displays. Would the sleep modes still have such a big impact?

Perhaps I should consider a small LCD display... and while I'm at it, get some mega88s Hrm...

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

jollino wrote:
kevin123 wrote:
Connect each switch between GND and a port pin on the micro. Enable the internal pull-ups or include external pull-ups. Then connect a separate diode from each switch all to one of the interrupt pins. Once again, the interrupt pin should have a pull-up. The cathodes should be toward the switch.

This way, when any button is pressed, the interrupt line will be pulled low and you can still read each switch individually.

I'm afraid I still don't get it (hey, I'm a newbie :) ).
In that case, the line gets pulled down when any of the buttons goes down, okay. But how do I read which switch is closed, all I can see from the AVR is that one of them brought the line down? :shock:

Thank you for being patient with me. :D


The switches would still be connected to individual port pins you can read to see which button was pressed in addition to the interrupt pin.

Quote:
Would that still be a big advantage if I have 3 led displays which are always on? The MCU might go to sleep, but lots (most of?) battery life would be "wasted" by the displays. Would the sleep modes still have such a big impact?

You are right, the LED displays will be using the majority of your power. In this case the wake up from button press will not gain much.

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

Quote:
You have a point, but done correctly using the interrupts can be a big advantage in terms of power saving.

Now THAT has to be well-explained to me before I admit that it has any basis in fact.

Necessary debouncing is power-wasting?

Now, perhaps I can agree with you if you are referring to using interrupts as a wake-up source. Is that what you are referring to?

But OP doesn't have a power-saving situation--compared to his 7-segment(s) a few buttons/switches against the internal pullups are a spit into the ocean.

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

kevin123 wrote:
The switches would still be connected to individual port pins you can read to see which button was pressed in addition to the interrupt pin.

Oops, I was in a rush and I misread "port pins" -- it makes more sense now. I suppose the diodes are to avoid bringing current back to the switch and limiting the spikes? I don't have my stuff here so I can't try right now. :)

Edit: ok, I see the point now. The diode would be to avoid that any other switch that uses the same external interrupt pin gets back-triggered.

Quote:
You are right, the LED displays will be using the majority of your power. In this case the wake up from button press will not gain much.

It would however make the handling of the buttons easier... I am going to try your multiplexing idea, and when I finish the prototype I'll let it run off a 9 V battery to see how long it lasts. If I can get a few hours out of it, it'll be good enough (it's not like I'd be using 24/7 :) ).

Last Edited: Sun. Mar 23, 2008 - 12:33 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:
But OP doesn't have a power-saving situation--compared to his 7-segment(s) a few buttons/switches against the internal pullups are a spit into the ocean.

Actually, power-saving would be nice, but as I said I don't need it to run for years with two AA cells in series. After all it'd just be a timed camera remote...

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

Quote:
Now, perhaps I can agree with you if you are referring to using interrupts as a wake-up source. Is that what you are referring to?

Yes, that is what I am referring to.

If you have no other devices sucking up power, you can get the AVR current to less than a uA while waiting for a button press.

Of course, for the OP this is not the case with the LED display, so it is probably not worth the effort to use an interrupt for that case. I just kinda got off on a tangent describing how to use an interrupt for several keys regardless of whether it was useful in this case.

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

Kevin, your diode-based multiplexing idea works like a charm. Thank you very much!