-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a61a4e3
commit 959ca2e
Showing
1 changed file
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
```bash | ||
视频教程地址: | ||
哔哩哔哩bilibili:树莓派爱好者基地、玩派VLOG | ||
``` | ||
|
||
## 一、概述 | ||
|
||
上一期用PICO点亮了LED灯,这一期准备用按键来控制LED灯的点亮。为了更有趣一些,所以准备用三种方式来实现按键控制LED。 | ||
编程语言: micropython。 | ||
|
||
PICO接口图 | ||
 | ||
按键图和原理图 | ||
 | ||
 | ||
## 二、开始 | ||
### 1、方式1(按键控制电路闭合) | ||
最朴实无华的方式,LED正极连接PICO的GP0口,接口给高电平,通过按键控制电路是否闭合,闭合时点亮LED。 | ||
 | ||
 | ||
|
||
### 2、方式2(按键逻辑控制点灯) | ||
先看看原理图 | ||
 | ||
再看看程序 | ||
|
||
```python | ||
from machine import Pin | ||
import time | ||
|
||
led=Pin(0,Pin.OUT) | ||
switch=Pin(1,Pin.IN,Pin.PULL_UP) | ||
|
||
while True: | ||
if switch.value()==0: | ||
led.value(1) | ||
else: | ||
led.value(0) | ||
|
||
``` | ||
|
||
这里GP0口依然与LED正极相连。按键一端和GP1口相连,一端和GND相连。GP1给一个上拉电阻,一直保持高电平状态,当按键按下时,电平变为低电平,触发条件,给GP0高电平,点亮LED。 | ||
 | ||
 | ||
|
||
### 3、方式3(按键逻辑控制点灯+外部中断) | ||
在方式2的基上做了一些改良,把while True循环检测改成了外部中断。电路连接完全不变。外部中断触发条件设置为下降沿触发,按键按下,GP1由高电平变为低电平,触发中断,点亮LED | ||
看一下程序 | ||
|
||
```python | ||
from machine import Pin | ||
import time | ||
|
||
led=Pin(0,Pin.OUT) | ||
switch=Pin(1,Pin.IN,Pin.PULL_UP) | ||
led.off() | ||
|
||
|
||
def on_led(switch): | ||
led.on() | ||
time.sleep(5) | ||
led.off() | ||
|
||
switch.irq(on_led,Pin.IRQ_FALLING) | ||
|
||
``` | ||
 |