NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Microcontroller Programming » Understanding Interrupts
May 10, 2010 by avicell ![]() |
There are too many variables for me to keep straight about interrupts, and I'd like to ask the community to help me understand. My project is the traffic light one, and I am attempting to use the interrupt feature for when the 'walk' button is pressed. Whenever the button is pressed, a logic 1 goes to PB1 (verified with multimeter). My aim is to have the MCU recognize a logic 1 on PB1 as an interrupt trigger, which will then enable the 'walk' function (that consists of a white LED going on for 3 seconds, then blinking for 3 seconds. The program will then restart). Now I know that I can just poll the pin for a logic 1, but I am trying to learn interrupts. Here is my code (note, that each different LED state is handled by a 'condition', commented below. Also, the temp sensor feature - which will ultimately sense for a car on the side street, and the LCD display functionality is currently disabled, to hone in on my de-bugging) Thanks in advance for all the help!:
|
---|---|
May 11, 2010 by avicell ![]() |
OK, I figured it out.... It's amazing what 10 hours away from the problem does! I needed the code: PCICR |= (1<<PCIE0) PCMSK0 |= (1<<PCINT1); Now only problem is I have the white LED on PB5 (output), and the push button on PB1 (input). I don't have my bit-masking proper to get the desired functionality of "When you push the button, all else stops and the 'walk' sequence begins. Then proceed back where you were". The program is acting erratically with the walk sequence happening a few times in a row. |
May 11, 2010 by Ralphxyz ![]() |
Hey avicell, I am trying to learn C programming and interrupts can I ask you some questions. Can you post your complete code? What is the tempsensor code doing? Thanks, Ralph |
May 11, 2010 by hevans (NerdKits Staff) ![]() |
Hi avicell, How is your push button wired up? I noticed in your interrupt handler you have
Which is going to check for the pin being high. It looks like you set up the pullup resistor to pull your pin high, and if you have your button set up correctly it should pull the pin low when you push it. You want your interrupt to fire on the high to low transition so I think you want to check for the pin being low in that if statement. If this was a perfect button it shouldn't make a difference, but you will typically get more than one transition every time you push the button due to mechanical bouncing. Try checking for a low state of the pin and let us know if that fixes the issue. Humberto |
May 11, 2010 by avicell ![]() |
Humberto - Your suggestion was spot on! One line of code changed and swapping out a wire to ground was all it needed! I have the C node of the button on PB1, NO to +5V, NC to ground, and whenever I press the button, the 'walk' function begins. Still have a little de-bugging to do since the walk function happens 2x in a row, but that shouldn't be a problem. Next task up is to figure out PWM. Thanks again. |
Please log in to post a reply.
Did you know that a thermometer can be made "faster" by using a bit of math? Learn more...
|