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

Thread: Configuring Extenal Interrupts (Teensy LC)

  1. #1
    Junior Member
    Join Date
    Mar 2020
    Posts
    8

    Configuring Extenal Interrupts (Teensy LC)

    Flipping the state of Pin 0 with this script does not invert the LED. Any help appreciated:
    Code:
    #include <avr/io.h>
    #include <avr/interrupt.h>
    
    volatile bool LED_State = LOW;
    
    void setup() {
      // put your setup code here, to run once:
        pinMode(0, INPUT);
        attachInterrupt(digitalPinToInterrupt(0), startBit_isr, FALLING);
      
        pinMode(23, OUTPUT); // LED for debugging
        LED_State = HIGH;
        digitalWrite(23, LED_State);
        sei();
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
        delay(1000);
    }
    
    void startBit_isr()
    {
        LED_State = !LED_State;
        digitalWrite(23, LED_State);
    }

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    11,489
    Is there an LED on pin #23? The Teensy LED is on LED_BUILTIN or 13.

    Also depending on how noisy the falling edge is delivered it may repeat multiple times - an even number of times will result in it staying off with the ON states being super short duration.
    Last edited by defragster; 04-02-2020 at 07:28 PM.

  3. #3
    Senior Member PaulS's Avatar
    Join Date
    Apr 2015
    Location
    Netherlands
    Posts
    136
    Perhaps it's better to define pinMode(0, INPUT) as pinMode(0, INPUT_PULLUP) to make sure the input pin is not floating.
    How are you exactly flipping the state of Pin 0?

  4. #4
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    11,489
    This code works on T_3.6 for the Pin 13==LED_BUILTIN LED with a switch on Pin 2 to GND - note added INPUT_PULLUP to pin 2.
    Code:
    volatile bool LED_State = LOW;
    
    void setup() {
    	Serial.begin(115200);
    	while (!Serial && millis() < 4000 );
    	Serial.println("\n" __FILE__ " " __DATE__ " " __TIME__);
    	pinMode(2, INPUT_PULLUP);
    	attachInterrupt(digitalPinToInterrupt(2), startBit_isr, FALLING);
    
    	pinMode(LED_BUILTIN, OUTPUT); // LED for debugging
    	LED_State = HIGH;
    	digitalWrite(LED_BUILTIN, LED_State);
    }
    
    void loop() {
    	delay(1000);
    }
    
    void startBit_isr()
    {
    	LED_State = !LED_State;
    	digitalWriteFast(LED_BUILTIN, LED_State);
    }
    UPDATED <edited> :: Not all T_LC pins are interrupt capable : moving to Pin #2 works where Pin 0 does not based on the back of the card as noted below.
    Last edited by defragster; 04-02-2020 at 09:30 PM.

  5. #5
    Junior Member
    Join Date
    Mar 2020
    Posts
    8
    Thanks for the input.

    For clarity:

    Yes my LED is on Pin 23 (I am using 13 for TPM0 count input, not in the example script).
    I have an external 1K pullup resistor driving a 100uF cap in Pin 0.

    I can see Pin 0 going up and down on my scope.

    I have tried defragster's code and though the built-in LED lights it does not respond to changes of the input. I do get the serial burst. (I like that I'll use it again.)

    A-ha! Tried using Pin 4 and all works! What a waste of time.
    Pin 0 works fine as output. Maybe not supported as interrupt on LC?

    Anyway I'm all happy now. Thanks everyone.


    On a different track whilst we're in flow...

    When I upload to the Teensy the connection to Teensy Serial Monitor is dropped, as expected. Now I seem to remember when I was using Arduinos last, the Serial monitor came live again after the upload completed. It doesn't do this on the Teensy, which calls for some timely mouse work. Is this normal?

    Also my uploads ALWAYs request a button push. However if I wait about 10 seconds everything continues correctly.

    (I run Win7 which may speak volumes - but it's OK on the Arduino Nano).

  6. #6
    Senior Member+ MichaelMeissner's Avatar
    Join Date
    Nov 2012
    Location
    Ayer Massachussetts
    Posts
    3,670
    Note, on the LC, not all pins can be attached to interrupts. If you turn to the back of the pinout card that came with the LC, you will see that pins 0, 1, 16/A2, 17/A3, 18/A4, 19/A5, 24/A10, 25/A11, and 26/A12 do not have an 'INT' next to the pin. This means you can't do an attachInterrupt on this particular pin. On all other ARM teensys, the digital pins can be used with attachInterrupt.

    The online version of the pinout card is at: https://www.pjrc.com/teensy/pinout.html.

  7. #7
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    11,489
    Quote Originally Posted by MichaelMeissner View Post
    Note, on the LC, not all pins can be attached to interrupts. If you turn to the back of the pinout card that came with the LC, you will see that pins 0, 1, 16/A2, 17/A3, 18/A4, 19/A5, 24/A10, 25/A11, and 26/A12 do not have an 'INT' next to the pin. This means you can't do an attachInterrupt on this particular pin.
    Good catch - that detail wasn't referenced here either . I had a ready T_3.6 and it worked … also worked moving to T_4.0 … I have a properly usable _T_LC in arm's reach

    Moving to pin #2 works:
    Code:
    // …
    	pinMode(2, INPUT_PULLUP);
    	attachInterrupt(digitalPinToInterrupt(2), startBit_isr, FALLING);
    // ...
    p#4 above edited

Posting Permissions

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