
SunFounder Thales Kit for Raspberry Pi Pico, Release 1.0
How it works?
In the previous lesson, we simply read the button value. This time, we tried a flexible way of using buttons: interrupt
requests, or IRQs.
For example, you are reading a book page by page, as if a program is executing a thread. At this time, someone came
to you to ask a question and interrupted your reading. Then the person is executing the interrupt request: asking you
to stop what you are doing, answer his questions, and then let you return to reading the book after the end.
MicroPython interrupt request also works in the same way, it allows certain operations to interrupt the main program.
It is achieved through the following two statements (the highlighted 2 lines):
import
machine
import
utime
import
urandom
led
=
machine
.
Pin(
15
, machine
.
Pin
.
OUT)
button
=
machine
.
Pin(
14
, machine
.
Pin
.
IN)
def
button_press
(pin):
button
.
irq(handler
=
None
)
rection_time
=
utime
.
ticks_diff(utime
.
ticks_ms(), timer_light_off)
(
"Your reaction time was "
+
str
(rection_time)
+
" milliseconds!"
)
led
.
value(
1
)
utime
.
sleep(urandom
.
uniform(
5
,
10
))
led
.
value(
0
)
timer_light_off
=
utime
.
ticks_ms()
button
.
irq(trigger
=
machine
.
Pin
.
IRQ_RISING, handler
=
button_press)
Here, a callback function (button_press) is first defined, which is called an interrupt handler. It will be executed when
an interrupt request is triggered. Then, set up an interrupt request in the main program, it contains two parts:
trigger
and
handler
.
• In this program, the
trigger
is
IRQ_RISING
, which means that the value of the pin rises from low level to
high level (That is, pressing the button).
•
handler
is the callback function
button_press
we defined before.
In this example, you will find a statement
button.irq(handler=None)
in the callback function, which is equiv-
alent to canceling the interrupt.
In order to better understand the interrupt request, we change the above code to the following (Use the same circuit):
import
machine
import
utime
button
=
machine
.
Pin(
14
, machine
.
Pin
.
IN)
count
=
0
def
button_press
(pin):
(
"You press the button!"
)
utime
.
sleep(
1
)
button
.
irq(trigger
=
machine
.
Pin
.
IRQ_RISING, handler
=
button_press)
while
True
:
count
+=
1
(count)
utime
.
sleep(
1
)
3.4. Projects
61
Содержание Thales Kit
Страница 1: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 Jimmy SunFounder Jun 04 2021 ...
Страница 2: ......
Страница 4: ...ii ...
Страница 6: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 2 CONTENTS ...
Страница 10: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 6 Chapter 1 Introduction to Raspberry Pi Pico ...
Страница 12: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 8 Chapter 2 What is Included in This Kit ...
Страница 13: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 2 1 Components List 2 1 Components List 9 ...
Страница 42: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 38 Chapter 2 What is Included in This Kit ...
Страница 140: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 136 Chapter 3 For MicroPython User ...
Страница 164: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 160 Chapter 4 For Arduino User ...