引脚声明的结构
我想知道应该如何为 MCP23017(I/O 扩展器微芯片)的引脚声明构建代码,以及是否应该将其放入函数中。
目前,我得到了以下脚本,其中包含 MCP 的引脚声明和许多使用它们的函数。这些函数由另一个文件调用,而这一切都在 Raspberry Pi 上进行。
import board
import busio
from digitalio import Direction
from adafruit_mcp230xx.mcp23017 import MCP23017
import time
i2c = busio.I2C(board.SCL, board.SDA)
mcp0 = MCP23017(i2c, address=0x27)
pin00 = mcp0.get_pin(0)
pin00.direction = Direction.OUTPUT
pin01 = mcp0.get_pin(1)
pin01.direction = Direction.OUTPUT
etc
pins = (pin00, pin01, etc)
def relays_off():
i = 0
while i < len(pins):
if not pins[i].value:
pins[i].value = True
i += 1
def other_function():
etc
整个项目是关于控制由这些引脚控制的继电器。 问题是,每次通电时都会出现错误,并且继电器会随机激活/停用。错误为[Errno 121]远程 I/O 错误
。 我必须执行一堆 relays_off()
来让它平静下来,直到不再出现错误,然后它就可以正常工作了。
所以我首先想知道我的编码是否正确,或者引脚声明是否应该位于其他函数调用的函数中?
I'm wondering how I should structure my code for the pin declarations of my MCP23017 (an I/O expander microchip) and whether I should put that in a function or not.
So currently I got the below script with my MCP's pin declarations and many functions using them. These functions are called by another file, and it's all this is on an Raspberry Pi.
import board
import busio
from digitalio import Direction
from adafruit_mcp230xx.mcp23017 import MCP23017
import time
i2c = busio.I2C(board.SCL, board.SDA)
mcp0 = MCP23017(i2c, address=0x27)
pin00 = mcp0.get_pin(0)
pin00.direction = Direction.OUTPUT
pin01 = mcp0.get_pin(1)
pin01.direction = Direction.OUTPUT
etc
pins = (pin00, pin01, etc)
def relays_off():
i = 0
while i < len(pins):
if not pins[i].value:
pins[i].value = True
i += 1
def other_function():
etc
The whole project is about controlling relays, controlled by those pins.
The issue is that every time I power it up I get an error and the relays are randomly activated/deactivated. The error is [Errno 121] Remote I/O error
.
I have to execute a bunch of relays_off()
to calm it down until I get no more errors, then it works fine.
So I'm first wondering if I coded that correctly, or if the pin declaration should be in a function called by the other functions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为简洁起见,省略了导入...
将所有引脚放在列表中怎么样?例如,
随后您可以通过引脚列表中各自的索引来访问引脚。
不确定这是否有帮助
Imports omitted for brevity...
How about having all the pins in a list? e.g.,
Subsequently you could access the pins by their respective index in pinlist.
Not sure if that helps