Expert Help to Upgrade ooPinChangeInt library to work on Mega2560 pins A6-A15

I'm writing a new FlowCounters library for flow meters, hall devices, etc. It is based on a library, ooPinChangeInt. However, on a Mega 2560, this library currently only works on pins 10-13, and 50-53. Pin 13 is the userLED, so it doesn't work well. Pins 50-53 are the SPI Bus. This leaves only three pins 10-12. The analog pins A6-A15 should work, but there is something wrong with ooPinChangeInt. I need expert help to fix this complicated library. I've attached the ooPinChangeInt library, my new FlowCounters library, and a sketch "MEGA_FlowCounters_Pin_Check.ino" to check which pins work.

Thanks,
-Turgo

ooPinChangeInt.zip (48.5 KB)

FlowCounters.zip (8.49 KB)

MEGA_FlowCounters_Pin_Check.ino (4.08 KB)

See 12.3.6 and 12.3.10.
It doesn't look to me that all the pins you ask about support PCINT.

Hi Crossroads,

What does 12.3.6 and 12.3.10 refer to?

The 2560 datasheet.
"12.3 Alternate Port Functions
Most port pins have alternate functions in addition to being general digital I/Os. Figure 12-5 on
page 76 shows how the port pin control signals from the simplified Figure 12-2 on page 71 can
be overridden by alternate functions.
The overriding signals may not be present in all port pins, but the figure serves as a generic description applicable to all port pins in the AVR microcontroller family."

Hi Crossroads,

The pin mapping image on the Arduino web site shows the analog pins A6-A15 mapped to port K which has PCINT capability. What do you make of that mapping image?

Hmm, I was considering ADC0 thru ADC15 of the 2560 chip, had not considered that the mapping to the board might be calling them something else.

Have you had a chance to look under the hood of ooPinChangeInt library to see why A6-A15 aren't working?

You need a software guru for that, I just glaze over trying to read low level stuff like libraries.

Are there any guru's out there who could help?

Did anyone get this working? I am looking to use this on A13-A15 and am not able to get it to work.

Hi Stafford,

It still doesn't work. The non-object oriented version does work, so it can be done. We just need a better programmer to fix the ooPinChangeInt library. GreyGnome is the original author, but I believe he has moved on. I'd sure like this library to work!

I am by no means a coding expert, but I went and did a side by side comparison of the oopinchangeint.h and pinchangeint.h and think I found the issue. In the PCIntPort constructor, the oopinchangeint file reduced this to 2 arguments:
PCintPort(int index,volatile uint8_t& maskReg) :
portInputReg(*portInputRegister(index + 2)),
portPCMask(maskReg),
firstPin(NULL),
PCICRbit(1 << index),
As you can see the index variable is used for the port register and the PCICRbit by incrementing by 2. This works fine on Ports A, B, C and D because they are different by 2. For example, in port B the index is 2 and the pcIndex is 0.
PCintPort portB=PCintPort(2, 0,PCMSK0); // port PB==2 (this is from pinintchange.h)
However, for Ports J and K they are not different by 2:
PCintPort portJ=PCintPort(10,1,PCMSK1); // port PJ==10
PCintPort portK=PCintPort(11,2,PCMSK2); // port PK==11
So, I reverted back to the 3 argument PCintPort from the Pinchangeint library and it seem to be working now. I have attached my updated file. Give it a try and see if it solves your issue.

Stafford

ooPinChangeInt.h (17.1 KB)

Success! thanks so much for your fix Stafford! Here's what worked for me:

FlowCounters flow10(10,.5); 
FlowCounters flow11(11,.5); 
FlowCounters flow12(12,.5); 
FlowCounters flow13(13,.5); 
FlowCounters flow14(14,.5); //didn't work, probably because of the serial connection.
FlowCounters flow15(15,.5);
FlowCounters flow50(50,.5);
FlowCounters flow51(51,.5);
FlowCounters flow52(52,.5);
FlowCounters flow53(53,.5);
FlowCounters flowA6(A6,.5); //Port F didn't work
FlowCounters flowA7(A7,.5); //Port F didn't work
FlowCounters flowA8(A8,.5);
FlowCounters flowA9(A9,.5);
FlowCounters flowA10(A10,.5);
FlowCounters flowA11(A11,.5);
FlowCounters flowA12(A12,.5);
FlowCounters flowA13(A13,.5);
FlowCounters flowA14(A14,.5);
FlowCounters flowA15(A15,.5);

There may be one more fix to make. A6 and A7 on port F did not work. Would you mind looking at it also?

I think you might be out of luck on port F. If you look at the Mega support on the oopinchangeint google site, they don't include Port F:
"Mega and friends are using port B, J and K for interrupts. B is working without any modifications.
J is mostly useless, because of the hardware UART. I was not able to get pin change notifications from the TX pin (14), so only 15 left. All other (PORT J) pins are not connected on the Arduino boards."

There is a discrepency on the pinout here:

The diagram shows that pins A6 and A7 are PCINT, but the table below doesn't include them. I looked at the ATMega2560 data sheet and it doesn't mention Port F having PCINT pins. So, I don't think Port F has any interrupts that you can use.

Thanks Stafford, for your though investigation. The fix to ooPinChangeInt for MEGA is complete. I'll contact the original author, GreyGnome, and request that he update the google code site. I also have a FlowCounters library that relies on ooPinChangeInt, which I will now release.