Counting two interupt pulses at once.

Hi all
Ive tried counting two pulses at once using interupts. if i count one pulse it works very well. at a second pulse there are both nothing close.. Ive tried both
with a teensy 3.5 and 3.6.
They seem to be interfering with each other.

I have also tried linking up three teensy's and having two count one different pulse and the third talk to the serial monitor.. But only print once the counting has stopped.

Has anyone been able to do this? or somthing like this? Im trying to build a two or three channel counter

TIA of any feedback
 
It might help to understand what you are doing. i.e. example program that shows the issue.

For example does your program look something like:

Code:
#define PIN1 1
#define PIN2 2
uint32_t pin1_count = 0;
uint32_t pin2_count = 0;

void pin1ISR() {pin1_count++;}
void pin2ISR() {pin2_count++;}

void setup() {
...
    attachInterrupt(PIN1, &pin1ISR, FALLING);
    attachInterrupt(PIN2, &pin2ISR, FALLING);
...

If you are doing something like that, than you might want to see which IO PORT both of your pins are on. If they are on the same IO port than they will share one underlying Interrupt handler function.

That is when you do attachInterrupt, the system actually installs Interrupt vector handlers for all of the IO ports.
Specifically within the call to attachInterrupt you have:
Code:
	attachInterruptVector(IRQ_PORTA, port_A_isr);
	attachInterruptVector(IRQ_PORTB, port_B_isr);
	attachInterruptVector(IRQ_PORTC, port_C_isr);
	attachInterruptVector(IRQ_PORTD, port_D_isr);
	attachInterruptVector(IRQ_PORTE, port_E_isr);
When these interrupts happen the underlying handler does:
Code:
#define PORT_ISR_FUNCTION_CLZ(port_name) \
	static void port_ ## port_name ## _isr(void) {            \
		uint32_t isfr = PORT ## port_name ##_ISFR;            \
		PORT ## port_name ##_ISFR = isfr;                     \
		voidFuncPtr* isr_table = isr_table_port ## port_name; \
		uint32_t bit_nr;                                      \
		while(isfr) {                                         \
			bit_nr = __builtin_ctz(isfr);                     \
			isr_table[bit_nr]();                              \
			isfr = isfr & (isfr-1);                           \
			if(!isfr) return;                                 \
		}                                                     \
	}
// END PORT_ISR_FUNCTION_CLZ

#if defined(KINETISK)
PORT_ISR_FUNCTION_CLZ(A)
PORT_ISR_FUNCTION_CLZ(B)
PORT_ISR_FUNCTION_CLZ(C)
PORT_ISR_FUNCTION_CLZ(D)
PORT_ISR_FUNCTION_CLZ(E)
Specifically it looks through the mask of interrupt pins that are set and then finds the bits that are on and sees which ones that are set and calls off to the appropriate user function, like pin1ISR() above.

So question is does your code work better when both pins are on the same port or different? If your program is such that only your pins are using the interrupts and your pins are on two different ports. You could replace the system functions with your own, which simply increments your counter and clears the interrupt, by using attachInterruptVector. You will need to setup everything first or maybe cheat, by calling attacthInterrupt first for the two pins and then overwrite the vectors.

If you are still losing interrupts, you might also then play with the interrupt priority of these interrupts to a higher priority like:: NVIC_SET_PRIORITY(IRQ_PORTA, 32);
The lower the number the higher the priority.

Again not sure if this helps or not.
 
Back
Top