Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 4 of 4

Thread: Counting two interupt pulses at once.

  1. #1

    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

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    23,051
    Quote Originally Posted by threephase03 View Post
    Has anyone been able to do this?
    I'm not even sure I know what "this" really is. Any chance you can show us the code you're trying and give some more specific detail about these pulses (maybe a oscilloscope screenshot)?

  3. #3
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    7,920
    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.

  4. #4
    Thanks for the response, i'll respond some code soon. but i think Kurte might have solved my problem.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •