Interrupts on MEGA2560

I have a working sketch ( written by Richard Visokey - AD7C) for a VFO (with a DDS AD9850 ) on a Arduino UNO .

I try to adapt and rewrite this sketch for a MEGA2560
I have problems with the interrup . On the UNO Richard use D2 & D3 as connections for the Rotary encoder . I will use D18 & D19 on the MEGA .
This are the original line for the UNO

Rotary r = Rotary(2,3);

PCICR |= (1 << PCIE2);
PCMSK2 |= (1 << PCINT18) | (1 << PCINT19);
sei();

ISR(PCINT2_vect) {
unsigned char result = r.process();
if (result) {

I changed the Rotary and pinmode lines to 18 & 19 but I can still not use the rotary encoder.
Can someone help me change the code for a MEGA as I do not understand the meaning of PCICR.......PCINT2......

All help is most welcome ,

Thanks , Marco

f8voa54:
Can someone help me change the code for a MEGA as I do not understand the meaning of PCICR.......PCINT2......

Start with the ATmega2560 Datasheet that you can download from Here..

Thank you , I have downloaded several data sheets , and I did it again now .
Sorry but , your download is a simple device overview . What do I have to do whit that ?
Marco.

f8voa54:
Sorry but , your download is a simple device overview . What do I have to do whit that ?
Marco.

Click on the "Documents" tab at the link I provided. There you can download a 435-page datasheet that completely describes all aspects of the chip.

Ohhhhh , OK , that's a lot of reading , at my age it will take weeks or months before I will see the end of this .

Thank you

Marco ,

Yea, it's big. Only look at the chapters you need to :slight_smile: The register naming conventions are very similar to the processor in the Uno, there's just more of them. So, a comparison of the relevant sections between the ATmega2560 and ATmega328P datasheets would take a lot less time than reading the whole thing. A Google search fro 'pin change interrupts atmega2560' might also prove helpful.

I removed the old UNO code :
Rotary r = Rotary(2,3);

PCICR |= (1 << PCIE2);
PCMSK2 |= (1 << PCINT18) | (1 << PCINT19);
sei();

ISR(PCINT2_vect) {
unsigned char result = r.process();
if (result) {

and replaced it by :

attachInterrupt(digitalPinToInterrupt(18),Down,CHANGE);
attachInterrupt(digitalPinToInterrupt(19),Up,CHANGE);
}
void Up(){
unsigned char result = r.process();
result == DIR_CW;
Opstart=Opstart+increment;
delay(100);
Serial.println(increment);Serial.println(Opstart);

};
void Down(){
unsigned char result = r.process();
result == DIR_CCW;
Opstart=Opstart-increment;

I made this little code . Interrupts are working now but the frequency change ( up or down ) is more a random logic.
Istead a change by 100 ( should be 100, 1000 or 10000 following the status of some ports ) the change is mostly a multiple of 100 ( very bizar )

I don't know if the new lines are all I need or if there is more to control the interrupts.

Marco

Quadrature encoders don't have "up" and "down" outputs. That is why you call r.process() to check the state of the input pins and return a direction.

Keep the old handler and change the name. Attach it to both pins.

attachInterrupt(digitalPinToInterrupt(18),Changed,CHANGE);
attachInterrupt(digitalPinToInterrupt(19),Changed,CHANGE);
}

void Changed(){
  unsigned char result = r.process();
  if (result)

Thank you John ,
Wunderful ,
So simple and elegant

Thanks a lot to all for all your efforts ,

Marco

The original code used pin change interrupts (PCINT) - on the Uno every pin is usable with pin change
interrupts, on the Mega only a few pins work with PCINTs. So changing to direct INT handlers as you've
done is the way to go - the Mega has more INT-capable pins than the Uno so there's less need for pin change
interrupts anyway.

Pin change interrupts monitor a whole port (upto 8 pins) simultaneously, but don't tell you which pins
changed.

OK , Mark ,
I did not know that .
I'll remove those lines from my sketch .
Thanks for the info

Have a nice evening

Marco