Pages: [1] 2   Go Down
Author Topic: Atmega don't perform the interrupt  (Read 1546 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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!
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12286
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Remove this...

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

...replace it with this...

Code:
void loop( void )
{
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No changes man  smiley-cry thanks !

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

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
#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?
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 441
Posts: 23834
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24429
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why have you got massive delays inside an interrupt service routine?
Logged

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

"The old Europe"
Offline Offline
Edison Member
*
Karma: 1
Posts: 2005
Bootloaders suck!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: June 30, 2011, 03:33:24 pm by madworm » Logged

• 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!

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 441
Posts: 23834
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Here you go

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

time to go sit in traffic ...
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 441
Posts: 23834
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You have internal pullups enabled, or external pullup/pulldown installed so the pin insn't floating? You need that for stable, predictable operation.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 441
Posts: 23834
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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() ?
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 441
Posts: 23834
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Pages: [1] 2   Go Up
Jump to: