Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Simple Interrupt Problem

    Scheduled Pinned Locked Moved WiPy 2&3
    7 Posts 3 Posters 2.0k Views 6 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • K Offline
      KMcLoud
      last edited by KMcLoud

      I was working on a significantly more complex programs and having some problems that I traced back to the fact that the interrupt wasn't firing.

      I've written a new, extremely simple program just to prove to myself that I can make interrupts work, and its not going so well.

      it uses the user button on the expansion board to trigger interrupts to increment a counter (or at least that's the idea)

      here's the contents of boot.py, there is no main.py:

      import micropython
      from machine import Pin
      
      micropython.alloc_emergency_exception_buf(100)
      
      int_counter =0
      
      def int_handler():
          global int_counter
          int_counter+=1
      
      
      button=Pin('GP17', mode=Pin.IN, pull=Pin.PULL_UP)
      button.irq(trigger=Pin.IRQ_RISING|Pin.IRQ_FALLING, handler=int_handler)
      

      I know the pin setup is working because when I go to the repl and run button() I get a value that toggles as I press and release the button

      I know the handler function works because when I manually run int_handler(), int_counter increments.

      Further, I suspect the trigger is working because when I press the button for the first time, the heartbeat LED blinks very rapidly for a couple seconds. However it won't do that again unless the button.irq() method is re-called.

      However, when I press the button, int_counter does not increment.

      Any idea what's going on here? I'm stumped

      1 Reply Last reply Reply Quote 0
      • L Offline
        LoneTech
        last edited by

        Use triple backticks ``` for code blocks. Pin interrupt handlers are called with the pin as an argument, so you need to add an argument for int_handler.

        K 1 Reply Last reply Reply Quote 1
        • K Offline
          KMcLoud
          last edited by

          This post is deleted!
          1 Reply Last reply Reply Quote 0
          • K Offline
            KMcLoud
            last edited by

            This post is deleted!
            1 Reply Last reply Reply Quote 0
            • K Offline
              KMcLoud @LoneTech
              last edited by

              @LoneTech

              Thank you so much, that fixed it!

              I'm hugely supportive of all the awesome work you guys are doing!

              Is there any way I can contribute to the docs to make that more clear? because I read them over a couple times never picked that up.

              a note on the "make a post" page indicating you need to use ``` instead of the BBcode would be super helpful too, any way I could help with that?

              1 Reply Last reply Reply Quote 1
              • H Offline
                HellDesk
                last edited by

                @KMcLoud or @LoneTech Any chance you could share some working interrupt code? I'm stuck trying to make this work myself... Thanks!

                1 Reply Last reply Reply Quote 0
                • K Offline
                  KMcLoud
                  last edited by KMcLoud

                  This works:

                  import micropython
                  from machine import Pin
                  
                  micropython.alloc_emergency_exception_buf(100)
                  
                  int_counter =0
                  
                  def int_handler(pin_obj):
                      global int_counter
                      int_counter+=1
                  
                  
                  button=Pin('GP17', mode=Pin.IN, pull=Pin.PULL_UP)
                  button.irq(trigger=Pin.IRQ_RISING|Pin.IRQ_FALLING, handler=int_handler)
                  

                  The key is that the "os" passes the pin object to the callback function as an argument. However, if your callback function isnt expecting any arguments, this causes an exception. Since the exception occurs outside of the main program execution its a little confusing to debug.

                  1 Reply Last reply Reply Quote 1

                  Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                  Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                  With your input, this post could be even better 💗

                  Register Login
                  • 1 / 1
                  • First post
                    Last post

                  Pycom on Twitter

                  Pycom