我有一些LED连接到我的Pi的GPIO引脚,我想在LED上显示,比方说一个8位二进制数。因此,如果数字是11110000,那么我希望前四个LED亮起,后四个LED熄灭。
发布于 2019-11-21 09:57:42
您可以从将二进制数转换为可用的值列表开始,以决定8个LED中的哪一个保持亮起。
state = 0b11110000
expanded = []
for x in range(8):
val = state & 0x01
state = state >> 1
expanded.append(val)
expanded = list(reversed(expanded))
print(expanded) # [1, 1, 1, 1, 0, 0, 0, 0]
使用expanded
,您可以决定哪些LED应该保持亮起。
发布于 2019-11-21 11:23:05
我想出来了,但在这种方法中可能有点复杂。因此,我的号码表示为0b1000101
temp = 0b1000101
binF = temp[2:].zfill(7) #prints 1000101
binF1 = temp[3:].zfill(6) #prints 000101
binF2 = temp[4:].zfill(5) #prints 00101
binF3 = temp[5:].zfill(4) #prints 0101
binF4 = temp[6:].zfill(3) #prints 101
binF5 = temp[7:].zfill(2) #prints 01
binF6 = temp[8:].zfill(1) #prints 1
#Now we want the most significant bit
binFA = binF[1:].zfill(1) #prints 1
binF1A = binF1[1:].zfill(1) #prints 0
binF2A = binF2[1:].zfill(1) #prints 0
binF3A = binF3[1:].zfill(1) #prints 0
binF4A = binF4[1:].zfill(1) #prints 1
binF5A = binF5[1:].zfill(1) #prints 0
binF6A = binF6[1:].zfill(1) #prints 1
从这里我们可以将这些值分配给特定的GPIO引脚
if binFA == '1':
GPIO.output(21, 1)
else:
GPIO.output(21, 0)
根据需要根据位数设置所有GPIO引脚
https://stackoverflow.com/questions/58966062
复制相似问题