Page 1 of 2

mega2560 interupt use

Posted: Mon Mar 02, 2020 6:54 pm
by hello
hi everyone;
I just did some testing on 2560 and it didn't look too good for some things, ........................
I didn't look at speeduino code too close and maybe there are some nuansses(sp), but if it uses interupts.........
cut and paste code if interested
place wire between 7 and desired interupt
need arduino ide, open monitor

cheers!



void setup() {
// put your setup code here, to run once:
//2, 3, 18, 19, 20, 21 mega2560
//ON PIN 18,falling, goes to blink, on 19,RISING&FALLING, it oscillates between blink&help
//2, 3 seem more stable
//20 seems more stable than 21, 21 oscillates between blink&help

pinMode(20,INPUT);//HALL1
pinMode(21,INPUT);//HALL2

attachInterrupt(digitalPinToInterrupt(20), blink, FALLING);
attachInterrupt(digitalPinToInterrupt(21), help, FALLING);

pinMode(7,OUTPUT);

//CHANGE FREQUENCY PWM
int myEraser = 7; // this is 111 in binary and is used as an eraser.....4B IS PORT4, relates to pin 6,7,8
TCCR4B &= ~myEraser; // this operation (AND plus NOT), set the three bits in TCCR4B to 0
int myPrescaler = 5; // this could be a number in [1 , 6]. In this case, 3 corresponds in binary to 011.
TCCR4B |= myPrescaler; //this operation (OR), replaces the last three bits in TCCR2B with our new value 011
Serial.begin(9600); // open the serial port at 9600 bps:

//conclusion pins 2,3 may be suitable for external interupts
//
}

void loop() {
// put your main code here, to run repeatedly:
analogWrite(7, 127);

}

void blink() {
Serial.println("BLINK");
}

void help() {
Serial.println("help");
}

Re: mega2560 interupt use

Posted: Mon Mar 02, 2020 9:44 pm
by noisymime
What are you actually trying to achieve here?

You're using the PWM output from pin 7 to trigger the external interrupts (Which work completely fine on any of the external enabled pins) and then printing something out. The obvious problem I see with this is that you're going to fill the serial buffer very quickly and then the calls to Serial.println are going to start blocking.

What frequency are you setting the PWM to?

Re: mega2560 interupt use

Posted: Tue Mar 03, 2020 2:08 pm
by hello
trying to see how the board behaves.
they may work fine under certain circumstances but under these conditions they behave irradically.
some pins behave one way-others in another but they are both suppose to be the same.

1rpm=0.017 hz 8500rpm, redline 141.7hz

Re: mega2560 interupt use

Posted: Tue Mar 03, 2020 4:18 pm
by JHolland
I'm not sure that I understand your set up, can you post a circuit diagram? If you are generating interrupts at 141Hz(7.1ms) then you don't have enough time between interrupts to output the data. At 9600baud then "blink" is going to take a minimum of 5*2*10 *(1/9600) = 10.4ms

Re: mega2560 interupt use

Posted: Wed Mar 04, 2020 2:32 pm
by hello
it outputs just fine

Re: mega2560 interupt use

Posted: Wed Mar 04, 2020 2:33 pm
by hello
as in the first post, just get a mega2560 board
run the prog. and use a wire between pins

Re: mega2560 interupt use

Posted: Fri Mar 06, 2020 8:17 am
by NickZ
as you have been told, your test method doesn't work correctly. the ATMEGA2560 is and does run perfectly fine.
there is no issue.

Re: mega2560 interupt use

Posted: Fri Mar 13, 2020 11:02 pm
by hello
ha!, ha!, ha!, I wish that were true would have saved me alot of time.
there great boards have a lot of functionality, but ya have to know how to use em.

Re: mega2560 interupt use

Posted: Fri Mar 13, 2020 11:31 pm
by noisymime
OK, run this sketch
Code: Select all
volatile unsigned long input1Time1;
volatile unsigned long input1Time2;
volatile unsigned long input2Time1;
volatile unsigned long input2Time2;

void setup() {
// put your setup code here, to run once:
//2, 3, 18, 19, 20, 21 mega2560
//ON PIN 18,falling, goes to blink, on 19,RISING&FALLING, it oscillates between blink&help
//2, 3 seem more stable
//20 seems more stable than 21, 21 oscillates between blink&help

pinMode(20,INPUT);//HALL1
pinMode(21,INPUT);//HALL2

attachInterrupt(digitalPinToInterrupt(20), blink, FALLING);
attachInterrupt(digitalPinToInterrupt(21), help, FALLING);

pinMode(7,OUTPUT);



//CHANGE FREQUENCY PWM
int myEraser = 7; // this is 111 in binary and is used as an eraser.....4B IS PORT4, relates to pin 6,7,8
TCCR4B &= ~myEraser; // this operation (AND plus NOT), set the three bits in TCCR4B to 0
int myPrescaler = 5; // this could be a number in [1 , 6]. In this case, 3 corresponds in binary to 011.
TCCR4B |= myPrescaler; //this operation (OR), replaces the last three bits in TCCR2B with our new value 011
Serial.begin(9600); // open the serial port at 9600 bps:

//conclusion pins 2,3 may be suitable for external interupts
//
}

void loop() {
// put your main code here, to run repeatedly:
analogWrite(7, 127);

Serial.print("Input 1 period: ");
Serial.println((input1Time2-input1Time1));
Serial.print("Input 2 period: ");
Serial.println((input2Time2-input2Time1));

}

void blink() {
//Serial.println("BLINK");
input1Time1 = input1Time2;
input1Time2 = micros();
}

void help() {
//Serial.println("help");
input2Time1 = input2Time2;
input2Time2 = micros();
}
It functions EXACTLY as I'd expect it to. The value output is 32640 uS, which is matched exactly with what I'm seeing on the scope.

Re: mega2560 interupt use

Posted: Sat Mar 14, 2020 2:59 pm
by hello
its not about time though
..................
if during program run, and it will run...................on & on..............
.................
change the wire from one interrupt to another
monitor output should change consistently
if you disconnect wire completely does it give an output?
if it does, is the board defective?