Go Down

Topic: Atmega don't perform the interrupt (Read 2305 times) previous topic - next topic

edelll

Hello,

i did this code, but when i tested at the board, it didn't enter in the interrupt.
Anyone knows whats the problem?

Code: [Select]
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdio.h>
int i=0;
int k=0;
int j=0;

void ataque() {
i++;
if (i>3) {
digitalWrite(13,HIGH);
        delay(1000);
digitalWrite(13,LOW);
TCCR1B = 0x8;
i=0;
}
}
void ataque1() {
k++;
if (k>3) {
digitalWrite(12,HIGH);
        delayMicroseconds(10);
digitalWrite(12,LOW);
TCCR3B = 0x8;
k=0;
}
}
void ataque2() {
j++;
if (j>3) {
digitalWrite(11,HIGH);
        delayMicroseconds(10);
digitalWrite(11,LOW);
TCCR4B = 0x8;
j=0;
}
}

void conversao() {
        OCR1A = analogRead(A0);
        OCR3A = OCR1A;
        OCR4A = OCR1A;
TCCR1B = 0x9; // inicia o timer da fase A
}

ISR(INT0_vect)
{
conversao(); // busca o sinal de controle
}

ISR(INT1_vect)
{
TCCR3B = 0x9; // inicia o timer da fase B
}

ISR(INT2_vect) {
TCCR4B = 0x9; // inicia o timer da fase C
}

ISR(TIMER1_COMPA_vect) {
ataque();
}
ISR(TIMER3_COMPA_vect) {
ataque1();
}
ISR(TIMER4_COMPA_vect) {
ataque2();
}


void setup () {
  sei(); // habilita todas as interrupções
EICRA = 0x1; // habilita as interrupções 0 1 e 2 por borda de subida
EIMSK = 0x7; // habilita interrupção externa 0 1 e 2
        pinMode(13,OUTPUT);
        pinMode(12,OUTPUT);
        pinMode(11,OUTPUT); // pinos de ataque setados como saída
TCCR1A = 0x0; // configuracoes dos timers
TCCR1B = 0x8;
TCCR1C = 0x0;
TCCR3A = 0x0;
TCCR3B = 0x8;
TCCR3C = 0x0;
TCCR4A = 0x0;
TCCR4B = 0x8;
TCCR4C = 0x0;
TIMSK1 = 0x2;
TIMSK3 = 0x2;
TIMSK4 = 0x2; // fim das configuracoes
}

int main(void)
{
    while(1)
    {
    }
}


thanks!

Coding Badly


Remove this...

Code: [Select]
int main(void)
{
    while(1)
    {
    }
}


...replace it with this...

Code: [Select]
void loop( void )
{
}


edelll

