NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Microcontroller Programming » Checking which pin sent an interrupt
October 24, 2010 by Metallic_Cloud |
I have two buttons on my breadboard connected to PC4 and PC5 and I have them both hooked up to an interrupt. When PC5 fires, it seems to only run the correct part, although when PC4 fires, it seems to be running both parts. Can someone please let me know what I'm doing wrong.
|
---|---|
October 24, 2010 by Rick_S |
A couple of issues (at least to my understanding).
|
October 25, 2010 by hevans (NerdKits Staff) |
HI Metallic_Cloud, I think the problem might be that you are checking for the pin to be high inside the interrupt handler, and not necessarily checking for a pin transition. What is most likely happening in your code is that when the PC5 interrupt fires (any transition on the pin) PC4 happens to be high, even though it might have not just actively transitioned. Checking for the current state of the pin is a good practice when you only have one pin on the bank changing but if you have two pins that are moving independently you probably want to have some sort of global state of your own for each of the pins (that is managed inside the interrupt). Then when an interrupt is fired you can check the direction of the transition and act on it as required. Hope let me know if something I said doesn't make sense. Humberto |
October 25, 2010 by Metallic_Cloud |
Hi Guys, Thanks for your help. I currently have it as the following
and it seems to be working, although I haven't had much time to properly test it. Humberto, are you saying I should have something like the following...
and have the same thing for PC4, or did I not understand correctly? Cheers |
October 25, 2010 by bretm |
Ideally you wouldn't use PC4 and PC5 because they both use interrupt 1. If you instead use pins that are on different interrupts it becomes easier to discern the cause of the interrupt. If you want to use the same interrupt for some reason, then yes, it's better to keep track of the state and check if it changes. I'd do it a bit differently, so that you don't read PINC multiple times in the ISR (it could change each time you read it):
If a pin changes state and then changes back again before the ISR gets past the preamble stuff and into your code, the transition won't be seen in "changedPins" so you may want to do things differently depending on whether you need to catch that case or not. |
October 25, 2010 by Metallic_Cloud |
Thanks, I think I will change my structure to be more like yours. I have almost finished my project, and I will post the results when it's done. The reason I am checking which pin sent the interrupt, instead of using different interrupts is because I will end up with 4 or 5 buttons, so either way there will needs to be more than one button per interrupt. Cheers |
November 04, 2010 by Hexorg |
Hey guys, I'm trying to connect 7 push-buttons to avr, so I have to use same interrupt. You all are saying that if pin changes while ISR is still running, there will be trouble... But code that you wrote will be executed in less then one microsecond, and there is no way human fingers can press and release the button within 1 microsecond. My project is messing up with the buttons too right now, so you must be right somewhere, but I don't understand why. |
November 04, 2010 by bretm |
A single human-generated button press results in several (potentially dozens) of digital logic transitions. There are two reasons for this. One is that the switch components physically rebound against each other (bounce) and take a while to settle down, and the second is that even in the absence of physical bouncing many switches take some time to transition from a high-impedence state (open circuit) to a low-impendence state (closed circuit) and the switch voltage spends some time in the digital "gray area" which is neither clearly 0V nor 5V and this can cause logic levels to jump around. Both reasons together are called bouncing, and you probably need to do debouncing, either in hardware or software. |
November 04, 2010 by Hexorg |
can I just put a little delay in the interrupt and read PINx after that? |
November 04, 2010 by bretm |
As a general rule you don't want to do delays inside interrupts because new interrupts can occur during the delay, and complicated stuff can happen depending on whether the MCU holds onto the interrupt for you or whether it throws it away or whether you've re-enable interrupts inside your handler, etc. I would read this to familiarize yourself with the problem and some of the many different possible solutions. |
November 04, 2010 by Hexorg |
thank you :D That helped a lot! I guess pushbuttons and pin-change interrupts just don't go too well together. I just happened to have a timer in my app running every millisecond, so I just read the button values in that timer, and xor it with previous state to see what changed. Still get a bounce every 40 presses, but that's not bad at all for my system. |
November 05, 2010 by bretm |
Instead of using just two samples you can easily incorporate 8 samples by doing this (using PC4 for example):
|
Please log in to post a reply.
Did you know that 20 LEDs can be controlled from 11 microcontroller pins, to make a twinkling heart outline? Learn more...
|