I am new to using this micro-controller and am having trouble setting up the interrupts. I am going to have a pump connected to a pin and when the pump encounters an error the pump will close and ground the switch connected to the micro-controller. I am guessing that I would have to use one of the PCINT interrupts since I am looking for a pin change, but I do not know how to set up the EICRA or PCICR to get this to work. If anyone has any information at all it would greatly help.
-
1This question requires some specific information. For example, what is the exact model of AVR processor you are using? Is it on a proprietary board or is it an "Arduino" board (if so which board)? Finally, just to my task easier, if possible please post a link to the Atmel Manual (on their web site) that documents the specific processor you are using. – JackCColeman Aug 05 '13 at 18:30
-
@JackCColeman I am using the ATmega88PA on a olimex dev board. The manual is http://www.atmel.com/Images/Atmel-8271-8-bit-AVR-Microcontroller-ATmega48A-48PA-88A-88PA-168A-168PA-328-328P_datasheet.pdf I am going to also need to use an external button as an interuppt as well. I understand generally how an interrupt works I just don't understand the setup if that makes sense. – brian Aug 06 '13 at 14:53
-
Perhaps you should update your question with that vital information instead of putting it in the comments. – David Bern Aug 06 '13 at 15:44
-
brian, have you looked at my answer? Was it what you where looking for? – David Bern Aug 06 '13 at 16:33
1 Answers
*Updated answer, the hardware is an Atmega88.
#include <avr/io.h>
#include <avr/interrupt.h>
ISR (PCINT0_vect){
/* This is where you get when an interrupt is happening */
}
int main(void)
{
/*Assumes that you are using PCINT0.
*It is also known as PB0
*/
DDRB &= ~(1<<PB0); /* Set PB0 as input */
PORTB |= (1<<PB0); /* Activate PULL UP resistor */
PCMSK0 |= (1 << PCINT0); /* Enable PCINT0 */
PCICR |= (1 << PCIE0); /* Activate interrupt on enabled PCINT7-0 */
sei (); /* Enables interrupt */
/* cli (); is used to disable interrupts. */
for(;;){
}
return 0;
}
The above example uses PB0 as input and activates a internal pull up resistor. This will have the effect that PINB is 1 until it is connected to ground. When connected to ground PINB will be 0.
PCINT0 is the activated pin, set in PCMSK. And PCICR is set to catch Pin changes on enabled PCINT7 to 0.
You can find all this information in the datasheet, it is a lot of information, but essential if you want to know how to use a AVR. Datasheet
You can find more information about the ISR(), sei (), cli () at nongnu.org there is also a complete list over vectors used by the ISR ().
AVR Freaks have a article that is free to download, it will help you understand how it works, the article is called "Basic interrupts and I/O"
I hope this get you started.

- 778
- 6
- 16
-
hey I was able to get everything working but I am still a little confused. I have the interrupt working but I did not set PB0 as in input so I don't understand how it is working. Also I am going to have to make a second PCINT but will I have to change how PCICR is defined to get this to work if I want to use PCINT2 which is pin16? Once I get this second interrupt working I will finally be done with my first project. @David Robertsson Thanks for all the help – brian Aug 14 '13 at 18:47
-
Well, to set them as inputs will come in handy when you have to determine which pin caused the interrupt. And if you want to do that, dont forget the pull up resistor http://en.wikipedia.org/wiki/Pull-up_resistor – David Bern Aug 14 '13 at 21:41
-
The followup questions you have are great. But, to avoid comment pollution, it is better to think about the new problem you have, and compose a new great question, so that other people are able to answer as well. If you follow my advices told here and in the answer I'm sure you will find the answers you seek. – David Bern Aug 14 '13 at 21:48