Question

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.

Was it helpful?

Solution

*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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top