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