树莓派5 LED项目与I²C LCD使用指南
1. 树莓派5 LED项目
1.1 基础代码与功能
首先,我们来看一段基础代码,它实现了LED端口的配置和二进制计数显示功能。
from time import sleep
PORT = [9,10,22,27,17,4,3,2]
# This function initializes the port list PORT[]
def Configure():
for i in range(8):
PORT[i] = LED(PORT[i])
# This function sends 8-bit data (0 to 255) to the PORT
def Port_Output(x):
b = bin(x)
b = b.replace("0b", "")
diff = 8 - len(b)
for i in range (0, diff):
b = "0" + b
for i in range (0, 8):
if b[i] == "1":
PORT[i].on()
else:
PORT[i].off()
return
# Main program loop. Count up in binary every second
cnt = 0
Configure()
while True:
Port_Output(cnt)
sleep(1)
cnt = cnt + 1
超级会员免费看
订阅专栏 解锁全文
2258

被折叠的 条评论
为什么被折叠?



