|
【MicroPython】LAB03 - GPIO 输入
[复制链接]
本帖最后由 slotg 于 2016-5-6 10:12 编辑
上个 LAB 定义的 ByteOut(x) 函数实现了一次 8 位的输出功能,我们依类似的作法再定义一个 ByteIn(x) 一次读取 8 位的输入函数:
- def ByteIn():
- x=0
- n=0
- for i in [0x01,0x02,0x04,0x8,0x10,0x20,0x40,0x80]:
- if keys[n].value()==0:
- x |= i # key on
- n+=1
- return x
复制代码
这次的 LAB 将 X1 .. X8 设定为输入功能并在管脚上接上 8 个开关,主回圈中将开关的状态显示在相对应的 LED 上,开关 ON 时 LED 亮,开关 OFF 时 LED 灭。
main.py
- # main.py LAB03
- from pyb import Pin
- ########################################################
- def ByteOut(x):
- n=0
- for i in [0x01,0x02,0x04,0x8,0x10,0x20,0x40,0x80]:
- if x&i:
- leds[n].value(0) # LED on
- else:
- leds[n].value(1) # LED off
- n+=1
- ########################################################
- def ByteIn():
- x=0
- n=0
- for i in [0x01,0x02,0x04,0x8,0x10,0x20,0x40,0x80]:
- if keys[n].value()==0:
- x |= i # key on
- n+=1
- return x
- ########################################################
- leds = [Pin(i, Pin.OUT_OD) for i in ['Y1','Y2','Y3','Y4','Y5','Y6','Y7','Y8']]
- keys = [Pin(i, Pin.IN, Pin.PULL_UP) for i in ['X1','X2','X3','X4','X5','X6','X7','X8']]
- for i in range (8): # all LED off
- leds[i].value(1)
- while True:
- ByteOut(ByteIn())
复制代码
运行结果:
【MicroPython】LAB01 - 流水灯
【MicroPython】LAB02 - 流水灯2
|
赞赏
-
1
查看全部赞赏
-
|