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

Thread: Cannot get button to work with external interrupt

  1. #1

    Cannot get button to work with external interrupt

    Hi,

    I'm trying to get a button to work on a Teensy 2.0 with C + Makefile but to no avail.

    Here's the code:

    Code:
    #include <avr/io.h>
    #include <avr/interrupt.h>
    #include <util/delay.h>             // _delay_ms()
    #include "print.h"              // print debug output through usb port
    #include "macros.h"             // set_bit(), clear_bit(), LED_ON, LED_OFF
    
    int main(void)
    {
        usb_init();                  // init usb port to send debugging information
        _delay_ms(2000);
      
        DDRD &= ~(1 << DDD0);     // Clear the PD0 pin
    
        PORTD |= (1 << PORTD0);    // turn On the Pull-up
    
        EICRA |= (1 << ISC00);    // set INT0 to trigger on ANY logic change
        EIMSK |= (1 << INT0);     // Turns on INT0
    
        sei();                    // turn on interrupts
    
        while(1)
        {
            /*main program loop here */
        }
    }
    
    
    
    ISR (INT0_vect)
    {
        print("Button pressed\n");
    }
    I get weird results. When I turn on the Teensy I see no interrupts. When my hand gets near the wires of the button a deluge of interrupts occurs continuously. When I press and hold down the button the interrupts stop. When I release the button the interrupt deluge starts anew.
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	Button.jpg 
Views:	67 
Size:	113.4 KB 
ID:	7312  
    Last edited by bigpilot; 06-06-2016 at 01:37 PM.

  2. #2
    Senior Member
    Join Date
    Apr 2013
    Posts
    1,905
    Can't vouch for your code there but the behavior looks like the pullup isn't active and you are making a low quality cap sense device due the floating wires. by way of test suggest adding a 10k pullup on the pin with a resistor. If that steadies things then you'll need someone smarter than me to look at how you are enabling pullup.

  3. #3
    I have measured the voltages at the INT0 pin and when the button is not pressed the voltage reads 4.5V. When I press the button and keep it down it reads -0.003V.

    It seems to be responding in exact the opposite manner I would expect.

  4. #4
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,232
    Quote Originally Posted by bigpilot View Post
    I have measured the voltages at the INT0 pin and when the button is not pressed the voltage reads 4.5V. When I press the button and keep it down it reads -0.003V.

    It seems to be responding in exact the opposite manner I would expect.
    I can't explain the hand effect (paranormal event?), but with pin mode as input PULLUP, then the voltage at that pin should be Vcc. Your button seems hooked to ground,so when you push it, you should see 0v -- so that seems to be working as one would expect.

    It''s considered bad form to do print's in ISRs, and buttons bounce, so one button push can result in multiple interrupts... hence possible "weird" behavior

    EDIT: I dusted off my Teensy 2.0, and using make created and ran your example. it ran OK and printed lots of "Button pressed" as i jumpered D0 to GND, and when jumper was removed. (more bounces than a button push probably but showed that code was working.)
    Last edited by manitou; 06-07-2016 at 06:24 PM.

  5. #5
    Thanks for checking with your Teensy. I tried something similar and I noticed that when I connect a write between the D0 pin to VCC the interrupts stop, when I connect the same wire to GND the interrupts also stop. But when I have the wire connected to D0 floating the interrupts flow continiously. Were you using a breadboard similar to the one I was using? Also, I'm using the USB port to power the Teensy, no external power supply.

    When I connect the wire connected to the D0 pin to the push button the interrupts flow when the button is not depressed. When I press the button the interrupts stop.

    I'm trying to figure out if the pin might be tri-stated, but I'm not sure yet.
    Last edited by bigpilot; 06-09-2016 at 04:13 PM.

  6. #6
    Senior Member
    Join Date
    Jun 2013
    Location
    So. Calif
    Posts
    2,828
    @bigpilot... not wanting to use Teensyduino libraries but rather code for direct hardware access?

    Hand-near-wire = interrupts is simply this: That GPIO pin is an input and there is no chip-internal or external pull-up resistor. Thus, the pin is "floating" and is therefore high impedance. This makes an antenna. You body introduces electromagnetic noise that starts with the power wiring in the room. Need a pull-up as suggested. After that, you need to de-bounce the switch make/break due to mechanical effects of all switches. There are libraries to implement debounce.
    Last edited by stevech; 06-09-2016 at 06:20 PM.

  7. #7
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,232
    Yes, I was using a smaller breadboard (your photo shows lots of other devices that may somehow contribute). @stevech, the sketch he provided does put D0 in INPUT_PULLUP mode, so it is NOT floating. I too am curious why @bigpilot is using low-level access?? the equivalent is
    Code:
    void ding() {
      Serial.println("Button pressed");
    }
    
    void setup() {
      Serial.begin(9600);
      pinMode(5, INPUT_PULLUP);
      attachInterrupt(5,ding,CHANGE);
    }
    
    void loop() {}
    and for me both sketches work when button is pressed and/or released, and i get no continuous flow of interrupts.

  8. #8
    Senior Member
    Join Date
    Jun 2013
    Location
    So. Calif
    Posts
    2,828
    Internal pull-ups are weak.. like 30-40K.

Posting Permissions

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