Interrupts will not...

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
hybotics
Posts: 10
Joined: Tue Apr 03, 2018 2:58 am

Interrupts will not...

Post by hybotics » Mon Nov 26, 2018 12:49 pm

Hi,

I have been working with interrupts lately.

Which pins on the PyBoard are capable of being interrupt sources? I am using X17, X18, and X19 now, which do work as interrupt sources. However, X20, X21, and X22 do not seem to work as interrupts. It is also possible that I have something configured wrong for these pins. I have a rotary encoder with switch connected to them.

Code: Select all

from pyb import ExtInt, Pin
from time import sleep

interrupted = False
last_position = 0
position = 0
direction = 0
direction_text = ""

#	Setup the rotary encoder
def apin_callback(line):
	global interrupted, apin_clicked, bpin_clicked
	global direction
	
	if (apin_clicked and bpin_clicked):
		#	Rotated clockwise
		direction = 1
	elif (apin_clicked and (not bpin_clicked)):
		#	Rotated counter clockwise
		direction = -1
	else:
		direction = 0
	
	interrupted = True
	apin_clicked = True

	return

apin_clicked = False
apin_int = ExtInt("X20", ExtInt.IRQ_FALLING, Pin.PULL_UP, apin_callback)

def bpin_callback(line):
	global interrupted, bpin_clicked

	interrupted = True	
	bpin_clicked = True

	return

bpin_clicked = False
bpin_int = ExtInt("X21", ExtInt.IRQ_FALLING, Pin.PULL_UP, bpin_callback)

def encoder_switch_callback(line):
	global interrupted, encoder_switch_pressed
	
	interrupted = True
	encoder_switch_pressed = True
	
encoder_switch_pressed = False
encoder_switch_int = ExtInt("X22", ExtInt.IRQ_FALLING, Pin.PULL_UP, encoder_switch_callback)

#	Enable interrupts
apin_int.enable()
bpin_int.enable()
encoder_switch_int.enable()
8-Dale

User avatar
pythoncoder
Posts: 3119
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Interrupts will not...

Post by pythoncoder » Mon Nov 26, 2018 1:56 pm

It works here, and I believe all pins can raise IRQ's. Try this, using a jumper to gnd to pull the pin down

Code: Select all

from pyb import ExtInt, Pin

def cb(_):
    print('cb')

apin_int = ExtInt("X20", ExtInt.IRQ_FALLING, Pin.PULL_UP, cb)
You will get multiple lines for each connection because of contact bounce.
Peter Hinch

User avatar
dhylands
Posts: 2904
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Interrupts will not...

Post by dhylands » Mon Nov 26, 2018 6:13 pm

Just a note, The ExitInt hardware is based on "lines" where the line is the pin number of the port. X17, for example, is pin B3 which means it uses line 3. X22 is C3 which also line 3. So you can only use ExtInt for X17 or X22 and not both at the same time.

Also note that X17 is also the same as the USR pushbutton, so the internal pullup resistor is enabled by default.

hybotics
Posts: 10
Joined: Tue Apr 03, 2018 2:58 am

Re: Interrupts will not...

Post by hybotics » Mon Nov 26, 2018 7:04 pm

dhylands wrote:
Mon Nov 26, 2018 6:13 pm
Just a note, The ExitInt hardware is based on "lines" where the line is the pin number of the port. X17, for example, is pin B3 which means it uses line 3. X22 is C3 which also line 3. So you can only use ExtInt for X17 or X22 and not both at the same time.
I am guessing I will have to look at the MCU datasheet to know which pins are on the same line as another.

My rotary encoder is active low, so it may not present an edge to trigger on. I do not see a way to trigger on a low or high state.

Are all the "X" pins possible external interrupt sources? X23 and X24 do not exist, according to an error message.

8-Dale

User avatar
dhylands
Posts: 2904
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Interrupts will not...

Post by dhylands » Mon Nov 26, 2018 10:04 pm

You can look at the quick reference for the pyboard here:
http://docs.micropython.org/en/latest/p ... ckref.html

X23 and X24 are power and ground, so there is no GPIO pin associated with it.

If the pin has a letter/number like C3 associated with X22 then you should be able to use ExtInt with it.

X1-12, Y1-12, X17-22 and P2-5 (although these are also attached to the LEDs) are all associated with GPIO pins.

hybotics
Posts: 10
Joined: Tue Apr 03, 2018 2:58 am

Re: Interrupts will not...

Post by hybotics » Tue Nov 27, 2018 3:09 am

dhylands wrote:
Mon Nov 26, 2018 10:04 pm
X23 and X24 are power and ground, so there is no GPIO pin associated with it.
They are silk screened on the board, so that was a mistake.
dhylands wrote:
Mon Nov 26, 2018 10:04 pm
If the pin has a letter/number like C3 associated with X22 then you should be able to use ExtInt with it.

X1-12, Y1-12, X17-22 and P2-5 (although these are also attached to the LEDs) are all associated with GPIO pins.
Thanks. I changed my rotary encoder + switch to pins X1, X2, and X3. These do not have any conflict as far as I know at present.

These encoders have some serious bounce to them, so I have to find a good debouncer algorithm. I am going to have to put the debouncer into the ISR though, which I am not sure is a good idea.

8-Dale

User avatar
pythoncoder
Posts: 3119
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Handling incremental encoders

Post by pythoncoder » Tue Nov 27, 2018 10:06 am

You might like to look here where there is a simple IRQ based solution which will cope with serious bounce.

It is also possible to use the Timer class to decode incremental encoders.
Peter Hinch

User avatar
dhylands
Posts: 2904
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Interrupts will not...

Post by dhylands » Tue Nov 27, 2018 4:43 pm

Here's some sample code for setting up a timer to do quadrature decoding:
https://github.com/dhylands/upy-example ... encoder.py

This example is a bit simpler: https://github.com/dhylands/upy-example ... ncoder2.py

The first example allowed to use some GPIO outputs for testing.

IIRC you need to use CH1 and CH2 of a timer to get hardware quadrature to work.

Post Reply