1
votes

I have ATmega32 AVR and I want to have more Interrupt pins than (INT 0, INT 1, INT 2) so Can I convert any pins in ATmega32 to an interrupt or there is another method to have more interrupts in the micro controller? Or should I get another AVR microcontroller?

2
use software interrupts. - NAGA RAJ S
What do You do in your actual ISRs? Or what is purpouse of them? - dunajski

2 Answers

1
votes

INT0/INT1/INT2 and pins assigned to them in Atmega32 have special features, for example to wake up MCU from energy saving modes. If you need another interrupt with special feature but on diffrent pin then it is not possible, but... if you want to check input pin state and then execute some actions there is workaround that I used a few times.

So, how external interrupts works?

(Simplified, pseudo-description) There is a special hardware controller inside your MCU that detect rise-up/down, change of chosen pins and when certain condition is met, it reports to interrupt controller what happened, then if mask bit of this interrupt is set to enabled your MCU invokes interrupt vector (ISR).

So, how to workaround this?

Short explaination:

  1. Use one of interrupts given by timer unit and set "faster" than signal on input can change.
  2. Probe input states in timer ISR.
  3. Whenever condition is met, for example high-state on pin for about 5 ms, you can save that fact in variable value.
  4. Make callback to execute action or just make action inside your ISR, that depends about execution time of this action, ISRs should be short in execution time.

Pseudocode below (typed without compiling, just here in answer textbox):

#include <avr/io.h>  
#include <avr/interrupt.h>

volatile int sth_happened = 0;

void ExecuteCallback(void)
{
    // your action goes here
    // reset sth_happened by assgining to 0
}

ISR(TIMER0_COMP_vect) // this ISR should be executed faster than signal on chosen pin can change
{
    // check PIN of your pin here

    // if state/signal on your pin met requirements,
    // save it to variable or execute another code
    // sth_happened = 1 / ExecuteCallback();
}

int main(void)
{
    // Prepare your ISR timer here
    // I would recommend to use CTC mode with desired execution time
    sei(); // enable interrupts
    
    while(1)
    {
        if (sth_happened) ExecuteCallback();
    }
    return 0;
}
0
votes

You might consider just making the main loop in your program run faster so you can read those pins in your program and react when they change.

If you really need actual interrupts, you might consider switching to a newer AVR like the ATmega328P, where every I/O line can be used as a pin-change interrupt (PCINT) pin.