No changes man  =( thanks !

i was thinking to do the interrupt part with arduino's language, does anyone knows where i can find the functions to use ?

edelll

I changed the interrupt configuration but saw no changes...

Code: [Select]
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdio.h>
int i=0;
int k=0;
int j=0;

void ataque() {
i++;
if (i>3) {
digitalWrite(13,HIGH);
        delay(100);
        digitalWrite(13,LOW);
TCCR1B = 0x8;
i=0;
}
}
void ataque1() {
k++;
if (k>3) {
digitalWrite(13,HIGH);
        delayMicroseconds(100);
digitalWrite(13,LOW);
TCCR3B = 0x8;
k=0;
}
}
void ataque2() {
j++;
if (j>3) {
digitalWrite(13,HIGH);
        delay(100);
digitalWrite(13,LOW);
TCCR4B = 0x8;
j=0;
}
}

void conversao() {
        OCR1A = analogRead(A0);
        OCR3A = OCR1A;
        OCR4A = OCR1A;
TCCR1B = 0x9; // inicia o timer da fase A
}

void ligatimer()
{
conversao(); // busca o sinal de controle
}

void ligatimer1()
{
TCCR3B = 0x9; // inicia o timer da fase B
}

void ligatimer2()
{
TCCR4B = 0x9; // inicia o timer da fase C
}

ISR(TIMER1_COMPA_vect) {
ataque();
}
ISR(TIMER3_COMPA_vect) {
ataque1();
}
ISR(TIMER4_COMPA_vect) {
ataque2();
}


void setup () {
  sei(); // habilita todas as interrupções
attachInterrupt(3,ligatimer,RISING);
attachInterrupt(1,ligatimer1,RISING);
attachInterrupt(2,ligatimer2,RISING);
        pinMode(13,OUTPUT);
        pinMode(12,OUTPUT);
        pinMode(11,OUTPUT); // pinos de ataque setados como saída
        digitalWrite(13,LOW);
        digitalWrite(12,LOW);
        digitalWrite(11,LOW);
TCCR1A = 0x0; // configuracoes dos timers
TCCR1B = 0x8;
TCCR1C = 0x0;
TCCR3A = 0x0;
TCCR3B = 0x8;
TCCR3C = 0x0;
TCCR4A = 0x0;
TCCR4B = 0x8;
TCCR4C = 0x0;
TIMSK1 = 0x2;
TIMSK3 = 0x2;
TIMSK4 = 0x2; // fim das configuracoes
}

void loop(void)
{
}



any ideas?

CrossRoads

I can post an example of how I used an interrupt to wake an arduino from sleep when I get home in an hour or so.
Or you can browse my recent posts, I think I just posted that code last night for someone else.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

AWOL

Why have you got massive delays inside an interrupt service routine?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

madworm

#6
Jun 30, 2011, 10:29 pm Last Edit: Jun 30, 2011, 10:33 pm by madworm Reason: 1
I'd go as far as saying 'infinite delays'.

You can't use the delay function(s) inside interrupt code, as they rely on the fact that another interrupt keeps ticking. And that doesn't happen.
• Upload doesn't work? Do a loop-back test.
• There's absolutely NO excuse for not having an ISP!
• Your AVR needs a brain surgery? Use the online FUSE calculator.
My projects: RGB LED matrix, RGB LED ring, various ATtiny gadgets...
• Microsoft is not the answer. It is the question, and the answer is NO!

CrossRoads

Here you go

http://arduino.cc/forum/index.php/topic,64472.msg476814.html#msg476814

time to go sit in traffic ...
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

edelll

in my project, i will put a zero cross signal at the ext interrupts ports and later trigger a triac.. the massives delay is just to see led's light as the signal to the triac.. in real project this delay will be just the pulse's large to the triac.. i'm doing this things because i'm home and dont have measurements instruments here.

now i was testing and i changed the mode to falling, and if a touch the pin the led keeps on until im touching it..
and when i use rising mode and gave a +5v pulse in int0 pin the led didnt turn on..

thanks for helping!

CrossRoads

You have internal pullups enabled, or external pullup/pulldown installed so the pin insn't floating? You need that for stable, predictable operation.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

edelll

I think that atmega2560 has internal pullups enabled by default, hasn't it ?

CrossRoads

No. Nothing is enabled by default. Pins are only turned on as inputs.

"The Port x pins are tri-stated when a reset condition becomes active, even if the clock is not running."

"The I/O ports of the AVR are immediately reset to their initial state when a reset source goes
active. This does not require any clock source to be running."

"The port pins are tri-stated when reset condition becomes active,
even if no clocks are running."

The sketch must turn the pullup on if it is to be used.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

edelll

If i put a signal in INT0 pin, it will perform as an input, won't it?
Should I change any register or add some code in addiction to attachInterrupt() ?

CrossRoads

if you define the interrupt pin
int pin2interrupt = 2;

and if you have this on void setup
pinMode(pin2interrupt, INPUT); // D2 is INT0
digitalWrite(pin2interrupt, HIGH);  // turns on internal pullup


then having this in your code will create an interrupth when D2 goes low:
 attachInterrupt(0, pin2interrupt, LOW);

and here's what will happen when it does:

// *  Name:        pin2Interrupt, "ISR" to run when interrupted
void pin2interrupt()
{
 /* Set a flag  here, turn on an LED, do something short */
}

So I didn't look - do you have all these pieces?
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

edelll

I was testing now and I think that I discovered what was the problem.. when i just touch pin 3 with my finger, it was entering at INT0 interrupt routine, so I make pin 3 as a output and this problem was solved.

But now I still don't can enter in INT0 routine by a failling edge at INT0 pin (that I suppose to be de SCL pin, at atmega 2560).

I didn't put digitalWrite() and pinMode() moves yet, i will try now and then I post the result. Thanks!

Go Up